From 847b06e5584f1fe0f4031ec334d0d9a586bc4c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Thu, 23 Sep 2021 03:35:41 +0300 Subject: [PATCH 1/3] add `when()` method --- src/Expectation.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Expectation.php b/src/Expectation.php index 253b1e1e..7a1e6057 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -177,6 +177,26 @@ final class Expectation return $this; } + /** + * It skips the tests in the callback if the condition is not truthy. + * + * @param Closure|bool|string $condition + */ + public function when($condition, callable $callback): Expectation + { + $condition = is_callable($condition) + ? $condition + : function () use ($condition) { + return $condition; + }; + + if ($condition()) { + $callback(new self($this->value)); + } + + return $this; + } + /** * Asserts that two variables have the same type and * value. Used on objects, it asserts that two From f41c3ce9ba4da273a780044110d633aa045ec4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Thu, 23 Sep 2021 03:35:46 +0300 Subject: [PATCH 2/3] add tests --- tests/Features/Expect/when.php | 101 +++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/Features/Expect/when.php diff --git a/tests/Features/Expect/when.php b/tests/Features/Expect/when.php new file mode 100644 index 00000000..db9fa4f1 --- /dev/null +++ b/tests/Features/Expect/when.php @@ -0,0 +1,101 @@ +whenObject = new stdClass(); + $this->whenObject->trueValue = true; + $this->whenObject->foo = 'foo'; +}); + +it('pass', function () { + expect('foo') + ->when( + true, + function ($value) { + return $value->toEqual('foo'); + } + ) + ->toEqual('foo'); + + expect(static::getCount())->toBe(2); +}); + +it('failures', function () { + expect('foo') + ->when( + true, + function ($value) { + return $value->toBeTrue(); + } + ) + ->toEqual('foo'); +})->throws(ExpectationFailedException::class, 'is true'); + +it('runs with truthy', function () { + expect($this->whenObject) + ->when( + 1, + function ($value) { + return $value->trueValue->toBeTrue(); + } + ) + ->foo->toEqual('foo'); + + expect(static::getCount())->toBe(2); +}); + +it('skips with falsy', function () { + expect($this->whenObject) + ->when( + 0, + function ($value) { + return $value->trueValue->toBeFalse(); // fails + } + ) + ->when( + false, + function ($value) { + return $value->trueValue->toBeFalse(); // fails + } + ) + ->foo->toEqual('foo'); + + expect(static::getCount())->toBe(1); +}); + +it('runs with truthy closure condition', function () { + expect($this->whenObject) + ->when( + function () { return '1'; }, + function ($value) { + return $value->trueValue->toBeTrue(); + } + ) + ->foo->toEqual('foo'); + + expect(static::getCount())->toBe(2); +}); + +it('skips with falsy closure condition', function () { + expect($this->whenObject) + ->when( + function () { return '0'; }, + function ($value) { + return $value->trueValue->toBeFalse(); // fails + } + ) + ->foo->toEqual('foo'); + + expect(static::getCount())->toBe(1); +}); + +it('can be used in higher order tests') + ->expect(false) + ->when( + function () { return true; }, + function ($value) { + return $value->toBeTrue(); + } + ) + ->throws(ExpectationFailedException::class, 'false is true'); From 1681c1f4f81790b6adcc9474ad6937b3e55f9242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Thu, 23 Sep 2021 03:35:56 +0300 Subject: [PATCH 3/3] update snapshots --- tests/.snapshots/success.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 4442bbb9..328c8e9f 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -481,6 +481,15 @@ ✓ closure missing parameter ✓ closure missing type-hint + PASS Tests\Features\Expect\when + ✓ it pass + ✓ it failures + ✓ it runs with truthy + ✓ it skips with falsy + ✓ it runs with truthy closure condition + ✓ it skips with falsy closure condition + ✓ it can be used in higher order tests + PASS Tests\Features\Helpers ✓ it can set/get properties on $this ✓ it throws error if property do not exist @@ -681,5 +690,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 447 passed + Tests: 4 incompleted, 9 skipped, 454 passed \ No newline at end of file