mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 10:47:25 +01:00
Merge pull request #924 from hungthai1401/to_have_same_size_expectation
[2.x] Add `toHaveSameSize` expectation
This commit is contained in:
@ -272,6 +272,23 @@ final class Expectation
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the size of the value and $expected are the same.
|
||||||
|
*
|
||||||
|
* @param array<int|string, mixed> $expected
|
||||||
|
* @return self<TValue>
|
||||||
|
*/
|
||||||
|
public function toHaveSameSize(iterable $expected, string $message = ''): self
|
||||||
|
{
|
||||||
|
if (! is_countable($this->value) && ! is_iterable($this->value)) {
|
||||||
|
InvalidExpectationValue::expected('countable|iterable');
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert::assertSameSize($expected, $this->value, $message);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that the value contains the property $name.
|
* Asserts that the value contains the property $name.
|
||||||
*
|
*
|
||||||
|
|||||||
24
tests/Features/Expect/toHaveSameSize.php
Normal file
24
tests/Features/Expect/toHaveSameSize.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pest\Exceptions\InvalidExpectationValue;
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('failures with wrong type', function () {
|
||||||
|
expect('foo')->toHaveSameSize([1]);
|
||||||
|
})->throws(InvalidExpectationValue::class, 'Invalid expectation value type. Expected [countable|iterable].');
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect([1, 2, 3])->toHaveSameSize([4, 5, 6]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect([1, 2, 3])->toHaveSameSize([1]);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('failures with message', function () {
|
||||||
|
expect([1, 2, 3])->toHaveSameSize([1], 'oh no!');
|
||||||
|
})->throws(ExpectationFailedException::class, 'oh no!');
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect([1, 2, 3])->not->toHaveSameSize([1]);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user