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
11class PHPUnit_Framework_Constraint_ArraySubsetTest extends PHPUnit_Framework_TestCase
12{
13    /**
14     * @param bool              $expected
15     * @param array|Traversable $subset
16     * @param array|Traversable $other
17     * @param bool              $strict
18     * @dataProvider evaluateDataProvider
19     */
20    public function testEvaluate($expected, $subset, $other, $strict)
21    {
22        $constraint = new PHPUnit_Framework_Constraint_ArraySubset($subset, $strict);
23
24        $this->assertSame($expected, $constraint->evaluate($other, '', true));
25    }
26
27    public static function evaluateDataProvider()
28    {
29        return [
30            'loose array subset and array other' => [
31                'expected' => true,
32                'subset'   => ['bar' => 0],
33                'other'    => ['foo' => '', 'bar' => '0'],
34                'strict'   => false
35            ],
36            'strict array subset and array other' => [
37                'expected' => false,
38                'subset'   => ['bar' => 0],
39                'other'    => ['foo' => '', 'bar' => '0'],
40                'strict'   => true
41            ],
42            'loose array subset and ArrayObject other' => [
43                'expected' => true,
44                'subset'   => ['bar' => 0],
45                'other'    => new ArrayObject(['foo' => '', 'bar' => '0']),
46                'strict'   => false
47            ],
48            'strict ArrayObject subset and array other' => [
49                'expected' => true,
50                'subset'   => new ArrayObject(['bar' => 0]),
51                'other'    => ['foo' => '', 'bar' => 0],
52                'strict'   => true
53            ],
54        ];
55    }
56
57    public function testEvaluateWithArrayAccess()
58    {
59        $arrayAccess = new ArrayAccessible(['foo' => 'bar']);
60
61        $constraint = new PHPUnit_Framework_Constraint_ArraySubset(['foo' => 'bar']);
62
63        $this->assertTrue($constraint->evaluate($arrayAccess, '', true));
64    }
65}
66