Failed Conditions
Branch ignore-unknown-keywords (efa676)
by Stéphane
02:58
created

UtilsTest::equalDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal;
11
12
class UtilsTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @dataProvider equalDataProvider
16
     *
17
     * @param mixed $a
18
     * @param mixed $b
19
     */
20
    public function testAreEqualWithEqualData($a, $b)
21
    {
22
        $this->assertTrue(Utils::areEqual($a, $b));
23
    }
24
25
    /**
26
     * @dataProvider notEqualDataProvider
27
     *
28
     * @param mixed $a
29
     * @param mixed $b
30
     */
31
    public function testAreEqualWithNotEqualData($a, $b)
32
    {
33
        $this->assertFalse(Utils::areEqual($a, $b));
34
    }
35
36
    public function testAreEqualWithRecursiveReference()
37
    {
38
        $a = new \stdClass();
39
        $a->foo = [1, 2, 3];
40
        $a->bar = new \stdClass();
41
        $a->bar->baz = $a;
42
43
        $b = new \stdClass();
44
        $b->foo = [1, 2, 3];
45
        $b->bar = new \stdClass();
46
        $b->bar->baz = $b;
47
48
        $this->assertTrue(Utils::areEqual($a, $b));
49
    }
50
51
    /**
52
     * @expectedException \RuntimeException
53
     */
54
    public function testLoadJsonFromFileThrowsIfFileDoesNotExist()
55
    {
56
        Utils::loadJsonFromFile('/does/not/exist');
57
    }
58
59
    /**
60
     * @expectedException \JVal\Exception\JsonDecodeException
61
     */
62
    public function testLoadJsonFromFileThrowsOnJsonDecodeError()
63
    {
64
        Utils::loadJsonFromFile(__DIR__.'/Data/schemas/invalid/undecodable.json');
65
    }
66
67
    public function equalDataProvider()
68
    {
69
        $a = new \stdClass();
70
        $b = new \stdClass();
71
        $a->foo = [1, 2, 3];
72
        $a->bar = new \stdClass();
73
        $b->foo = [1, 2, 3];
74
        $b->bar = new \stdClass();
75
76
        return [
77
            [1, 1],
78
            ['foo', 'foo'],
79
            [['foo', 'bar', []], ['foo', 'bar', []]],
80
            [new \stdClass(), new \stdClass()],
81
            [$a, $b],
82
        ];
83
    }
84
85
    public function notEqualDataProvider()
86
    {
87
        $a = new \stdClass();
88
        $b = new \stdClass();
89
        $a->foo = [1, 2, 3];
90
        $a->bar = new \stdClass();
91
        $a->bar->baz = [1];
92
        $b->foo = [1, 2, 3];
93
        $b->bar = new \stdClass();
94
        $b->bar->baz = [1, []];
95
96
        return [
97
            [1, -1],
98
            ['foo', 'bar'],
99
            [['foo', ['bar', 'baz']], ['foo', ['bar', 'quz']]],
100
            [['foo', ['a' => 'bar', 'baz']], ['foo', ['bar', 'quz']]],
101
            [$a, $b],
102
        ];
103
    }
104
}
105