|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
/** |
|
3
|
|
|
* Tests for the changelog Changelog class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-changelogger |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Changelog\Tests; |
|
9
|
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Changelog\Changelog; |
|
11
|
|
|
use Automattic\Jetpack\Changelog\ChangelogEntry; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Tests for the changelog Changelog class. |
|
17
|
|
|
* |
|
18
|
|
|
* @covers \Automattic\Jetpack\Changelog\Changelog |
|
19
|
|
|
*/ |
|
20
|
|
|
class ChangelogTest extends TestCase { |
|
21
|
|
|
use \Yoast\PHPUnitPolyfills\Polyfills\ExpectException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Test prologue and epilogue. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testPrologueAndEpilogue() { |
|
27
|
|
|
$changelog = new Changelog(); |
|
28
|
|
|
$this->assertSame( '', $changelog->getPrologue() ); |
|
29
|
|
|
$this->assertSame( '', $changelog->getEpilogue() ); |
|
30
|
|
|
|
|
31
|
|
|
$this->assertSame( $changelog, $changelog->setPrologue( 'Foo' )->setEpilogue( 'Bar' ) ); |
|
32
|
|
|
$this->assertSame( 'Foo', $changelog->getPrologue() ); |
|
33
|
|
|
$this->assertSame( 'Bar', $changelog->getEpilogue() ); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertSame( $changelog, $changelog->setPrologue( 123 )->setEpilogue( 456 ) ); |
|
36
|
|
|
$this->assertSame( '123', $changelog->getPrologue() ); |
|
37
|
|
|
$this->assertSame( '456', $changelog->getEpilogue() ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Test entries. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testEntries() { |
|
44
|
|
|
$changelog = new Changelog(); |
|
45
|
|
|
$entries = array( |
|
46
|
|
|
4 => new ChangelogEntry( '4.0' ), |
|
47
|
|
|
3 => new ChangelogEntry( '3.0' ), |
|
48
|
|
|
2 => new ChangelogEntry( '2.0' ), |
|
49
|
|
|
1 => new ChangelogEntry( '1.0' ), |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertSame( array(), $changelog->getEntries() ); |
|
53
|
|
|
$this->assertSame( null, $changelog->getLatestEntry() ); |
|
54
|
|
|
$this->assertSame( array(), $changelog->getVersions() ); |
|
55
|
|
|
$this->assertSame( null, $changelog->findEntryByVersion( '2.0' ) ); |
|
56
|
|
|
$this->assertSame( |
|
57
|
|
|
array(), |
|
58
|
|
|
$changelog->findEntriesByVersions( |
|
59
|
|
|
array( |
|
60
|
|
|
'>=' => '2.0', |
|
61
|
|
|
'<' => '4.0', |
|
62
|
|
|
) |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertSame( $changelog, $changelog->setEntries( $entries ) ); |
|
67
|
|
|
$this->assertSame( array_values( $entries ), $changelog->getEntries() ); |
|
68
|
|
|
$this->assertSame( $entries[4], $changelog->getLatestEntry() ); |
|
69
|
|
|
$this->assertSame( array( '4.0', '3.0', '2.0', '1.0' ), $changelog->getVersions() ); |
|
70
|
|
|
$this->assertSame( $entries[2], $changelog->findEntryByVersion( '2.0' ) ); |
|
71
|
|
|
$this->assertSame( |
|
72
|
|
|
array( $entries[3], $entries[2] ), |
|
73
|
|
|
$changelog->findEntriesByVersions( |
|
74
|
|
|
array( |
|
75
|
|
|
'>=' => '2.0', |
|
76
|
|
|
'<' => '4.0', |
|
77
|
|
|
) |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$e = new ChangelogEntry( '5.0' ); |
|
82
|
|
|
$this->assertSame( $changelog, $changelog->addEntry( $e ) ); |
|
83
|
|
|
$this->assertSame( array( $e, $entries[4], $entries[3], $entries[2], $entries[1] ), $changelog->getEntries() ); |
|
84
|
|
|
$this->assertSame( $e, $changelog->getLatestEntry() ); |
|
85
|
|
|
$this->assertSame( array( '5.0', '4.0', '3.0', '2.0', '1.0' ), $changelog->getVersions() ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Test setEntries error. |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function testSetEntries_error1() { |
|
92
|
|
|
$changelog = new Changelog(); |
|
93
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
94
|
|
|
$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\Changelog::setEntries: Expected a ChangelogEntry, got NULL at index 0' ); |
|
95
|
|
|
$changelog->setEntries( array( null ) ); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Test setEntries error. |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
public function testSetEntries_error2() { |
|
102
|
|
|
$changelog = new Changelog(); |
|
103
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
104
|
|
|
$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\Changelog::setEntries: Expected a ChangelogEntry, got Automattic\\Jetpack\\Changelog\\Changelog at index 0' ); |
|
105
|
|
|
$changelog->setEntries( array( $changelog ) ); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
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: