mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
feat(sequence): Add support for nested traversable
This commit is contained in:
@ -297,12 +297,14 @@
|
||||
|
||||
PASS Tests\Features\Expect\sequence
|
||||
✓ an exception is thrown if the the type is not iterable
|
||||
✓ an exception is thrown if there are no expectations
|
||||
✓ allows for sequences of checks to be run on iterable data
|
||||
✓ loops back to the start if it runs out of sequence items
|
||||
✓ fails if the number of iterable items is greater than the number of expectations
|
||||
✓ fails if the number of iterable items is less 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
|
||||
✓ it works with traversables
|
||||
|
||||
PASS Tests\Features\Expect\toBe
|
||||
✓ strict comparisons
|
||||
@ -1194,4 +1196,4 @@
|
||||
WARN Tests\Visual\Version
|
||||
- visual snapshot of help command output
|
||||
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 844 passed (1947 assertions)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 845 passed (1965 assertions)
|
||||
@ -1,11 +1,13 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\ExpectationFailedException;
|
||||
|
||||
test('an exception is thrown if the the type is not iterable', function () {
|
||||
expect('Foobar')->each->sequence();
|
||||
})->throws(BadMethodCallException::class, 'Expectation value is not iterable.');
|
||||
|
||||
test('an exception is thrown if there are no expectations', function () {
|
||||
expect(['Foobar'])->sequence();
|
||||
})->throws(InvalidArgumentException::class, 'No sequence expectations defined.');
|
||||
|
||||
test('allows for sequences of checks to be run on iterable data', function () {
|
||||
expect([1, 2, 3])
|
||||
->sequence(
|
||||
@ -40,7 +42,7 @@ test('loops back to the start if it runs out of sequence items', function () {
|
||||
expect(static::getCount())->toBe(16);
|
||||
});
|
||||
|
||||
test('fails if the number of iterable items is greater than the number of expectations', function () {
|
||||
test('fails if the number of iterable items is less than the number of expectations', function () {
|
||||
expect([1, 2])
|
||||
->sequence(
|
||||
function ($expectation) {
|
||||
@ -53,7 +55,7 @@ test('fails if the number of iterable items is greater than the number of expect
|
||||
$expectation->toBeInt()->toEqual(3);
|
||||
},
|
||||
);
|
||||
})->throws(ExpectationFailedException::class);
|
||||
})->throws(OutOfRangeException::class, 'Sequence expectations are more than the iterable items.');
|
||||
|
||||
test('it works with associative arrays', function () {
|
||||
expect(['foo' => 'bar', 'baz' => 'boom'])
|
||||
@ -86,3 +88,26 @@ test('it can be passed a mixture of value types', function () {
|
||||
|
||||
expect(static::getCount())->toBe(4);
|
||||
});
|
||||
|
||||
test('it works with traversables', function () {
|
||||
$generator = (function () {
|
||||
yield 'one' => (fn () => yield from [1, 2, 3])();
|
||||
yield 'two' => (fn () => yield from [4, 5, 6])();
|
||||
yield 'three' => (fn () => yield from [7, 8, 9])();
|
||||
})();
|
||||
|
||||
expect($generator)->sequence(
|
||||
fn ($value, $key) => $key->toBe('one')
|
||||
->and($value)
|
||||
->toBeInstanceOf(Generator::class)
|
||||
->sequence(1, 2, 3),
|
||||
fn ($value, $key) => $key->toBe('two')
|
||||
->and($value)
|
||||
->toBeInstanceOf(Generator::class)
|
||||
->sequence(4, 5, 6),
|
||||
fn ($value, $key) => $key->toBe('three')
|
||||
->and($value)
|
||||
->toBeInstanceOf(Generator::class)
|
||||
->sequence(7, 8, 9),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user