Completed
Push — master ( cd2604...399c1f )
by Matthieu
03:36
created

AbstractTestClass::assertDeepCopyOf()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 4.909
cc 9
eloc 25
nc 10
nop 2
1
<?php
2
3
namespace DeepCopyTest;
4
5
/**
6
 * Abstract test class
7
 */
8
abstract class AbstractTestClass extends \PHPUnit_Framework_TestCase
9
{
10
    protected function assertDeepCopyOf($expected, $actual)
11
    {
12
        if (is_null($expected)) {
13
            $this->assertNull($actual);
14
            return;
15
        }
16
17
        $this->assertInternalType(gettype($expected), $actual);
18
19
        if (is_array($expected)) {
20
            $this->assertInternalType('array', $actual);
21
            $this->assertCount(count($expected), $actual);
22
            foreach ($actual as $i => $item) {
23
                $this->assertDeepCopyOf($expected[$i], $item);
24
            }
25
            return;
26
        }
27
28
        if (!is_object($expected)) {
29
            $this->assertSame($expected, $actual);
30
            return;
31
        }
32
33
        $this->assertNotSame($expected, $actual);
34
        $this->assertInstanceOf(get_class($expected), $actual);
35
36
        $expectedProperties = (array) $expected;
37
        $actualProperties = (array) $actual;
38
39
        $this->assertSame(array_keys($expectedProperties), array_keys($actualProperties));
40
        foreach ($expectedProperties as $name => $value) {
41
            $this->assertDeepCopyOf($value, $actualProperties[$name]);
42
        }
43
44
        if ($expected instanceof \SplDoublyLinkedList) {
45
            /** @var \SplDoublyLinkedList $actual */
46
            $this->assertSame($expected->count(), $actual->count());
47
48
            while (!$expected->isEmpty() && !$actual->isEmpty()) {
49
                $this->assertDeepCopyOf($expected->pop(), $actual->pop());
50
            }
51
        }
52
    }
53
}
54