Completed
Push — add/changelog-tooling ( c37726...963953 )
by
unknown
371:25 queued 360:46
created

ChangelogEntryTest::testSetChanges_error2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

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

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
187
	}
188
189
	/**
190
	 * Test setChanges error.
191
	 */
192 View Code Duplication
	public function testSetChanges_error2() {
193
		$entry = new ChangelogEntry( '1.0' );
194
		$this->expectException( InvalidArgumentException::class );
195
		$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\ChangelogEntry::setChanges: Expected a ChangeEntry, got Automattic\\Jetpack\\Changelog\\ChangelogEntry at index 0' );
196
		$entry->setChanges( array( $entry ) );
0 ignored issues
show
Documentation introduced by
array($entry) is of type array<integer,object<Aut...log\\ChangelogEntry>"}>, but the function expects a array<integer,object<Aut...Changelog\ChangeEntry>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
197
	}
198
199
}
200