Completed
Push — add/changelog-tooling ( b30521...fa9ac3 )
by
unknown
1097:09 queued 1086:59
created

ChangelogTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 89
Duplicated Lines 13.48 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 12
loc 89
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testPrologueAndEpilogue() 0 13 1
A testEntries() 0 44 1
A testSetEntries_error1() 6 6 1
A testSetEntries_error2() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 ) );
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...
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 ) );
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...
106
	}
107
108
}
109