feat(expect): handle property calls to opposite expectations

This commit is contained in:
Nuno Maduro
2020-07-15 01:05:36 +02:00
parent 2751bc9674
commit 1e61144cd2
4 changed files with 31 additions and 3 deletions

View File

@ -9,7 +9,7 @@ use PHPUnit\Framework\Assert;
/** /**
* @internal * @internal
* *
* @property Expectation $not Creates the opposite expectation. * @property Expectation not Creates the opposite expectation.
*/ */
final class Expectation final class Expectation
{ {

View File

@ -42,4 +42,19 @@ final class OppositeExpectation
throw new ExpectationFailedException(sprintf('@todo')); throw new ExpectationFailedException(sprintf('@todo'));
} }
/**
* Handle dynamic properties gets into the original expectation.
*/
public function __get(string $name): Expectation
{
try {
/* @phpstan-ignore-next-line */
$this->original->{$name};
} catch (ExpectationFailedException $e) {
return $this->original;
}
throw new ExpectationFailedException(sprintf('@todo'));
}
} }

View File

@ -2,6 +2,9 @@
PASS Tests\CustomTestCase\ExecutedTest PASS Tests\CustomTestCase\ExecutedTest
✓ that gets executed ✓ that gets executed
PASS Tests\Expect\not
✓ not property calls
PASS Tests\Expect\toBe PASS Tests\Expect\toBe
✓ expect true → toBeTrue → and false → toBeFalse ✓ expect true → toBeTrue → and false → toBeFalse
✓ strict comparisons ✓ strict comparisons
@ -309,5 +312,5 @@
WARN Tests\Visual\Success WARN Tests\Visual\Success
- visual snapshot of test suite on success - visual snapshot of test suite on success
Tests: 6 skipped, 182 passed Tests: 6 skipped, 183 passed
Time: 5.72s Time: 5.77s

10
tests/Expect/not.php Normal file
View File

@ -0,0 +1,10 @@
<?php
test('not property calls', function () {
expect(true)
->toBeTrue()
->not()->toBeFalse()
->not->toBeFalse
->and(false)
->toBeFalse();
});