Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / ArraySubset.php
1 <?php
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
11 /**
12  * Constraint that asserts that the array it is evaluated for has a specified subset.
13  *
14  * Uses array_replace_recursive() to check if a key value subset is part of the
15  * subject array.
16  *
17  * @since Class available since Release 4.4.0
18  */
19 class PHPUnit_Framework_Constraint_ArraySubset extends PHPUnit_Framework_Constraint
20 {
21     /**
22      * @var array|ArrayAccess
23      */
24     protected $subset;
25
26     /**
27      * @var bool
28      */
29     protected $strict;
30
31     /**
32      * @param array|ArrayAccess $subset
33      * @param bool              $strict Check for object identity
34      */
35     public function __construct($subset, $strict = false)
36     {
37         parent::__construct();
38         $this->strict = $strict;
39         $this->subset = $subset;
40     }
41
42     /**
43      * Evaluates the constraint for parameter $other. Returns true if the
44      * constraint is met, false otherwise.
45      *
46      * @param array|ArrayAccess $other Array or ArrayAccess object to evaluate.
47      *
48      * @return bool
49      */
50     protected function matches($other)
51     {
52         //type cast $other & $this->subset as an array to allow 
53         //support in standard array functions.
54         if($other instanceof ArrayAccess) {
55             $other = (array) $other;
56         }
57
58         if($this->subset instanceof ArrayAccess) {
59             $this->subset = (array) $this->subset;
60         }
61
62         $patched = array_replace_recursive($other, $this->subset);
63
64         if ($this->strict) {
65             return $other === $patched;
66         } else {
67             return $other == $patched;
68         }
69     }
70
71     /**
72      * Returns a string representation of the constraint.
73      *
74      * @return string
75      */
76     public function toString()
77     {
78         return 'has the subset ' . $this->exporter->export($this->subset);
79     }
80
81     /**
82      * Returns the description of the failure
83      *
84      * The beginning of failure messages is "Failed asserting that" in most
85      * cases. This method should return the second part of that sentence.
86      *
87      * @param mixed $other Evaluated value or object.
88      *
89      * @return string
90      */
91     protected function failureDescription($other)
92     {
93         return 'an array ' . $this->toString();
94     }
95 }