|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of PHPUnit. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <sebastian@phpunit.de> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace Ekyna\Component\Payum\Payzen\Tests\Constraint; |
| 11 | + |
| 12 | +use ArrayAccess; |
| 13 | +use PHPUnit\Framework\Constraint\Constraint; |
| 14 | +use SebastianBergmann\RecursionContext\InvalidArgumentException; |
| 15 | +use function array_key_exists; |
| 16 | +use function is_array; |
| 17 | + |
| 18 | +/** |
| 19 | + * Constraint that asserts that the array it is evaluated for has a given key and match a constraint type. |
| 20 | + * |
| 21 | + * Uses array_key_exists() to check if the key is found in the input array, if not found the evaluation fails. |
| 22 | + * |
| 23 | + * The array key and the constraint type are passed in the constructor. |
| 24 | + */ |
| 25 | +final class ArrayItem extends Constraint |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + private $key; |
| 31 | + /** |
| 32 | + * @var Constraint |
| 33 | + */ |
| 34 | + private $constraint; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param string $key |
| 38 | + * @param Constraint $constraint |
| 39 | + */ |
| 40 | + public function __construct(string $key, Constraint $constraint) |
| 41 | + { |
| 42 | + $this->key = $key; |
| 43 | + $this->constraint = $constraint; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns a string representation of the constraint. |
| 48 | + * |
| 49 | + * @throws InvalidArgumentException |
| 50 | + */ |
| 51 | + public function toString(): string |
| 52 | + { |
| 53 | + return 'has the key ' . $this->exporter()->export($this->key) . $this->constraint->toString(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Evaluates the constraint for parameter $other. Returns true if the |
| 58 | + * constraint is met, false otherwise. |
| 59 | + * |
| 60 | + * @param mixed $other value or object to evaluate |
| 61 | + */ |
| 62 | + protected function matches($other): bool |
| 63 | + { |
| 64 | + if (is_array($other)) { |
| 65 | + if (!array_key_exists($this->key, $other)) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + return $this->constraint->matches($other[$this->key]); |
| 70 | + } |
| 71 | + |
| 72 | + if ($other instanceof ArrayAccess) { |
| 73 | + if (!$other->offsetExists($this->key)) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + return $this->constraint->matches($other[$this->key]); |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the description of the failure. |
| 85 | + * |
| 86 | + * The beginning of failure messages is "Failed asserting that" in most |
| 87 | + * cases. This method should return the second part of that sentence. |
| 88 | + * |
| 89 | + * @param mixed $other evaluated value or object |
| 90 | + * |
| 91 | + * @throws InvalidArgumentException |
| 92 | + */ |
| 93 | + protected function failureDescription($other): string |
| 94 | + { |
| 95 | + return 'an array ' . $this->toString(); |
| 96 | + } |
| 97 | +} |
0 commit comments