| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of PHPUnit. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (c) Sebastian Bergmann <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 SilverStripe\Dev\Constraint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PHPUnit\Framework\Constraint\Constraint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use PHPUnit\Framework\ExpectationFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SebastianBergmann\Comparator\ComparisonFailure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | if (!class_exists(Constraint::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Constraint that asserts that the array it is evaluated for has a specified subset. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Uses array_replace_recursive() to check if a key value subset is part of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * subject array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | final class ArraySubset extends Constraint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $subset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $strict; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function __construct(iterable $subset, bool $strict = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->strict = $strict; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->subset = $subset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Evaluates the constraint for parameter $other | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * If $returnResult is set to false (the default), an exception is thrown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * in case of a failure. null is returned otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * If $returnResult is true, the result of the evaluation is returned as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * a boolean value instead: true in case of success, false in case of a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @throws ExpectationFailedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function evaluate($other, string $description = '', bool $returnResult = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         //type cast $other & $this->subset as an array to allow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         //support in standard array functions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $other        = $this->toArray($other); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->subset = $this->toArray($this->subset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $patched = \array_replace_recursive($other, $this->subset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($this->strict) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $result = $other === $patched; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $result = $other == $patched; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($returnResult) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if (!$result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $f = new ComparisonFailure( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $patched, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $other, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 \var_export($patched, true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 \var_export($other, true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $this->fail($other, $description, $f); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Returns a string representation of the constraint. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function toString(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return 'has the subset ' . $this->exporter()->export($this->subset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Returns the description of the failure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * The beginning of failure messages is "Failed asserting that" in most | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * cases. This method should return the second part of that sentence. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param mixed $other evaluated value or object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     protected function failureDescription($other): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return 'an array ' . $this->toString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     private function toArray(iterable $other): array | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         if (\is_array($other)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |             return $other; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         if ($other instanceof \ArrayObject) { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |             return $other->getArrayCopy(); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         if ($other instanceof \Traversable) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             return \iterator_to_array($other); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         // Keep BC even if we know that array would not be the expected one | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         return (array) $other; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  |