Completed
Push — add/changelog-tooling ( fa9ac3...7f5585 )
by
unknown
517:08 queued 507:24
created

ChangelogTest::provideJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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

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...
98
	}
99
100
	/**
101
	 * Test setEntries error.
102
	 */
103 View Code Duplication
	public function testSetEntries_error2() {
104
		$changelog = new Changelog();
105
		$this->expectException( InvalidArgumentException::class );
106
		$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\Changelog::setEntries: Expected a ChangelogEntry, got Automattic\\Jetpack\\Changelog\\Changelog at index 0' );
107
		$changelog->setEntries( array( $changelog ) );
0 ignored issues
show
Documentation introduced by
array($changelog) is of type array<integer,object<Aut...hangelog\\Changelog>"}>, but the function expects a array<integer,object<Aut...ngelog\ChangelogEntry>>.

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...
108
	}
109
110
	/**
111
	 * Test JSON serialization.
112
	 *
113
	 * @dataProvider provideJson
114
	 * @param string           $json JSON data.
115
	 * @param Changelog|string $changelog Changelog, or error message if decoding should fail.
116
	 */
117 View Code Duplication
	public function testJson( $json, $changelog ) {
118
		if ( is_string( $changelog ) ) {
119
			$this->expectException( InvalidArgumentException::class );
120
			$this->expectExceptionMessage( $changelog );
121
			Changelog::jsonUnserialize( json_decode( $json ) );
122
		} else {
123
			$this->assertSame( $json, json_encode( $changelog ) );
124
			$this->assertEquals( $changelog, Changelog::jsonUnserialize( json_decode( $json ) ) );
125
		}
126
	}
127
128
	/**
129
	 * Data provider for testJson.
130
	 */
131
	public function provideJson() {
132
		return array(
133
			'Basic serialization'              => array(
134
				'{"__class__":"Automattic\\\\Jetpack\\\\Changelog\\\\Changelog","prologue":"","epilogue":"","entries":[]}',
135
				new Changelog(),
136
			),
137
			'Serialization with data'          => array(
138
				'{"__class__":"Automattic\\\\Jetpack\\\\Changelog\\\\Changelog","prologue":"Prologue","epilogue":"Epilogue","entries":[{"__class__":"Automattic\\\\Jetpack\\\\Changelog\\\\ChangelogEntry","version":"1.1","link":null,"timestamp":"2021-02-18T00:00:00+00:00","prologue":"","epilogue":"","changes":[]},{"__class__":"Automattic\\\\Jetpack\\\\Changelog\\\\ChangelogEntry","version":"1.0","link":null,"timestamp":"2021-02-17T00:00:00+00:00","prologue":"","epilogue":"","changes":[]}]}',
139
				( new Changelog() )->setPrologue( 'Prologue' )->setEpilogue( 'Epilogue' )->setEntries(
140
					array(
141
						new ChangelogEntry( '1.1', array( 'timestamp' => '2021-02-18' ) ),
142
						new ChangelogEntry( '1.0', array( 'timestamp' => '2021-02-17' ) ),
143
					)
144
				),
145
			),
146
			'Bad unserialization, no class'    => array(
147
				'{"prologue":"","epilogue":"","entries":[]}',
148
				'Invalid data',
149
			),
150
			'Bad unserialization, wrong class' => array(
151
				'{"__class__":"Automattic\\\\Jetpack\\\\Changelog\\\\ChangelogEntry","version":"1.0","link":null,"timestamp":"2021-02-18T00:00:00+00:00","prologue":"","epilogue":"","changes":[]}',
152
				'Cannot instantiate Automattic\\Jetpack\\Changelog\\ChangelogEntry via Automattic\\Jetpack\\Changelog\\Changelog::jsonUnserialize',
153
			),
154
		);
155
	}
156
157
}
158