mirror of
https://github.com/pestphp/pest.git
synced 2026-03-11 10:17:23 +01:00
feat: adds toMatchArray
This commit is contained in:
@ -408,10 +408,17 @@ final class Expectation
|
|||||||
* Asserts that the value array has the provided $key.
|
* Asserts that the value array has the provided $key.
|
||||||
*
|
*
|
||||||
* @param string|int $key
|
* @param string|int $key
|
||||||
|
* @param mixed $value
|
||||||
*/
|
*/
|
||||||
public function toHaveKey($key): Expectation
|
public function toHaveKey($key, $value = null): Expectation
|
||||||
{
|
{
|
||||||
Assert::assertArrayHasKey($key, $this->value);
|
$array = (array) $this->value;
|
||||||
|
|
||||||
|
Assert::assertArrayHasKey($key, $array);
|
||||||
|
|
||||||
|
if (func_num_args() > 1) {
|
||||||
|
Assert::assertEquals($value, $array[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -490,6 +497,20 @@ final class Expectation
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the value array matches the given array subset.
|
||||||
|
*
|
||||||
|
* @param array<int|string, mixed> $array
|
||||||
|
*/
|
||||||
|
public function toMatchArray($array): Expectation
|
||||||
|
{
|
||||||
|
foreach ($array as $property => $value) {
|
||||||
|
$this->toHaveKey($property, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that the value object matches a subset
|
* Asserts that the value object matches a subset
|
||||||
* of the properties of an given object.
|
* of the properties of an given object.
|
||||||
|
|||||||
@ -200,6 +200,11 @@
|
|||||||
PASS Tests\Expect\toMatch
|
PASS Tests\Expect\toMatch
|
||||||
✓ pass
|
✓ pass
|
||||||
✓ failures
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toMatchArray
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
✓ not failures
|
✓ not failures
|
||||||
|
|
||||||
PASS Tests\Expect\toMatchConstraint
|
PASS Tests\Expect\toMatchConstraint
|
||||||
@ -399,5 +404,5 @@
|
|||||||
✓ depends run test only once
|
✓ depends run test only once
|
||||||
✓ depends works with the correct test name
|
✓ depends works with the correct test name
|
||||||
|
|
||||||
Tests: 7 skipped, 235 passed
|
Tests: 7 skipped, 238 passed
|
||||||
|
|
||||||
31
tests/Expect/toMatchArray.php
Normal file
31
tests/Expect/toMatchArray.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->user = [
|
||||||
|
'id' => 1,
|
||||||
|
'name' => 'Nuno',
|
||||||
|
'email' => 'enunomaduro@gmail.com',
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect($this->user)->toMatchArray([
|
||||||
|
'name' => 'Nuno',
|
||||||
|
'email' => 'enunomaduro@gmail.com',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect($this->user)->toMatchArray([
|
||||||
|
'name' => 'Not the same name',
|
||||||
|
'email' => 'enunomaduro@gmail.com',
|
||||||
|
]);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect($this->user)->not->toMatchArray([
|
||||||
|
'id' => 1,
|
||||||
|
]);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
Reference in New Issue
Block a user