forked from api-platform/core
/
ArraySubsetTrait.php
93 lines (82 loc) · 2.43 KB
/
ArraySubsetTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Symfony\Bundle\Test\Constraint;
use SebastianBergmann\Comparator\ComparisonFailure;
use SebastianBergmann\Exporter\Exporter;
/**
* Constraint that asserts that the array it is evaluated for has a specified subset.
*
* Uses array_replace_recursive() to check if a key value subset is part of the
* subject array.
*
* Imported from dms/phpunit-arraysubset-asserts, because the original constraint has been deprecated.
*
* @copyright Sebastian Bergmann <sebastian@phpunit.de>
* @copyright Rafael Dohms <rdohms@gmail.com>
*
* @see https://github.com/sebastianbergmann/phpunit/issues/3494
*/
trait ArraySubsetTrait
{
public function __construct(private iterable $subset, private readonly bool $strict = false)
{
}
private function _evaluate($other, string $description = '', bool $returnResult = false): ?bool
{
// type cast $other & $this->subset as an array to allow
// support in standard array functions.
$other = $this->toArray($other);
$this->subset = $this->toArray($this->subset);
$patched = array_replace_recursive($other, $this->subset);
if ($this->strict) {
$result = $other === $patched;
} else {
$result = $other == $patched;
}
if ($returnResult) {
return $result;
}
if ($result) {
return null;
}
$f = new ComparisonFailure(
$patched,
$other,
var_export($patched, true),
var_export($other, true)
);
$this->fail($other, $description, $f);
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return 'has the subset '.(new Exporter())->export($this->subset);
}
/**
* {@inheritdoc}
*/
protected function failureDescription($other): string
{
return 'an array '.$this->toString();
}
private function toArray(iterable $other): array
{
if (\is_array($other)) {
return $other;
}
if ($other instanceof \ArrayObject) {
return $other->getArrayCopy();
}
return iterator_to_array($other);
}
}