• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Symfony/Bundle/Test/Constraint/ArraySubsetTrait.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Symfony\Bundle\Test\Constraint;
15

16
use SebastianBergmann\Comparator\ComparisonFailure;
17
use SebastianBergmann\Exporter\Exporter;
18

19
/**
20
 * Constraint that asserts that the array it is evaluated for has a specified subset.
21
 *
22
 * Uses array_replace_recursive() to check if a key value subset is part of the
23
 * subject array.
24
 *
25
 * Imported from dms/phpunit-arraysubset-asserts, because the original constraint has been deprecated.
26
 *
27
 * @copyright Sebastian Bergmann <sebastian@phpunit.de>
28
 * @copyright Rafael Dohms <rdohms@gmail.com>
29
 *
30
 * @see https://github.com/sebastianbergmann/phpunit/issues/3494
31
 */
32
trait ArraySubsetTrait
33
{
34
    public function __construct(private iterable $subset, private readonly bool $strict = false)
35
    {
36
    }
88✔
37

38
    private function _evaluate($other, string $description = '', bool $returnResult = false): ?bool
39
    {
40
        // type cast $other & $this->subset as an array to allow
41
        // support in standard array functions.
42
        $other = $this->toArray($other);
88✔
43
        $this->subset = $this->toArray($this->subset);
88✔
44
        $patched = array_replace_recursive($other, $this->subset);
88✔
45
        if ($this->strict) {
88✔
46
            $result = $other === $patched;
34✔
47
        } else {
48
            $result = $other == $patched;
54✔
49
        }
50
        if ($returnResult) {
88✔
51
            return $result;
10✔
52
        }
53
        if ($result) {
78✔
54
            return null;
74✔
55
        }
56

57
        $f = new ComparisonFailure(
4✔
58
            $patched,
4✔
59
            $other,
4✔
60
            var_export($patched, true),
4✔
61
            var_export($other, true)
4✔
62
        );
4✔
63
        $this->fail($other, $description, $f);
4✔
64
    }
65

66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function toString(): string
70
    {
71
        return 'has the subset '.(new Exporter())->export($this->subset);
4✔
72
    }
73

74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function failureDescription($other): string
78
    {
79
        return 'an array '.$this->toString();
4✔
80
    }
81

82
    private function toArray(iterable $other): array
83
    {
84
        if (\is_array($other)) {
88✔
85
            return $other;
88✔
86
        }
87
        if ($other instanceof \ArrayObject) {
6✔
88
            return $other->getArrayCopy();
4✔
89
        }
90

91
        return iterator_to_array($other);
2✔
92
    }
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc