1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) 2018 Dennis Birkholz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* $Id$ |
7
|
|
|
* Author: $Format:%an <%ae>, %ai$ |
8
|
|
|
* Committer: $Format:%cn <%ce>, %ci$ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace morgue\archive; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @coversDefaultClass \morgue\archive\Archive |
17
|
|
|
*/ |
18
|
|
|
final class ArchiveTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testImmutability() |
21
|
|
|
{ |
22
|
|
|
$old = new Archive(); |
23
|
|
|
|
24
|
|
|
$value = 'new comment'; |
25
|
|
|
$this->assertNotSame($value, $old->getComment()); |
26
|
|
|
$new = $old->withComment($value); |
27
|
|
|
$this->assertNotSame($old, $new); |
28
|
|
|
$this->assertNotSame($old->getComment(), $new->getComment()); |
29
|
|
|
$this->assertSame($value, $new->getComment()); |
30
|
|
|
$old = $new; |
31
|
|
|
|
32
|
|
|
$this->assertEmpty($old->getEntries()); |
33
|
|
|
|
34
|
|
|
$entry = new ArchiveEntry('entry1'); |
35
|
|
|
$this->assertNotContains($entry, $old->getEntries()); |
36
|
|
|
$new = $old->addEntry($entry); |
37
|
|
|
$this->assertNotSame($old, $new); |
38
|
|
|
$this->assertNotContains($entry, $old->getEntries()); |
39
|
|
|
$this->assertContains($entry, $new->getEntries()); |
40
|
|
|
$this->assertSame($entry, $new->getEntry(0)); |
41
|
|
|
$this->assertSame($entry, $new->getEntry('entry1')); |
42
|
|
|
$old = $new; |
43
|
|
|
|
44
|
|
|
$new = $old |
45
|
|
|
->addEntry(new ArchiveEntry('entry2')) |
46
|
|
|
->addEntry($entry3 = new ArchiveEntry('entry3')) |
47
|
|
|
->addEntry(new ArchiveEntry('entry4')) |
48
|
|
|
->addEntry(new ArchiveEntry('entry5')) |
49
|
|
|
; |
50
|
|
|
$this->assertNotSame($old, $new); |
51
|
|
|
$this->assertCount(5, $new->getEntries()); |
52
|
|
|
$old = $new; |
53
|
|
|
|
54
|
|
|
$entry = $entry3->withComment('a comment'); |
55
|
|
|
$this->assertContains($entry3, $old->getEntries()); |
56
|
|
|
$this->assertNotContains($entry, $old->getEntries()); |
57
|
|
|
|
58
|
|
|
$new = $old->replaceEntry($entry3, $entry); |
59
|
|
|
$this->assertNotSame($old, $new); |
60
|
|
|
$this->assertNotContains($entry, $old->getEntries()); |
61
|
|
|
$this->assertContains($entry, $new->getEntries()); |
62
|
|
|
$this->assertSame($entry3, $old->getEntry('entry3')); |
63
|
|
|
$this->assertSame($entry, $new->getEntry('entry3')); |
64
|
|
|
$old = $new; |
65
|
|
|
|
66
|
|
|
$new = $old->delEntry($entry); |
67
|
|
|
$this->assertNotSame($old, $new); |
68
|
|
|
$this->assertNotContains($entry, $new->getEntries()); |
69
|
|
|
$this->assertCount(4, $new->getEntries()); |
70
|
|
|
$this->assertNull($new->getEntry(2)); |
71
|
|
|
$this->assertNull($new->getEntry('entry3')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @covers ::addEntry() |
76
|
|
|
*/ |
77
|
|
|
public function testAddEntry() |
78
|
|
|
{ |
79
|
|
|
$archive = new Archive(); |
80
|
|
|
$entry = new ArchiveEntry('entry'); |
81
|
|
|
$archive = $archive->addEntry($entry); |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$archive->addEntry($entry); |
85
|
|
|
$this->fail('Except exception'); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $e); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$archive->addEntry(new ArchiveEntry('entry2'), 0); |
92
|
|
|
$this->fail('Except exception'); |
93
|
|
|
} catch (\Exception $e) { |
94
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $e); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$archive->addEntry(new ArchiveEntry('entry')); |
99
|
|
|
$this->fail('Except exception'); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $e); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @covers ::delEntry() |
107
|
|
|
*/ |
108
|
|
|
public function testDelEntry() |
109
|
|
|
{ |
110
|
|
|
$archive = new Archive(); |
111
|
|
|
$entry = new ArchiveEntry('entry'); |
112
|
|
|
|
113
|
|
|
$archive = $archive->addEntry($entry); |
114
|
|
|
$this->assertCount(1, $archive->getEntries()); |
115
|
|
|
|
116
|
|
|
$this->assertCount(0, $archive->delEntry(0)->getEntries()); |
117
|
|
|
$this->assertCount(0, $archive->delEntry('entry')->getEntries()); |
118
|
|
|
$this->assertCount(0, $archive->delEntry($entry)->getEntries()); |
119
|
|
|
|
120
|
|
|
try { |
121
|
|
|
$archive->delEntry(1); |
122
|
|
|
$this->fail('Except exception'); |
123
|
|
|
} catch (\Exception $e) { |
124
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $e); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
try { |
128
|
|
|
$archive->delEntry('entry2'); |
129
|
|
|
$this->fail('Except exception'); |
130
|
|
|
} catch (\Exception $e) { |
131
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $e); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
try { |
135
|
|
|
$archive->delEntry(new ArchiveEntry('entry2')); |
136
|
|
|
$this->fail('Except exception'); |
137
|
|
|
} catch (\Exception $e) { |
138
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $e); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
try { |
142
|
|
|
$archive->delEntry(new \SplObjectStorage()); |
|
|
|
|
143
|
|
|
$this->fail('Except exception'); |
144
|
|
|
} catch (\Exception $e) { |
145
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $e); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @covers ::getEntry() |
151
|
|
|
* @expectedException \InvalidArgumentException |
152
|
|
|
*/ |
153
|
|
|
public function testGetEntry() |
154
|
|
|
{ |
155
|
|
|
$archive = new Archive(); |
156
|
|
|
$archive->getEntry(new \SplObjectStorage()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @covers ::replaceEntry() |
161
|
|
|
* @expectedException \InvalidArgumentException |
162
|
|
|
*/ |
163
|
|
|
public function testReplaceEntry() |
164
|
|
|
{ |
165
|
|
|
$archive = new Archive(); |
166
|
|
|
$archive->replaceEntry(new ArchiveEntry('entry1'), new ArchiveEntry('entry2')); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: