|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Alezhu\PHPUnitArrayContainsAsserts\Constraint; |
| 4 | + |
| 5 | +use PHPUnit\Framework\AssertionFailedError; |
| 6 | +use PHPUnit\Framework\Constraint\Constraint; |
| 7 | +use PHPUnit\Framework\Constraint\IsEqual; |
| 8 | +use PHPUnit\Framework\Constraint\IsIdentical; |
| 9 | +use SebastianBergmann\Comparator\ComparisonFailure; |
| 10 | + |
| 11 | + |
| 12 | +class ArrayContains extends ArrayContainsBase |
| 13 | +{ |
| 14 | + protected $other_array; |
| 15 | + |
| 16 | + public function toString(): string |
| 17 | + { |
| 18 | + return 'is contains expected values or key-value pairs'; |
| 19 | + } |
| 20 | + |
| 21 | + protected function _matchAssoc($other): bool |
| 22 | + { |
| 23 | + foreach ($this->subset as $key => $value) { |
| 24 | + if (!array_key_exists($key, $this->other_array)) { |
| 25 | + $failure = new ComparisonFailure($this->subset, $other, var_export($this->subset, true), var_export($other, true)); |
| 26 | + $this->fail($other, "Actual data not contains some expected keys: ($key)", $failure); |
| 27 | + } |
| 28 | + |
| 29 | + $actual = $this->other_array[$key]; |
| 30 | + $constraint = $value instanceof Constraint ? $value : ($this->strict ? new IsIdentical($value) : new IsEqual($value)); |
| 31 | + $valid = $constraint->evaluate($actual, '', true); |
| 32 | + if (!$valid) { |
| 33 | + $failure = new ComparisonFailure($this->subset, $other, var_export($this->subset, true), var_export($other, true)); |
| 34 | + $this->fail($other, "Actual data contains unexpected values: ($key)", $failure); |
| 35 | + } |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + protected function _matchUnAssoc($other): bool |
| 41 | + { |
| 42 | + $flip_fail = false; |
| 43 | + set_error_handler(function () use (&$flip_fail) { |
| 44 | + $flip_fail = true; |
| 45 | + }, E_WARNING); |
| 46 | + $assoc_array = array_flip($this->other_array); |
| 47 | + restore_error_handler(); |
| 48 | + |
| 49 | + foreach ($this->subset as $value) { |
| 50 | + if ( |
| 51 | + (!$flip_fail && !array_key_exists($value, $assoc_array)) |
| 52 | + || |
| 53 | + ($flip_fail && !in_array($value, $this->other_array, $this->strict)) |
| 54 | + ) { |
| 55 | + $failure = new ComparisonFailure($this->subset, $other, var_export($this->subset, true), var_export($other, true)); |
| 56 | + $this->fail($other, "Actual data not contains some values: ($value)", $failure); |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + protected function matches($other): bool |
| 63 | + { |
| 64 | + if ($this->_isAssocArray($other)) { |
| 65 | + if (!$this->_isAssocArray($this->subset)) { |
| 66 | + throw new AssertionFailedError('Actual data is an associative array, but expected data is not'); |
| 67 | + } |
| 68 | + $this->other_array = $this->_toArray($other); |
| 69 | + return $this->_matchAssoc($other); |
| 70 | + } else { |
| 71 | + if ($this->_isAssocArray($this->subset)) { |
| 72 | + throw new AssertionFailedError('Actual data is not an associative array, but expected data is that'); |
| 73 | + } |
| 74 | + $this->other_array = $this->_toArray($other); |
| 75 | + return $this->_matchUnAssoc($other); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments