From 42138a4ecfe6800ef3db2c03c3e0ce2aabd38496 Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 12:05:39 +0100 Subject: [PATCH 1/9] Sequenced iterables can now be passed as direct values. --- src/Expectation.php | 13 ++++++++++--- tests/Expect/sequence.php | 6 ++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index f543de9..f967211 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -110,7 +110,7 @@ public function each(callable $callback = null): Each if (is_callable($callback)) { foreach ($this->value as $item) { - $callback(expect($item)); + $callback(new Expectation($item)); } } @@ -119,8 +119,10 @@ public function each(callable $callback = null): Each /** * 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 @@ public function sequence(callable ...$callbacks): 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/Expect/sequence.php b/tests/Expect/sequence.php index fe6cb63..d98d1a9 100644 --- a/tests/Expect/sequence.php +++ b/tests/Expect/sequence.php @@ -44,3 +44,9 @@ function ($expectation, $key) { $expectation->toEqual('bar'); $key->toEqual('foo 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); +}); From 1692a1c7260a2ac9a03e8270e4d93b38311378bf Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 12:09:17 +0100 Subject: [PATCH 2/9] Adds another test --- tests/Expect/sequence.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Expect/sequence.php b/tests/Expect/sequence.php index d98d1a9..b4f82cb 100644 --- a/tests/Expect/sequence.php +++ b/tests/Expect/sequence.php @@ -50,3 +50,13 @@ function ($expectation, $key) { $expectation->toEqual('boom'); $key->toEqual('ba 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 f1df5f9e440a98831df3621590bb6ca36cbb48be Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 16:48:16 +0100 Subject: [PATCH 3/9] Attempts to fix CI --- .github/workflows/static.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 9e8f74d..e9e992a 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -20,7 +20,7 @@ jobs: coverage: none - name: Install Dependencies - run: composer update --no-interaction --no-progress --ansi + run: COMPOSER_ROOT_VERSION=dev-main composer update --no-interaction --no-progress --ansi - name: Run PHP-CS-Fixer run: vendor/bin/php-cs-fixer fix -v --allow-risky=yes --dry-run --ansi @@ -45,7 +45,7 @@ jobs: coverage: none - name: Install Dependencies - run: composer update --prefer-stable --no-interaction --no-progress --ansi + run: COMPOSER_ROOT_VERSION=dev-main composer update --prefer-stable --no-interaction --no-progress --ansi - name: Run PHPStan run: vendor/bin/phpstan analyse --no-progress --ansi From c85f3fdd77b935a2174cf8281b8cd199b74d461a Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 16:49:59 +0100 Subject: [PATCH 4/9] Attempts to fix CI --- .github/workflows/static.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index e9e992a..9977848 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -20,7 +20,7 @@ jobs: coverage: none - name: Install Dependencies - run: COMPOSER_ROOT_VERSION=dev-main composer update --no-interaction --no-progress --ansi + run: COMPOSER_ROOT_VERSION=dev-master composer update --no-interaction --no-progress --ansi - name: Run PHP-CS-Fixer run: vendor/bin/php-cs-fixer fix -v --allow-risky=yes --dry-run --ansi @@ -45,7 +45,7 @@ jobs: coverage: none - name: Install Dependencies - run: COMPOSER_ROOT_VERSION=dev-main composer update --prefer-stable --no-interaction --no-progress --ansi + run: COMPOSER_ROOT_VERSION=dev-master composer update --prefer-stable --no-interaction --no-progress --ansi - name: Run PHPStan run: vendor/bin/phpstan analyse --no-progress --ansi From 1a2e0f40b2d093aaa97e62900c9e562e396b332a Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 16:51:06 +0100 Subject: [PATCH 5/9] Fixes CI --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a6149f2..aee6981 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -30,7 +30,7 @@ jobs: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - name: Install PHP dependencies - run: composer update --${{ matrix.dependency-version }} --no-interaction --no-progress --ansi + run: COMPOSER_ROOT_VERSION=dev-master composer update --${{ matrix.dependency-version }} --no-interaction --no-progress --ansi - name: Unit Tests run: ./vendor/bin/pest --colors=always From ee1f92fff8ba4367669c79c78a9952ef79aaf2a9 Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 16:57:16 +0100 Subject: [PATCH 6/9] Fixes CI --- .github/workflows/static.yml | 7 +++++-- .github/workflows/tests.yml | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 9977848..25377c4 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -4,6 +4,9 @@ on: ['push', 'pull_request'] jobs: cs: + env: + COMPOSER_ROOT_VERSION: dev-master + runs-on: ubuntu-latest name: Code Style @@ -20,7 +23,7 @@ jobs: coverage: none - name: Install Dependencies - run: COMPOSER_ROOT_VERSION=dev-master composer update --no-interaction --no-progress --ansi + run: composer update --no-interaction --no-progress --ansi - name: Run PHP-CS-Fixer run: vendor/bin/php-cs-fixer fix -v --allow-risky=yes --dry-run --ansi @@ -45,7 +48,7 @@ jobs: coverage: none - name: Install Dependencies - run: COMPOSER_ROOT_VERSION=dev-master composer update --prefer-stable --no-interaction --no-progress --ansi + run: composer update --prefer-stable --no-interaction --no-progress --ansi - name: Run PHPStan run: vendor/bin/phpstan analyse --no-progress --ansi diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index aee6981..870eddb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,6 +4,8 @@ on: ['push', 'pull_request'] jobs: ci: + env: + COMPOSER_ROOT_VERSION: dev-master runs-on: ${{ matrix.os }} strategy: matrix: @@ -30,7 +32,7 @@ jobs: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - name: Install PHP dependencies - run: COMPOSER_ROOT_VERSION=dev-master composer update --${{ matrix.dependency-version }} --no-interaction --no-progress --ansi + run: composer update --${{ matrix.dependency-version }} --no-interaction --no-progress --ansi - name: Unit Tests run: ./vendor/bin/pest --colors=always From c97ef7b27665c186d10964cec73d92eff658773a Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 16:58:30 +0100 Subject: [PATCH 7/9] Fixes CI --- .github/workflows/static.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 25377c4..2eaa154 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -29,6 +29,9 @@ jobs: run: vendor/bin/php-cs-fixer fix -v --allow-risky=yes --dry-run --ansi phpstan: + env: + COMPOSER_ROOT_VERSION: dev-master + runs-on: ubuntu-latest strategy: matrix: From f6ef6ed2163387690f7d6d29f5a7761690984583 Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 17:05:54 +0100 Subject: [PATCH 8/9] Adds same functionality in `each` call --- src/Expectation.php | 11 ++++++++++- tests/Expect/each.php | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Expectation.php b/src/Expectation.php index f967211..736d943 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -101,13 +101,22 @@ public function not(): OppositeExpectation /** * Creates an expectation on each item of the iterable "value". + * + * @param callable|mixed $callback + * @param mixed $arguments + * + * @return Each|Expectation */ - public function each(callable $callback = null): Each + public function each($callback = null, ...$arguments) { if (!is_iterable($this->value)) { throw new BadMethodCallException('Expectation value is not iterable.'); } + if (count($arguments) > 0) { + return $this->sequence($callback, ...$arguments); + } + if (is_callable($callback)) { foreach ($this->value as $item) { $callback(new Expectation($item)); diff --git a/tests/Expect/each.php b/tests/Expect/each.php index 2857cd2..6c96f1b 100644 --- a/tests/Expect/each.php +++ b/tests/Expect/each.php @@ -87,3 +87,17 @@ expect(static::getCount())->toBe(12); }); + +test('it can be passed multiple arguments and defers to sequence if so', function () { + expect(['foo', 'bar', 'baz'])->sequence('foo', 'bar', 'baz'); + + expect(static::getCount())->toBe(3); + + expect(['foo', 'bar', 'baz'])->each( + 'foo', + function ($expectation) { $expectation->toEqual('bar')->toBeString(); }, + 'baz' + ); + + expect(static::getCount())->toBe(8); // +1 for first count check +}); From 3c30456036eca40e6caf3a83606645133244372d Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Jun 2021 19:25:07 +0100 Subject: [PATCH 9/9] Reverts each values --- src/Expectation.php | 11 +---------- tests/Expect/each.php | 14 -------------- 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 736d943..f967211 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -101,22 +101,13 @@ public function not(): OppositeExpectation /** * Creates an expectation on each item of the iterable "value". - * - * @param callable|mixed $callback - * @param mixed $arguments - * - * @return Each|Expectation */ - public function each($callback = null, ...$arguments) + public function each(callable $callback = null): Each { if (!is_iterable($this->value)) { throw new BadMethodCallException('Expectation value is not iterable.'); } - if (count($arguments) > 0) { - return $this->sequence($callback, ...$arguments); - } - if (is_callable($callback)) { foreach ($this->value as $item) { $callback(new Expectation($item)); diff --git a/tests/Expect/each.php b/tests/Expect/each.php index 6c96f1b..2857cd2 100644 --- a/tests/Expect/each.php +++ b/tests/Expect/each.php @@ -87,17 +87,3 @@ expect(static::getCount())->toBe(12); }); - -test('it can be passed multiple arguments and defers to sequence if so', function () { - expect(['foo', 'bar', 'baz'])->sequence('foo', 'bar', 'baz'); - - expect(static::getCount())->toBe(3); - - expect(['foo', 'bar', 'baz'])->each( - 'foo', - function ($expectation) { $expectation->toEqual('bar')->toBeString(); }, - 'baz' - ); - - expect(static::getCount())->toBe(8); // +1 for first count check -});