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

ChangelogTest::testEntries()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 80
rs 8.4362
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		$e = $changelog->addEntry();
67
		$this->assertInstanceOf( ChangelogEntry::class, $e );
68
		$this->assertSame( '0.0.1-dev', $e->getVersion() );
69
70
		$this->assertSame( array( $e ), $changelog->getEntries() );
71
		$this->assertSame( $e, $changelog->getLatestEntry() );
72
		$this->assertSame( array( '0.0.1-dev' ), $changelog->getVersions() );
73
		$this->assertSame( null, $changelog->findEntryByVersion( '2.0' ) );
74
		$this->assertSame(
75
			array(),
76
			$changelog->findEntriesByVersions(
77
				array(
78
					'>=' => '2.0',
79
					'<'  => '4.0',
80
				)
81
			)
82
		);
83
84
		$this->assertSame( $changelog, $changelog->setEntries( $entries ) );
85
		$this->assertSame( array_values( $entries ), $changelog->getEntries() );
86
		$this->assertSame( $entries[4], $changelog->getLatestEntry() );
87
		$this->assertSame( array( '4.0', '3.0', '2.0', '1.0' ), $changelog->getVersions() );
88
		$this->assertSame( $entries[2], $changelog->findEntryByVersion( '2.0' ) );
89
		$this->assertSame(
90
			array( $entries[3], $entries[2] ),
91
			$changelog->findEntriesByVersions(
92
				array(
93
					'>=' => '2.0',
94
					'<'  => '4.0',
95
				)
96
			)
97
		);
98
99
		$e1 = $changelog->addEntry();
100
		$this->assertInstanceOf( ChangelogEntry::class, $e1 );
101
		$this->assertSame( '4.0-p1', $e1->getVersion() );
102
		$e2 = $changelog->addEntry();
103
		$this->assertInstanceOf( ChangelogEntry::class, $e2 );
104
		$this->assertSame( '4.0-p2', $e2->getVersion() );
105
		$e2->setVersion( '4.0-dev+pl_123' );
106
		$e3 = $changelog->addEntry();
107
		$this->assertInstanceOf( ChangelogEntry::class, $e3 );
108
		$this->assertSame( '4.0-dev+pl_124', $e3->getVersion() );
109
		$this->assertSame( array_merge( array( $e3, $e2, $e1 ), array_values( $entries ) ), $changelog->getEntries() );
110
		$this->assertSame( $e3, $changelog->getLatestEntry() );
111
		$this->assertSame( array( '4.0-dev+pl_124', '4.0-dev+pl_123', '4.0-p1', '4.0', '3.0', '2.0', '1.0' ), $changelog->getVersions() );
112
		$this->assertSame( $entries[2], $changelog->findEntryByVersion( '2.0' ) );
113
		$this->assertSame(
114
			array( $e3, $e2, $entries[3], $entries[2] ),
115
			$changelog->findEntriesByVersions(
116
				array(
117
					'>=' => '2.0',
118
					'<'  => '4.0',
119
				)
120
			)
121
		);
122
	}
123
124
	/**
125
	 * Test setEntries error.
126
	 */
127 View Code Duplication
	public function testSetEntries_error1() {
128
		$changelog = new Changelog();
129
		$this->expectException( InvalidArgumentException::class );
130
		$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\Changelog::setEntries: Expected a ChangelogEntry, got NULL at index 0' );
131
		$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...
132
	}
133
134
	/**
135
	 * Test setEntries error.
136
	 */
137 View Code Duplication
	public function testSetEntries_error2() {
138
		$changelog = new Changelog();
139
		$this->expectException( InvalidArgumentException::class );
140
		$this->expectExceptionMessage( 'Automattic\\Jetpack\\Changelog\\Changelog::setEntries: Expected a ChangelogEntry, got Automattic\\Jetpack\\Changelog\\Changelog at index 0' );
141
		$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...
142
	}
143
144
}
145