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 */
17class PHPUnit_Framework_Constraint_ArraySubset extends PHPUnit_Framework_Constraint
18{
19    /**
20     * @var array|Traversable
21     */
22    protected $subset;
23
24    /**
25     * @var bool
26     */
27    protected $strict;
28
29    /**
30     * @param array|Traversable $subset
31     * @param bool              $strict Check for object identity
32     */
33    public function __construct($subset, $strict = false)
34    {
35        parent::__construct();
36        $this->strict = $strict;
37        $this->subset = $subset;
38    }
39
40    /**
41     * Evaluates the constraint for parameter $other. Returns true if the
42     * constraint is met, false otherwise.
43     *
44     * @param array|Traversable $other Array or Traversable object to evaluate.
45     *
46     * @return bool
47     */
48    protected function matches($other)
49    {
50        //type cast $other & $this->subset as an array to allow
51        //support in standard array functions.
52        $other        = $this->toArray($other);
53        $this->subset = $this->toArray($this->subset);
54
55        $patched = array_replace_recursive($other, $this->subset);
56
57        if ($this->strict) {
58            return $other === $patched;
59        } else {
60            return $other == $patched;
61        }
62    }
63
64    /**
65     * Returns a string representation of the constraint.
66     *
67     * @return string
68     */
69    public function toString()
70    {
71        return 'has the subset ' . $this->exporter->export($this->subset);
72    }
73
74    /**
75     * Returns the description of the failure
76     *
77     * The beginning of failure messages is "Failed asserting that" in most
78     * cases. This method should return the second part of that sentence.
79     *
80     * @param mixed $other Evaluated value or object.
81     *
82     * @return string
83     */
84    protected function failureDescription($other)
85    {
86        return 'an array ' . $this->toString();
87    }
88
89    /**
90     * @param array|Traversable $other
91     *
92     * @return array
93     */
94    private function toArray($other)
95    {
96        if (is_array($other)) {
97            return $other;
98        } elseif ($other instanceof ArrayObject) {
99            return $other->getArrayCopy();
100        } elseif ($other instanceof Traversable) {
101            return iterator_to_array($other);
102        }
103
104        // Keep BC even if we know that array would not be the expected one
105        return (array) $other;
106    }
107}
108