1
|
|
|
<?php |
2
|
|
|
namespace SubjectivePHPTest\Spl\DataStructures; |
3
|
|
|
|
4
|
|
|
use SubjectivePHP\Spl\DataStructures\ImmutableArrayObject; |
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Unit tests for the \SubjectivePHP\Spl\DataStructures\ImmutableArrayObject class. |
9
|
|
|
* |
10
|
|
|
* @coversDefaultClass \SubjectivePHP\Spl\DataStructures\ImmutableArrayObject |
11
|
|
|
*/ |
12
|
|
|
final class ImmutableArrayObjectTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Verify basic behavior of createFromMutable(). |
16
|
|
|
* |
17
|
|
|
* @test |
18
|
|
|
* @covers ::createFromMutable |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function createFromMutable() |
23
|
|
|
{ |
24
|
|
|
$data = ['a', 'b', 'c']; |
25
|
|
|
$mutable = new \ArrayObject($data); |
26
|
|
|
$immutable = ImmutableArrayObject::createFromMutable($mutable); |
27
|
|
|
|
28
|
|
|
$this->assertInstanceOf('\SubjectivePHP\Spl\DataStructures\ImmutableArrayObject', $immutable); |
29
|
|
|
$this->assertSame($mutable->getArrayCopy(), $immutable->getArrayCopy()); |
30
|
|
|
$this->assertSame($mutable->getFlags(), $immutable->getFlags()); |
31
|
|
|
$this->assertSame($mutable->getIteratorClass(), $immutable->getIteratorClass()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Verify behavior of offsetSet(). |
36
|
|
|
* |
37
|
|
|
* @test |
38
|
|
|
* @covers ::offsetSet |
39
|
|
|
* @expectedException \LogicException |
40
|
|
|
* @expectedExceptionMessage Attempting to write to an immutable array |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function offsetSet() |
45
|
|
|
{ |
46
|
|
|
$immutable = new ImmutableArrayObject(['a', 'b', 'c']); |
47
|
|
|
$immutable[3] = 'd'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Verify behavior of offsetUnset(). |
52
|
|
|
* |
53
|
|
|
* @test |
54
|
|
|
* @covers ::offsetUnset |
55
|
|
|
* @expectedException \LogicException |
56
|
|
|
* @expectedExceptionMessage Attempting to write to an immutable array |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function offsetUnset() |
61
|
|
|
{ |
62
|
|
|
$immutable = new ImmutableArrayObject(['a', 'b', 'c']); |
63
|
|
|
unset($immutable[3]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Verify behavior of append(). |
68
|
|
|
* |
69
|
|
|
* @test |
70
|
|
|
* @covers ::append |
71
|
|
|
* @expectedException \LogicException |
72
|
|
|
* @expectedExceptionMessage Attempting to write to an immutable array |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function append() |
77
|
|
|
{ |
78
|
|
|
$immutable = new ImmutableArrayObject(['a', 'b', 'c']); |
79
|
|
|
$immutable->append('d'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Verify behavior of exchangeArray(). |
84
|
|
|
* |
85
|
|
|
* @test |
86
|
|
|
* @covers ::exchangeArray |
87
|
|
|
* @expectedException \LogicException |
88
|
|
|
* @expectedExceptionMessage Attempting to write to an immutable array |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function exchangeArray() |
93
|
|
|
{ |
94
|
|
|
$immutable = new ImmutableArrayObject(['a', 'b', 'c']); |
95
|
|
|
$immutable->exchangeArray(['x', 'y', 'z']); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|