From 1e61144cd2e27b3a9169b731043901941dfe6b12 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 15 Jul 2020 01:05:36 +0200 Subject: [PATCH] feat(expect): handle property calls to opposite expectations --- src/Expectation.php | 2 +- src/OppositeExpectation.php | 15 +++++++++++++++ tests/.snapshots/success.txt | 7 +++++-- tests/Expect/not.php | 10 ++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/Expect/not.php diff --git a/src/Expectation.php b/src/Expectation.php index 67d5aa9e..4aa2eee9 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -9,7 +9,7 @@ use PHPUnit\Framework\Assert; /** * @internal * - * @property Expectation $not Creates the opposite expectation. + * @property Expectation not Creates the opposite expectation. */ final class Expectation { diff --git a/src/OppositeExpectation.php b/src/OppositeExpectation.php index 08a7ebc6..4ab4449d 100644 --- a/src/OppositeExpectation.php +++ b/src/OppositeExpectation.php @@ -42,4 +42,19 @@ final class OppositeExpectation 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')); + } } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 9b6e5b25..af19b3e5 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2,6 +2,9 @@ PASS Tests\CustomTestCase\ExecutedTest ✓ that gets executed + PASS Tests\Expect\not + ✓ not property calls + PASS Tests\Expect\toBe ✓ expect true → toBeTrue → and false → toBeFalse ✓ strict comparisons @@ -309,5 +312,5 @@ WARN Tests\Visual\Success - visual snapshot of test suite on success - Tests: 6 skipped, 182 passed - Time: 5.72s + Tests: 6 skipped, 183 passed + Time: 5.77s diff --git a/tests/Expect/not.php b/tests/Expect/not.php new file mode 100644 index 00000000..79220b19 --- /dev/null +++ b/tests/Expect/not.php @@ -0,0 +1,10 @@ +toBeTrue() + ->not()->toBeFalse() + ->not->toBeFalse + ->and(false) + ->toBeFalse(); +});