mirror of
https://github.com/pestphp/pest.git
synced 2026-03-07 00:07:22 +01:00
Merge pull request #399 from mertasan/sequence
Fix: `sequence()` can generate false positives
This commit is contained in:
@ -154,9 +154,10 @@ final class Expectation
|
|||||||
throw new BadMethodCallException('Expectation value is not iterable.');
|
throw new BadMethodCallException('Expectation value is not iterable.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = is_array($this->value) ? $this->value : iterator_to_array($this->value);
|
$value = is_array($this->value) ? $this->value : iterator_to_array($this->value);
|
||||||
$keys = array_keys($value);
|
$keys = array_keys($value);
|
||||||
$values = array_values($value);
|
$values = array_values($value);
|
||||||
|
$callbacksCount = count($callbacks);
|
||||||
|
|
||||||
$index = 0;
|
$index = 0;
|
||||||
|
|
||||||
@ -165,6 +166,10 @@ final class Expectation
|
|||||||
$index = $index < count($values) - 1 ? $index + 1 : 0;
|
$index = $index < count($values) - 1 ? $index + 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($callbacksCount > count($values)) {
|
||||||
|
Assert::assertLessThanOrEqual(count($value), count($callbacks));
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($values as $key => $item) {
|
foreach ($values as $key => $item) {
|
||||||
if (is_callable($callbacks[$key])) {
|
if (is_callable($callbacks[$key])) {
|
||||||
call_user_func($callbacks[$key], new self($item), new self($keys[$key]));
|
call_user_func($callbacks[$key], new self($item), new self($keys[$key]));
|
||||||
|
|||||||
@ -184,7 +184,7 @@
|
|||||||
✓ an exception is thrown if the the type is not iterable
|
✓ an exception is thrown if the the type is not iterable
|
||||||
✓ allows for sequences of checks to be run on iterable data
|
✓ allows for sequences of checks to be run on iterable data
|
||||||
✓ loops back to the start if it runs out of sequence items
|
✓ 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
|
✓ fails if the number of iterable items is greater than the number of expectations
|
||||||
✓ it works with associative arrays
|
✓ it works with associative arrays
|
||||||
✓ it can be passed non-callable values
|
✓ it can be passed non-callable values
|
||||||
✓ it can be passed a mixture of value types
|
✓ it can be passed a mixture of value types
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
test('an exception is thrown if the the type is not iterable', function () {
|
test('an exception is thrown if the the type is not iterable', function () {
|
||||||
expect('Foobar')->each->sequence();
|
expect('Foobar')->each->sequence();
|
||||||
})->throws(BadMethodCallException::class, 'Expectation value is not iterable.');
|
})->throws(BadMethodCallException::class, 'Expectation value is not iterable.');
|
||||||
@ -26,16 +28,14 @@ test('loops back to the start if it runs out of sequence items', function () {
|
|||||||
expect(static::getCount())->toBe(16);
|
expect(static::getCount())->toBe(16);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it works if the number of items in the iterable is smaller than the number of expectations', function () {
|
test('fails if the number of iterable items is greater than the number of expectations', function () {
|
||||||
expect([1, 2])
|
expect([1, 2])
|
||||||
->sequence(
|
->sequence(
|
||||||
function ($expectation) { $expectation->toBeInt()->toEqual(1); },
|
function ($expectation) { $expectation->toBeInt()->toEqual(1); },
|
||||||
function ($expectation) { $expectation->toBeInt()->toEqual(2); },
|
function ($expectation) { $expectation->toBeInt()->toEqual(2); },
|
||||||
function ($expectation) { $expectation->toBeInt()->toEqual(3); },
|
function ($expectation) { $expectation->toBeInt()->toEqual(3); },
|
||||||
);
|
);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
expect(static::getCount())->toBe(4);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it works with associative arrays', function () {
|
test('it works with associative arrays', function () {
|
||||||
expect(['foo' => 'bar', 'baz' => 'boom'])
|
expect(['foo' => 'bar', 'baz' => 'boom'])
|
||||||
|
|||||||
Reference in New Issue
Block a user