Skip to content
This repository was archived by the owner on Jun 16, 2021. It is now read-only.

Sequenced expectations can now be passed as non-callable values #16

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on: ['push', 'pull_request']

jobs:
cs:
env:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, this could be added to a top level env key to remove the duplication, but not really necessary. 👍🏻

COMPOSER_ROOT_VERSION: dev-master

runs-on: ubuntu-latest

name: Code Style
Expand All @@ -26,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:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on: ['push', 'pull_request']

jobs:
ci:
env:
COMPOSER_ROOT_VERSION: dev-master
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand Down
13 changes: 10 additions & 3 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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.');
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions tests/Expect/sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ 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);
});

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);
});