|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
/** |
|
3
|
|
|
* Tests for the changelog ChangelogEntry class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-changelogger |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Changelog\Tests; |
|
9
|
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Changelog\ChangeEntry; |
|
11
|
|
|
use Automattic\Jetpack\Changelog\ChangelogEntry; |
|
12
|
|
|
use DateTime; |
|
13
|
|
|
use InvalidArgumentException; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Tests for the changelog ChangelogEntry class. |
|
18
|
|
|
* |
|
19
|
|
|
* @covers \Automattic\Jetpack\Changelog\ChangelogEntry |
|
20
|
|
|
*/ |
|
21
|
|
|
class ChangelogEntryTest extends TestCase { |
|
22
|
|
|
use \Yoast\PHPUnitPolyfills\Polyfills\ExpectException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Test general getters. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testGetters() { |
|
28
|
|
|
$then = new DateTime( 'now' ); |
|
29
|
|
|
$entry = new ChangelogEntry( '1.0' ); |
|
30
|
|
|
$now = new DateTime( 'now' ); |
|
31
|
|
|
$this->assertSame( '1.0', $entry->getVersion() ); |
|
32
|
|
|
$this->assertGreaterThanOrEqual( $then, $entry->getTimestamp() ); |
|
33
|
|
|
$this->assertLessThanOrEqual( $now, $entry->getTimestamp() ); |
|
34
|
|
|
$this->assertSame( null, $entry->getLink() ); |
|
35
|
|
|
$this->assertSame( '', $entry->getPrologue() ); |
|
36
|
|
|
$this->assertSame( '', $entry->getEpilogue() ); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertSame( $entry, $entry->setVersion( '2.0' )->setPrologue( 'Foo' )->setEpilogue( 'Bar' )->setLink( 'https://example.org' ) ); |
|
39
|
|
|
$this->assertSame( 'https://example.org', $entry->getLink() ); |
|
40
|
|
|
$this->assertSame( '2.0', $entry->getVersion() ); |
|
41
|
|
|
$this->assertSame( 'Foo', $entry->getPrologue() ); |
|
42
|
|
|
$this->assertSame( 'Bar', $entry->getEpilogue() ); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertSame( $entry, $entry->setVersion( 111 )->setPrologue( 222 )->setEpilogue( 333 )->setLink( '' ) ); |
|
45
|
|
|
$this->assertSame( '111', $entry->getVersion() ); |
|
46
|
|
|
$this->assertSame( null, $entry->getLink() ); |
|
47
|
|
|
$this->assertSame( '222', $entry->getPrologue() ); |
|
48
|
|
|
$this->assertSame( '333', $entry->getEpilogue() ); |
|
49
|
|
|
|
|
50
|
|
|
$entry = new ChangelogEntry( |
|
51
|
|
|
'1.0', |
|
52
|
|
|
array( |
|
53
|
|
|
'version' => '2.0', |
|
54
|
|
|
'prologue' => 'XXX', |
|
55
|
|
|
'timestamp' => '2021-02-12', |
|
56
|
|
|
) |
|
57
|
|
|
); |
|
58
|
|
|
$this->assertSame( '1.0', $entry->getVersion() ); |
|
59
|
|
|
$this->assertSame( 'XXX', $entry->getPrologue() ); |
|
60
|
|
|
$this->assertSame( '2021-02-12 00:00:00', $entry->getTimestamp()->format( 'Y-m-d H:i:s' ) ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Test changes. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testChanges() { |
|
67
|
|
|
$entry = new ChangelogEntry( '1.0' ); |
|
68
|
|
|
$changes = array( |
|
69
|
|
|
new ChangeEntry( |
|
70
|
|
|
array( |
|
71
|
|
|
'subheading' => 'A', |
|
72
|
|
|
'content' => '14', |
|
73
|
|
|
) |
|
74
|
|
|
), |
|
75
|
|
|
new ChangeEntry( |
|
76
|
|
|
array( |
|
77
|
|
|
'subheading' => 'B', |
|
78
|
|
|
'content' => '2', |
|
79
|
|
|
) |
|
80
|
|
|
), |
|
81
|
|
|
new ChangeEntry( |
|
82
|
|
|
array( |
|
83
|
|
|
'subheading' => 'B', |
|
84
|
|
|
'content' => '8', |
|
85
|
|
|
) |
|
86
|
|
|
), |
|
87
|
|
|
new ChangeEntry( |
|
88
|
|
|
array( |
|
89
|
|
|
'subheading' => 'C', |
|
90
|
|
|
'content' => '6', |
|
91
|
|
|
) |
|
92
|
|
|
), |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertSame( array(), $entry->getChanges() ); |
|
96
|
|
|
$this->assertSame( array(), $entry->getChangesBySubheading() ); |
|
97
|
|
|
$this->assertSame( array(), $entry->getChangesBySubheading( 'B' ) ); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertSame( $entry, $entry->setChanges( $changes ) ); |
|
100
|
|
|
$this->assertSame( $changes, $entry->getChanges() ); |
|
101
|
|
|
$this->assertSame( |
|
102
|
|
|
array( |
|
103
|
|
|
'A' => array( $changes[0] ), |
|
104
|
|
|
'B' => array( $changes[1], $changes[2] ), |
|
105
|
|
|
'C' => array( $changes[3] ), |
|
106
|
|
|
), |
|
107
|
|
|
$entry->getChangesBySubheading() |
|
108
|
|
|
); |
|
109
|
|
|
$this->assertSame( array( $changes[1], $changes[2] ), $entry->getChangesBySubheading( 'B' ) ); |
|
110
|
|
|
|
|
111
|
|
|
$c1 = new ChangeEntry( |
|
112
|
|
|
array( |
|
113
|
|
|
'subheading' => 'B', |
|
114
|
|
|
'content' => '5', |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
$c2 = new ChangeEntry( |
|
118
|
|
|
array( |
|
119
|
|
|
'subheading' => 'B', |
|
120
|
|
|
'content' => '5', |
|
121
|
|
|
) |
|
122
|
|
|
); |
|
123
|
|
|
$c3 = new ChangeEntry( |
|
124
|
|
|
array( |
|
125
|
|
|
'subheading' => 'X', |
|
126
|
|
|
'content' => '1', |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
$this->assertSame( $entry, $entry->insertEntry( $c1 ) ); |
|
130
|
|
|
$this->assertSame( $entry, $entry->insertEntry( $c2, array( 'ordering' => array( 'content' ) ) ) ); |
|
131
|
|
|
$this->assertSame( $entry, $entry->insertEntry( $c3 ) ); |
|
132
|
|
|
$this->assertSame( array( $c2, $changes[0], $changes[1], $c1, $changes[2], $changes[3], $c3 ), $entry->getChanges() ); |
|
133
|
|
|
$this->assertSame( |
|
134
|
|
|
array( |
|
135
|
|
|
'B' => array( $c2, $changes[1], $c1, $changes[2] ), |
|
136
|
|
|
'A' => array( $changes[0] ), |
|
137
|
|
|
'C' => array( $changes[3] ), |
|
138
|
|
|
'X' => array( $c3 ), |
|
139
|
|
|
), |
|
140
|
|
|
$entry->getChangesBySubheading() |
|
141
|
|
|
); |
|
142
|
|
|
$this->assertSame( array( $c2, $changes[1], $c1, $changes[2] ), $entry->getChangesBySubheading( 'B' ) ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Test constructor error. |
|
147
|
|
|
*/ |
|
148
|
|
|
public function testConstructor_error() { |
|
149
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
150
|
|
|
$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\ChangelogEntry::__construct: Unrecognized data item "foo"' ); |
|
151
|
|
|
new ChangelogEntry( '1.0', array( 'foo' => 'bar' ) ); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Test setVersion error. |
|
156
|
|
|
*/ |
|
157
|
|
|
public function testSetVersion_error() { |
|
158
|
|
|
$entry = new ChangelogEntry( '1.0' ); |
|
159
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
160
|
|
|
$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\ChangelogEntry::setVersion: Version may not be empty' ); |
|
161
|
|
|
$entry->setVersion( '' ); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Test setLink error. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function testSetLink_error() { |
|
168
|
|
|
$entry = new ChangelogEntry( '1.0' ); |
|
169
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
170
|
|
|
$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\ChangelogEntry::setLink: Invalid URL' ); |
|
171
|
|
|
$entry->setLink( '/bogus' ); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Test setTimestamp error. |
|
176
|
|
|
*/ |
|
177
|
|
|
public function testSetTimestamp_error() { |
|
178
|
|
|
$entry = new ChangelogEntry( '1.0' ); |
|
179
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
180
|
|
|
$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\ChangelogEntry::setTimestamp: Invalid timestamp' ); |
|
181
|
|
|
$entry->setTimestamp( 'bogus' ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Test setChanges error. |
|
186
|
|
|
*/ |
|
187
|
|
View Code Duplication |
public function testSetChanges_error1() { |
|
188
|
|
|
$entry = new ChangelogEntry( '1.0' ); |
|
189
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
190
|
|
|
$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\ChangelogEntry::setChanges: Expected a ChangeEntry, got NULL at index 0' ); |
|
191
|
|
|
$entry->setChanges( array( null ) ); |
|
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Test setChanges error. |
|
196
|
|
|
*/ |
|
197
|
|
View Code Duplication |
public function testSetChanges_error2() { |
|
198
|
|
|
$entry = new ChangelogEntry( '1.0' ); |
|
199
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
200
|
|
|
$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\ChangelogEntry::setChanges: Expected a ChangeEntry, got Automattic\\Jetpack\\Changelog\\ChangelogEntry at index 0' ); |
|
201
|
|
|
$entry->setChanges( array( $entry ) ); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
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: