forked from nelmio/alice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeepCloneTest.php
128 lines (99 loc) · 2.77 KB
/
DeepCloneTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Nelmio\Alice;
use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use stdClass;
/**
* @covers ::\Nelmio\Alice\deep_clone
*/
class DeepCloneTest extends TestCase
{
/**
* @dataProvider provideScalarValues
*/
public function testDeepCloneScalarsReturnsScalar($value): void
{
$clone = deep_clone($value);
static::assertEquals($value, $clone);
}
public function testDeepCloneObjects(): void
{
$foo = new stdClass();
$bar = new stdClass();
$foo->name = 'foo';
$foo->bar = $bar;
$foo->date = new DateTime();
$bar->name = 'bar';
$bar->foo = $foo;
$bar->date = new DateTimeImmutable();
$fooClone = deep_clone($foo);
$this->assertEqualsButNotSame($foo, $fooClone);
$this->assertEqualsButNotSame($bar, $fooClone->bar);
$barClone = deep_clone($bar);
$this->assertEqualsButNotSame($bar, $barClone);
$this->assertEqualsButNotSame($foo, $barClone->foo);
}
public function testDeepCloneArrays(): void
{
$foo = new stdClass();
$bar = new stdClass();
$arr1 = [$foo];
$arr2 = [$bar];
$foo->name = 'foo';
$foo->bar = $bar;
$bar->name = 'bar';
$bar->foo = $foo;
$fooClone = deep_clone($arr1)[0];
$this->assertEqualsButNotSame($foo, $fooClone);
$this->assertEqualsButNotSame($bar, $fooClone->bar);
$barClone = deep_clone($arr2)[0];
$this->assertEqualsButNotSame($bar, $barClone);
$this->assertEqualsButNotSame($foo, $barClone->foo);
}
public function testDeepCloneClosure(): void
{
$foo = new stdClass();
$bar = new stdClass();
$c1 = static function () use ($foo) {
return $foo;
};
$foo->name = 'foo';
$foo->bar = $bar;
$bar->name = 'bar';
$bar->foo = $foo;
$fooClone = deep_clone($c1)();
static::assertSame($foo, $fooClone);
static::assertSame($bar, $fooClone->bar);
}
public function provideScalarValues()
{
return [
[null],
['null'],
[0],
[1],
[-1],
[0.5],
[-0.5],
['string'],
[''],
[true],
[false],
];
}
private function assertEqualsButNotSame($expected, $value): void
{
static::assertEquals($expected, $value);
static::assertNotSame($expected, $value);
}
}