From 241d4cf94c33f23ab4160cb188dcbd1e2121de4c Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 16 Jun 2021 20:34:17 +0100 Subject: [PATCH 01/23] Reimplements non-callable sequence values. --- src/Expectation.php | 13 ++++++++++--- tests/.snapshots/success.txt | 4 +++- tests/Features/Expect/sequence.php | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index e35acc5f..f14491c3 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -110,7 +110,7 @@ final class Expectation if (is_callable($callback)) { foreach ($this->value as $item) { - $callback(expect($item)); + $callback(new Expectation($item)); } } @@ -119,8 +119,10 @@ final class Expectation /** * Allows you to specify a sequential set of expectations for each item in a iterable "value". + * + * @param mixed ...$callbacks */ - public function sequence(callable ...$callbacks): Expectation + public function sequence(...$callbacks): Expectation { if (!is_iterable($this->value)) { throw new BadMethodCallException('Expectation value is not iterable.'); @@ -138,7 +140,12 @@ final class Expectation } foreach ($values as $key => $item) { - call_user_func($callbacks[$key], expect($item), expect($keys[$key])); + if (is_callable($callbacks[$key])) { + call_user_func($callbacks[$key], new Expectation($item), new Expectation($keys[$key])); + continue; + } + + (new Expectation($item))->toEqual($callbacks[$key]); } return $this; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 9f57d4fa..2929bb74 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -152,6 +152,8 @@ ✓ loops back to the start if it runs out of sequence items ✓ it works if the number of items in the iterable is smaller than the number of expectations ✓ it works with associative arrays + ✓ it can be passed non-callable values + ✓ it can be passed a mixture of value types PASS Tests\Features\Expect\toBe ✓ strict comparisons @@ -554,5 +556,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 340 passed + Tests: 4 incompleted, 7 skipped, 342 passed \ No newline at end of file diff --git a/tests/Features/Expect/sequence.php b/tests/Features/Expect/sequence.php index fe6cb635..b4f82cb5 100644 --- a/tests/Features/Expect/sequence.php +++ b/tests/Features/Expect/sequence.php @@ -44,3 +44,19 @@ test('it works with associative arrays', function () { function ($expectation, $key) { $expectation->toEqual('boom'); $key->toEqual('baz'); }, ); }); + +test('it can be passed non-callable values', function () { + expect(['foo', 'bar', 'baz'])->sequence('foo', 'bar', 'baz'); + + expect(static::getCount())->toBe(3); +}); + +test('it can be passed a mixture of value types', function () { + expect(['foo', 'bar', 'baz'])->sequence( + 'foo', + function ($expectation) { $expectation->toEqual('bar')->toBeString(); }, + 'baz' + ); + + expect(static::getCount())->toBe(4); +}); From 7ff64540a64ec8bda987dab2501f0dc6165dae2f Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 16 Jun 2021 20:48:23 +0100 Subject: [PATCH 02/23] Adds nested Higher Order Expectations. --- src/Concerns/Extendable.php | 4 +- src/Concerns/RetrievesValues.php | 29 +++++++ src/Expectation.php | 26 +++++- src/HigherOrderExpectation.php | 79 ++++++++----------- tests/.snapshots/success.txt | 5 +- tests/Features/Expect/HigherOrder/methods.php | 13 +++ .../HigherOrder/methodsAndProperties.php | 15 ++++ .../Expect/HigherOrder/properties.php | 10 +++ 8 files changed, 128 insertions(+), 53 deletions(-) create mode 100644 src/Concerns/RetrievesValues.php diff --git a/src/Concerns/Extendable.php b/src/Concerns/Extendable.php index 36c39243..ee87f9ab 100644 --- a/src/Concerns/Extendable.php +++ b/src/Concerns/Extendable.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Pest\Concerns; +use BadMethodCallException; use Closure; -use Pest\HigherOrderExpectation; /** * @internal @@ -43,7 +43,7 @@ trait Extendable public function __call(string $method, array $parameters) { if (!static::hasExtend($method)) { - return new HigherOrderExpectation($this, $method, $parameters); + throw new BadMethodCallException("$method is not a callable method name."); } /** @var Closure $extend */ diff --git a/src/Concerns/RetrievesValues.php b/src/Concerns/RetrievesValues.php new file mode 100644 index 00000000..c38f5abd --- /dev/null +++ b/src/Concerns/RetrievesValues.php @@ -0,0 +1,29 @@ +|object $value + * @param mixed $default + * + * @return mixed + */ + private function retrieve(string $key, $value, $default = null) + { + if (is_array($value)) { + return $value[$key] ?? $default; + } + + // @phpstan-ignore-next-line + return $value->$key ?? $default; + } +} diff --git a/src/Expectation.php b/src/Expectation.php index e35acc5f..11d7c579 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -6,6 +6,7 @@ namespace Pest; use BadMethodCallException; use Pest\Concerns\Extendable; +use Pest\Concerns\RetrievesValues; use PHPUnit\Framework\Assert; use PHPUnit\Framework\Constraint\Constraint; use SebastianBergmann\Exporter\Exporter; @@ -18,7 +19,10 @@ use SebastianBergmann\Exporter\Exporter; */ final class Expectation { - use Extendable; + use Extendable { + __call as __extendsCall; + } + use RetrievesValues; /** * The expectation value. @@ -696,6 +700,24 @@ final class Expectation return $this->exporter->export($value); } + /** + * Dynamically handle calls to the class or + * creates a new higher order expectation. + * + * @param array $parameters + * + * @return mixed + */ + public function __call(string $method, array $parameters) + { + if (!static::hasExtend($method)) { + /* @phpstan-ignore-next-line */ + return new HigherOrderExpectation($this, $this->value->$method(...$parameters)); + } + + return $this->__extendsCall($method, $parameters); + } + /** * Dynamically calls methods on the class without any arguments * or creates a new higher order expectation. @@ -705,7 +727,7 @@ final class Expectation public function __get(string $name) { if (!method_exists($this, $name) && !static::hasExtend($name)) { - return new HigherOrderExpectation($this, $name); + return new HigherOrderExpectation($this, $this->retrieve($name, $this->value)); } /* @phpstan-ignore-next-line */ diff --git a/src/HigherOrderExpectation.php b/src/HigherOrderExpectation.php index 90044d6a..d2710ecc 100644 --- a/src/HigherOrderExpectation.php +++ b/src/HigherOrderExpectation.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pest; use Pest\Concerns\Expectable; +use Pest\Concerns\RetrievesValues; /** * @internal @@ -14,6 +15,7 @@ use Pest\Concerns\Expectable; final class HigherOrderExpectation { use Expectable; + use RetrievesValues; /** * @var Expectation @@ -30,6 +32,11 @@ final class HigherOrderExpectation */ private $opposite = false; + /** + * @var bool + */ + private $shouldReset = false; + /** * @var string */ @@ -38,45 +45,12 @@ final class HigherOrderExpectation /** * Creates a new higher order expectation. * - * @param array|null $parameters - * @phpstan-ignore-next-line + * @param mixed $value */ - public function __construct(Expectation $original, string $name, ?array $parameters = null) + public function __construct(Expectation $original, $value) { - $this->original = $original; - $this->name = $name; - - $this->expectation = $this->expect( - is_null($parameters) ? $this->getPropertyValue() : $this->getMethodValue($parameters) - ); - } - - /** - * Retrieves the property value from the original expectation. - * - * @return mixed - */ - private function getPropertyValue() - { - if (is_array($this->original->value)) { - return $this->original->value[$this->name]; - } - - // @phpstan-ignore-next-line - return $this->original->value->{$this->name}; - } - - /** - * Retrieves the value of the method from the original expectation. - * - * @param array $arguments - * - * @return mixed - */ - private function getMethodValue(array $arguments) - { - // @phpstan-ignore-next-line - return $this->original->value->{$this->name}(...$arguments); + $this->original = $original; + $this->expectation = $this->expect($value); } /** @@ -96,8 +70,9 @@ final class HigherOrderExpectation */ public function __call(string $name, array $arguments): self { - if (!$this->originalHasMethod($name)) { - return new self($this->original, $name, $arguments); + if (!$this->expectationHasMethod($name)) { + /* @phpstan-ignore-next-line */ + return new self($this->original, $this->getValue()->$name(...$arguments)); } return $this->performAssertion($name, $arguments); @@ -112,8 +87,8 @@ final class HigherOrderExpectation return $this->not(); } - if (!$this->originalHasMethod($name)) { - return new self($this->original, $name); + if (!$this->expectationHasMethod($name)) { + return new self($this->original, $this->retrieve($name, $this->getValue())); } return $this->performAssertion($name, []); @@ -122,11 +97,21 @@ final class HigherOrderExpectation /** * Determines if the original expectation has the given method name. */ - private function originalHasMethod(string $name): bool + private function expectationHasMethod(string $name): bool { return method_exists($this->original, $name) || $this->original::hasExtend($name); } + /** + * Retrieve the applicable value based on the current reset condition. + * + * @return mixed + */ + private function getValue() + { + return $this->shouldReset ? $this->original->value : $this->expectation->value; + } + /** * Performs the given assertion with the current expectation. * @@ -134,13 +119,11 @@ final class HigherOrderExpectation */ private function performAssertion(string $name, array $arguments): self { - $expectation = $this->opposite - ? $this->expectation->not() - : $this->expectation; + /* @phpstan-ignore-next-line */ + $this->expectation = ($this->opposite ? $this->expectation->not() : $this->expectation)->{$name}(...$arguments); - $this->expectation = $expectation->{$name}(...$arguments); // @phpstan-ignore-line - - $this->opposite = false; + $this->opposite = false; + $this->shouldReset = true; return $this; } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 9f57d4fa..e6496067 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -111,9 +111,11 @@ ✓ it works inside of each ✓ it works with sequence ✓ it can compose complex expectations + ✓ it can handle nested method calls PASS Tests\Features\Expect\HigherOrder\methodsAndProperties ✓ it can access methods and properties + ✓ it can handle nested methods and properties PASS Tests\Features\Expect\HigherOrder\properties ✓ it allows properties to be accessed from the value @@ -124,6 +126,7 @@ ✓ it works with sequence ✓ it can compose complex expectations ✓ it works with objects + ✓ it works with nested properties PASS Tests\Features\Expect\each ✓ an exception is thrown if the the type is not iterable @@ -554,5 +557,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 340 passed + Tests: 4 incompleted, 7 skipped, 343 passed \ No newline at end of file diff --git a/tests/Features/Expect/HigherOrder/methods.php b/tests/Features/Expect/HigherOrder/methods.php index 7f340f4a..66329b73 100644 --- a/tests/Features/Expect/HigherOrder/methods.php +++ b/tests/Features/Expect/HigherOrder/methods.php @@ -59,6 +59,14 @@ it('can compose complex expectations', function () { ); }); +it('can handle nested method calls', function () { + expect(new HasMethods()) + ->newInstance()->newInstance()->name()->toEqual('Has Methods')->toBeString() + ->newInstance()->name()->toEqual('Has Methods')->not->toBeInt + ->name()->toEqual('Has Methods') + ->books()->each->toBeArray(); +}); + class HasMethods { public function name() @@ -97,4 +105,9 @@ class HasMethods ], ]; } + + public function newInstance() + { + return new static(); + } } diff --git a/tests/Features/Expect/HigherOrder/methodsAndProperties.php b/tests/Features/Expect/HigherOrder/methodsAndProperties.php index 34ce09b4..f75f698a 100644 --- a/tests/Features/Expect/HigherOrder/methodsAndProperties.php +++ b/tests/Features/Expect/HigherOrder/methodsAndProperties.php @@ -14,10 +14,20 @@ it('can access methods and properties', function () { ); }); +it('can handle nested methods and properties', function () { + expect(new HasMethodsAndProperties()) + ->meta->foo->bar->toBeString()->toEqual('baz')->not->toBeInt + ->newInstance()->meta->foo->toBeArray() + ->newInstance()->multiply(2, 2)->toEqual(4)->not->toEqual(5) + ->newInstance()->books()->toBeArray(); +}); + class HasMethodsAndProperties { public $name = 'Has Methods and Properties'; + public $meta = ['foo' => ['bar' => 'baz']]; + public $posts = [ [ 'is_published' => true, @@ -47,4 +57,9 @@ class HasMethodsAndProperties { return $x * $y; } + + public function newInstance() + { + return new static(); + } } diff --git a/tests/Features/Expect/HigherOrder/properties.php b/tests/Features/Expect/HigherOrder/properties.php index c73e3d03..154d17e4 100644 --- a/tests/Features/Expect/HigherOrder/properties.php +++ b/tests/Features/Expect/HigherOrder/properties.php @@ -58,6 +58,12 @@ it('works with objects', function () { ); }); +it('works with nested properties', function () { + expect(new HasProperties()) + ->nested->foo->bar->toBeString()->toEqual('baz') + ->posts->toBeArray()->toHaveCount(2); +}); + class HasProperties { public $name = 'foo'; @@ -72,4 +78,8 @@ class HasProperties 'title' => 'Bar', ], ]; + + public $nested = [ + 'foo' => ['bar' => 'baz'], + ]; } From 579bb1b90caa3cc4490f8cf4175016995911023e Mon Sep 17 00:00:00 2001 From: luke Date: Thu, 17 Jun 2021 00:15:42 +0100 Subject: [PATCH 03/23] Updates DocBlock type hinting --- src/Expectation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Expectation.php b/src/Expectation.php index f14491c3..424f4b60 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -120,7 +120,7 @@ final class Expectation /** * Allows you to specify a sequential set of expectations for each item in a iterable "value". * - * @param mixed ...$callbacks + * @param callable(Expectation, Expectation): void|mixed ...$callbacks */ public function sequence(...$callbacks): Expectation { From d96a2485b67a265c773c42eee03154324756a65b Mon Sep 17 00:00:00 2001 From: luke Date: Thu, 17 Jun 2021 00:18:12 +0100 Subject: [PATCH 04/23] Updates snapshot --- tests/.snapshots/success.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 05d2cc3a..75333e28 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -111,9 +111,11 @@ ✓ it works inside of each ✓ it works with sequence ✓ it can compose complex expectations + ✓ it can handle nested method calls PASS Tests\Features\Expect\HigherOrder\methodsAndProperties ✓ it can access methods and properties + ✓ it can handle nested methods and properties PASS Tests\Features\Expect\HigherOrder\properties ✓ it allows properties to be accessed from the value @@ -124,6 +126,7 @@ ✓ it works with sequence ✓ it can compose complex expectations ✓ it works with objects + ✓ it works with nested properties PASS Tests\Features\Expect\each ✓ an exception is thrown if the the type is not iterable @@ -569,5 +572,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 355 passed + Tests: 4 incompleted, 7 skipped, 358 passed \ No newline at end of file From c1b27579ca791d3728f211c862448c4398f3e44f Mon Sep 17 00:00:00 2001 From: Daniel Ang Date: Fri, 18 Jun 2021 12:43:17 +0200 Subject: [PATCH 05/23] Method json() to parse JSON strings - Parse a JSON string into array - Test --- src/Expectation.php | 11 +++++++++++ tests/Features/Expect/json.php | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/Features/Expect/json.php diff --git a/src/Expectation.php b/src/Expectation.php index 45b52891..3c238805 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -60,6 +60,17 @@ final class Expectation return new self($value); } + /** + * Parses Json String to Array. + */ + public function json(): Expectation + { + Assert::assertIsString($this->value); + Assert::assertJson($this->value); + + return new self(json_decode($this->value, true)); + } + /** * Dump the expectation value and end the script. * diff --git a/tests/Features/Expect/json.php b/tests/Features/Expect/json.php new file mode 100644 index 00000000..2228fd56 --- /dev/null +++ b/tests/Features/Expect/json.php @@ -0,0 +1,14 @@ +json() + ->name + ->toBe('Nuno'); +}); + +test('fails with broken json string', function () { + expect('{":"Nuno"}')->json(); +})->throws(ExpectationFailedException::class); From ecb37fce91d6ddcf55bf9d5d6c4176127340ad35 Mon Sep 17 00:00:00 2001 From: Daniel Ang Date: Fri, 18 Jun 2021 13:04:31 +0200 Subject: [PATCH 06/23] Refactoring to use toBeJson() Refactoring according to @lukeraymonddowning suggestion --- src/Expectation.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 3c238805..e2d6369b 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -65,10 +65,7 @@ final class Expectation */ public function json(): Expectation { - Assert::assertIsString($this->value); - Assert::assertJson($this->value); - - return new self(json_decode($this->value, true)); + return $this->toBeJson()->and(json_decode($this->value, true)); } /** From 4bf69b97bdb20844b36f902535ba9a8eb2771f19 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 18 Jun 2021 12:06:41 +0100 Subject: [PATCH 07/23] feat(json): updates docs --- src/Expectation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Expectation.php b/src/Expectation.php index e2d6369b..85cb0094 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -61,7 +61,7 @@ final class Expectation } /** - * Parses Json String to Array. + * Creates a new expectation with the decoded JSON value. */ public function json(): Expectation { From 3afdedbd3f3e486483073fc0cacf87fb3ada454a Mon Sep 17 00:00:00 2001 From: Daniel Ang Date: Fri, 18 Jun 2021 13:11:37 +0200 Subject: [PATCH 08/23] Snapshots --- tests/.snapshots/success.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 05d2cc3a..422bfc2f 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -140,6 +140,10 @@ ✓ it macros true is true with argument ✓ it macros false is not true with argument + PASS Tests\Features\Expect\json + ✓ it properly parses json string + ✓ fails with broken json string + PASS Tests\Features\Expect\not ✓ not property calls @@ -569,5 +573,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 355 passed + Tests: 4 incompleted, 7 skipped, 357 passed \ No newline at end of file From db9f1254b5f40d20525b3bef096fb4c03bd32c6e Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 12:30:57 +0100 Subject: [PATCH 09/23] Tags Pest 1.6 --- src/Pest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pest.php b/src/Pest.php index 759cb913..847a15fc 100644 --- a/src/Pest.php +++ b/src/Pest.php @@ -6,7 +6,7 @@ namespace Pest; function version(): string { - return '1.5.0'; + return '1.6.0'; } function testDirectory(string $file = ''): string From 9b34650e7297c2c506bc88d7528e4e4aba132a77 Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 12:36:43 +0100 Subject: [PATCH 10/23] Adds changelog for 1.6.0 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee5a3af9..ab56a6b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [v1.6.0 (2021-06-18)](https://github.com/pestphp/pest/compare/v1.5.0...v1.6.0) +### Added +- Adds a new `json` expectation method to improve testing with JSON strings ([#325](https://github.com/pestphp/pest/pull/325)) +- Adds dot notation support to the `toHaveKey` and `toHaveKeys` expectations ([#322](https://github.com/pestphp/pest/pull/322)) + ## [v1.5.0 (2021-06-15)](https://github.com/pestphp/pest/compare/v1.4.0...v1.5.0) ### Changed - Moves plugins from the `require` section to the core itself ([#317](https://github.com/pestphp/pest/pull/317)), ([#318](https://github.com/pestphp/pest/pull/318)), ([#320](https://github.com/pestphp/pest/pull/320)) From b6f0496c3c071a897c054be449d98a74ef850dd4 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 18 Jun 2021 12:39:05 +0100 Subject: [PATCH 11/23] chore: updates funding --- .github/FUNDING.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 341f601a..8b8b5939 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -2,4 +2,3 @@ github: [nunomaduro,owenvoke,olivernybroe,octoper] patreon: nunomaduro -custom: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L From 8a384a6d657e466f3c17d672197917fff2a14197 Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 21:46:57 +0100 Subject: [PATCH 12/23] Merge fixes --- src/Expectation.php | 3 ++- tests/.snapshots/success.txt | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 57a7758a..53f17a3c 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -128,9 +128,10 @@ final class Expectation } /** + * @template TValue * Allows you to specify a sequential set of expectations for each item in a iterable "value". * - * @param callable(Expectation, Expectation): void|mixed ...$callbacks + * @param callable(Expectation, Expectation): void|TValue ...$callbacks */ public function sequence(...$callbacks): Expectation { diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 422bfc2f..3dd36cae 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -156,6 +156,8 @@ ✓ loops back to the start if it runs out of sequence items ✓ it works if the number of items in the iterable is smaller than the number of expectations ✓ it works with associative arrays + ✓ it can be passed non-callable values + ✓ it can be passed a mixture of value types PASS Tests\Features\Expect\toBe ✓ strict comparisons @@ -573,5 +575,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 357 passed + Tests: 4 incompleted, 7 skipped, 359 passed \ No newline at end of file From 3d7b6426a1ea9be0660b3f369ab2ab60b1ef205a Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 21:49:38 +0100 Subject: [PATCH 13/23] Updates snapshots --- tests/.snapshots/success.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 422bfc2f..ebe6ffe9 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -111,9 +111,11 @@ ✓ it works inside of each ✓ it works with sequence ✓ it can compose complex expectations + ✓ it can handle nested method calls PASS Tests\Features\Expect\HigherOrder\methodsAndProperties ✓ it can access methods and properties + ✓ it can handle nested methods and properties PASS Tests\Features\Expect\HigherOrder\properties ✓ it allows properties to be accessed from the value @@ -124,6 +126,7 @@ ✓ it works with sequence ✓ it can compose complex expectations ✓ it works with objects + ✓ it works with nested properties PASS Tests\Features\Expect\each ✓ an exception is thrown if the the type is not iterable @@ -573,5 +576,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 357 passed + Tests: 4 incompleted, 7 skipped, 360 passed \ No newline at end of file From 4b55de27f1f3306165a37a5329c42761321dcde0 Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 21:52:24 +0100 Subject: [PATCH 14/23] Adds generics --- src/Concerns/RetrievesValues.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Concerns/RetrievesValues.php b/src/Concerns/RetrievesValues.php index c38f5abd..e0c93bbc 100644 --- a/src/Concerns/RetrievesValues.php +++ b/src/Concerns/RetrievesValues.php @@ -10,12 +10,14 @@ namespace Pest\Concerns; trait RetrievesValues { /** + * @template TValue + * * Safely retrieve the value at the given key from an object or array. * - * @param array|object $value - * @param mixed $default + * @param array|object $value + * @param TValue|null $default * - * @return mixed + * @return TValue|null */ private function retrieve(string $key, $value, $default = null) { From aeded0a356a520631b2041d779306e883b8ec763 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 18 Jun 2021 22:01:14 +0100 Subject: [PATCH 15/23] refacto: coding style updates --- src/Expectation.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 53f17a3c..e619a6ae 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -120,7 +120,7 @@ final class Expectation if (is_callable($callback)) { foreach ($this->value as $item) { - $callback(new Expectation($item)); + $callback(new self($item)); } } @@ -128,10 +128,11 @@ final class Expectation } /** - * @template TValue * Allows you to specify a sequential set of expectations for each item in a iterable "value". * - * @param callable(Expectation, Expectation): void|TValue ...$callbacks + * @template TValue + * + * @param callable(self, self): void|TValue ...$callbacks */ public function sequence(...$callbacks): Expectation { @@ -152,11 +153,11 @@ final class Expectation foreach ($values as $key => $item) { if (is_callable($callbacks[$key])) { - call_user_func($callbacks[$key], new Expectation($item), new Expectation($keys[$key])); + call_user_func($callbacks[$key], new self($item), new self($keys[$key])); continue; } - (new Expectation($item))->toEqual($callbacks[$key]); + (new self($item))->toEqual($callbacks[$key]); } return $this; From 27de6106ab343bcea5ae9261a4d960ef31ba214b Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 22:01:16 +0100 Subject: [PATCH 16/23] Adds type hinting --- src/Concerns/Expectable.php | 5 ++++- src/Concerns/RetrievesValues.php | 8 ++++---- src/Expectation.php | 9 ++++++--- src/HigherOrderExpectation.php | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Concerns/Expectable.php b/src/Concerns/Expectable.php index 371bf8d4..5087572f 100644 --- a/src/Concerns/Expectable.php +++ b/src/Concerns/Expectable.php @@ -12,9 +12,12 @@ use Pest\Expectation; trait Expectable { /** + * @template TValue + * * Creates a new expectation. * - * @param mixed $value + * @param TValue $value + * @return Expectation */ public function expect($value): Expectation { diff --git a/src/Concerns/RetrievesValues.php b/src/Concerns/RetrievesValues.php index e0c93bbc..1d789e9d 100644 --- a/src/Concerns/RetrievesValues.php +++ b/src/Concerns/RetrievesValues.php @@ -10,14 +10,14 @@ namespace Pest\Concerns; trait RetrievesValues { /** - * @template TValue + * @template TRetrievableValue * * Safely retrieve the value at the given key from an object or array. * - * @param array|object $value - * @param TValue|null $default + * @param array|object $value + * @param TRetrievableValue|null $default * - * @return TValue|null + * @return TRetrievableValue|null */ private function retrieve(string $key, $value, $default = null) { diff --git a/src/Expectation.php b/src/Expectation.php index 73b78508..d9519f00 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -16,6 +16,8 @@ use SebastianBergmann\Exporter\Exporter; /** * @internal * + * @template TValue + * * @property Expectation $not Creates the opposite expectation. * @property Each $each Creates an expectation on each element on the traversable value. */ @@ -47,7 +49,7 @@ final class Expectation /** * Creates a new expectation. * - * @param mixed $value + * @param TValue $value */ public function __construct($value) { @@ -57,7 +59,8 @@ final class Expectation /** * Creates a new expectation. * - * @param mixed $value + * @param TValue $value + * @return Expectation */ public function and($value): Expectation { @@ -722,7 +725,7 @@ final class Expectation * * @param array $parameters * - * @return mixed + * @return HigherOrderExpectation|mixed */ public function __call(string $method, array $parameters) { diff --git a/src/HigherOrderExpectation.php b/src/HigherOrderExpectation.php index d2710ecc..8d29aa3d 100644 --- a/src/HigherOrderExpectation.php +++ b/src/HigherOrderExpectation.php @@ -66,7 +66,7 @@ final class HigherOrderExpectation /** * Dynamically calls methods on the class with the given arguments. * - * @param array $arguments + * @param array $arguments */ public function __call(string $name, array $arguments): self { @@ -115,7 +115,7 @@ final class HigherOrderExpectation /** * Performs the given assertion with the current expectation. * - * @param array $arguments + * @param array $arguments */ private function performAssertion(string $name, array $arguments): self { From 99bcf98617efd775dd58620a069053d964e8ec14 Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 22:03:01 +0100 Subject: [PATCH 17/23] Rebuilds snapshots --- tests/.snapshots/success.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 3dd36cae..e7ecc1a0 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -111,9 +111,11 @@ ✓ it works inside of each ✓ it works with sequence ✓ it can compose complex expectations + ✓ it can handle nested method calls PASS Tests\Features\Expect\HigherOrder\methodsAndProperties ✓ it can access methods and properties + ✓ it can handle nested methods and properties PASS Tests\Features\Expect\HigherOrder\properties ✓ it allows properties to be accessed from the value @@ -124,6 +126,7 @@ ✓ it works with sequence ✓ it can compose complex expectations ✓ it works with objects + ✓ it works with nested properties PASS Tests\Features\Expect\each ✓ an exception is thrown if the the type is not iterable @@ -575,5 +578,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 359 passed + Tests: 4 incompleted, 7 skipped, 362 passed \ No newline at end of file From 3a20696da4a3e1e2a352e93844a7394a4af7a53b Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 22:03:51 +0100 Subject: [PATCH 18/23] CS --- src/Concerns/Expectable.php | 1 + src/Concerns/RetrievesValues.php | 2 +- src/Expectation.php | 1 + src/HigherOrderExpectation.php | 4 ---- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Concerns/Expectable.php b/src/Concerns/Expectable.php index 5087572f..981e443d 100644 --- a/src/Concerns/Expectable.php +++ b/src/Concerns/Expectable.php @@ -17,6 +17,7 @@ trait Expectable * Creates a new expectation. * * @param TValue $value + * * @return Expectation */ public function expect($value): Expectation diff --git a/src/Concerns/RetrievesValues.php b/src/Concerns/RetrievesValues.php index 1d789e9d..a8d832b1 100644 --- a/src/Concerns/RetrievesValues.php +++ b/src/Concerns/RetrievesValues.php @@ -15,7 +15,7 @@ trait RetrievesValues * Safely retrieve the value at the given key from an object or array. * * @param array|object $value - * @param TRetrievableValue|null $default + * @param TRetrievableValue|null $default * * @return TRetrievableValue|null */ diff --git a/src/Expectation.php b/src/Expectation.php index 6477b3d6..32299dc2 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -60,6 +60,7 @@ final class Expectation * Creates a new expectation. * * @param TValue $value + * * @return Expectation */ public function and($value): Expectation diff --git a/src/HigherOrderExpectation.php b/src/HigherOrderExpectation.php index 8d29aa3d..14c8504c 100644 --- a/src/HigherOrderExpectation.php +++ b/src/HigherOrderExpectation.php @@ -65,8 +65,6 @@ final class HigherOrderExpectation /** * Dynamically calls methods on the class with the given arguments. - * - * @param array $arguments */ public function __call(string $name, array $arguments): self { @@ -114,8 +112,6 @@ final class HigherOrderExpectation /** * Performs the given assertion with the current expectation. - * - * @param array $arguments */ private function performAssertion(string $name, array $arguments): self { From 22a1aac84a08a863d26adefe285b7c97da76a016 Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 22:08:47 +0100 Subject: [PATCH 19/23] Fixes types --- src/Expectation.php | 4 ++-- src/HigherOrderExpectation.php | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 32299dc2..e41f21d0 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -138,9 +138,9 @@ final class Expectation /** * Allows you to specify a sequential set of expectations for each item in a iterable "value". * - * @template TValue + * @template TSequenceValue * - * @param callable(self, self): void|TValue ...$callbacks + * @param callable(self, self): void|TSequenceValue ...$callbacks */ public function sequence(...$callbacks): Expectation { diff --git a/src/HigherOrderExpectation.php b/src/HigherOrderExpectation.php index 14c8504c..6e2ed23f 100644 --- a/src/HigherOrderExpectation.php +++ b/src/HigherOrderExpectation.php @@ -65,6 +65,8 @@ final class HigherOrderExpectation /** * Dynamically calls methods on the class with the given arguments. + * + * @param array $arguments */ public function __call(string $name, array $arguments): self { @@ -112,6 +114,8 @@ final class HigherOrderExpectation /** * Performs the given assertion with the current expectation. + * + * @param array $arguments */ private function performAssertion(string $name, array $arguments): self { From c773d1cd57bf832cc18f4beffe7bf89d42f462bb Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 18 Jun 2021 22:09:56 +0100 Subject: [PATCH 20/23] chore: type checks scripts folder --- phpstan.neon | 1 + scripts/compile.php | 1 + 2 files changed, 2 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index a32cf76a..35d5f07e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,6 +7,7 @@ parameters: level: max paths: - src + - scripts checkMissingIterableValueType: true checkGenericClassInNonGenericObjectType: false diff --git a/scripts/compile.php b/scripts/compile.php index 56c3974c..7607bc90 100644 --- a/scripts/compile.php +++ b/scripts/compile.php @@ -15,6 +15,7 @@ $globalsFilePath = implode(DIRECTORY_SEPARATOR, [ $compiledFilePath = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__), 'compiled', 'globals.php']); +/** @phpstan-ignore-next-line */ @unlink($compiledFilePath); $replace = function ($contents, $string, $by) { From d16a48bf0fdc9eca724e10f5f452240733e60376 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 18 Jun 2021 22:16:54 +0100 Subject: [PATCH 21/23] chore: cs --- scripts/compile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/compile.php b/scripts/compile.php index 7607bc90..3779c421 100644 --- a/scripts/compile.php +++ b/scripts/compile.php @@ -15,7 +15,7 @@ $globalsFilePath = implode(DIRECTORY_SEPARATOR, [ $compiledFilePath = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__), 'compiled', 'globals.php']); -/** @phpstan-ignore-next-line */ +/* @phpstan-ignore-next-line */ @unlink($compiledFilePath); $replace = function ($contents, $string, $by) { From f6d3ce41bc6d58a1895118fa50fd669b7ba3f274 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 18 Jun 2021 22:17:44 +0100 Subject: [PATCH 22/23] chore: updates funding --- .github/FUNDING.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 8b8b5939..8e345cd7 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,4 +1,4 @@ # These are supported funding model platforms -github: [nunomaduro,owenvoke,olivernybroe,octoper] +github: [nunomaduro,owenvoke,olivernybroe,octoper,lukeraymonddowning] patreon: nunomaduro From 1680613e1250070ab6ccfcbec429d04f1830f5bc Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 19 Jun 2021 14:42:29 +0100 Subject: [PATCH 23/23] docs: update changelog --- CHANGELOG.md | 5 +++++ src/Pest.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab56a6b0..38a673cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [v1.7.0 (2021-06-19)](https://github.com/pestphp/pest/compare/v1.6.0...v1.7.0) +### Added +- Support for non-callable values in the sequence method, which will be passed as `toEqual` ([#323](https://github.com/pestphp/pest/pull/323)) +- Support for nested Higher Order Expectations ([#324](https://github.com/pestphp/pest/pull/324)) + ## [v1.6.0 (2021-06-18)](https://github.com/pestphp/pest/compare/v1.5.0...v1.6.0) ### Added - Adds a new `json` expectation method to improve testing with JSON strings ([#325](https://github.com/pestphp/pest/pull/325)) diff --git a/src/Pest.php b/src/Pest.php index 847a15fc..6fc5894b 100644 --- a/src/Pest.php +++ b/src/Pest.php @@ -6,7 +6,7 @@ namespace Pest; function version(): string { - return '1.6.0'; + return '1.7.0'; } function testDirectory(string $file = ''): string