Completed
Push — add/e2e-connection-purchase-fl... ( d02cce...68beb5 )
by Yaroslav
14:07 queued 04:35
created

WP_Test_Manifest_Handler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 54.02 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 47
loc 87
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_registering_adds_to_the_input_array() 15 15 1
A test_registering_adds_latest_version_to_the_input_array() 16 16 1
A test_registering_does_not_add_dev_versions_to_the_input_array() 16 16 1
A test_registering_adds_the_dev_version_to_the_input_array_with_constant() 0 17 2

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
2
/**
3
 * File loader test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use Jetpack\AutoloaderTestData\Plugin\Test;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Test.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Test suite class for the Autoloader part that handles file loading.
13
 */
14
class WP_Test_Manifest_Handler extends TestCase {
15
16
	/**
17
	 * Tests whether registering a manifest file registers the individual class file.
18
	 */
19 View Code Duplication
	public function test_registering_adds_to_the_input_array() {
20
		$input_array      = array();
21
		$manifest_handler = new Manifest_Handler(
22
			array(
23
				TEST_DATA_PATH . '/plugins/plugin_current',
24
			),
25
			new Version_Selector()
26
		);
27
28
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $input_array );
29
30
		$this->assertArrayHasKey( Test::class, $input_array );
31
		$this->assertEquals( '1.0.0.0', $input_array[ Test::class ]['version'] );
32
		$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php' );
33
	}
34
35
	/**
36
	 * Tests whether registering a manifest file will override already registered paths with newer ones.
37
	 */
38 View Code Duplication
	public function test_registering_adds_latest_version_to_the_input_array() {
39
		$input_array      = array();
40
		$manifest_handler = new Manifest_Handler(
41
			array(
42
				TEST_DATA_PATH . '/plugins/plugin_newer',
43
				TEST_DATA_PATH . '/plugins/plugin_current',
44
			),
45
			new Version_Selector()
46
		);
47
48
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $input_array );
49
50
		$this->assertArrayHasKey( Test::class, $input_array );
51
		$this->assertEquals( '2.0.0.0', $input_array[ Test::class ]['version'] );
52
		$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/plugin_newer/includes/class-test.php' );
53
	}
54
55
	/**
56
	 * Tests whether registering a manifest file ignores the dev version of the file when
57
	 * JETPACK_AUTOLOAD_DEV is not set.
58
	 */
59 View Code Duplication
	public function test_registering_does_not_add_dev_versions_to_the_input_array() {
60
		$input_array      = array();
61
		$manifest_handler = new Manifest_Handler(
62
			array(
63
				TEST_DATA_PATH . '/plugins/plugin_dev',
64
				TEST_DATA_PATH . '/plugins/plugin_current',
65
			),
66
			new Version_Selector()
67
		);
68
69
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $input_array );
70
71
		$this->assertArrayHasKey( Test::class, $input_array );
72
		$this->assertEquals( '1.0.0.0', $input_array[ Test::class ]['version'] );
73
		$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php' );
74
	}
75
76
	/**
77
	 * Tests whether registering a manifest file prioritizes the dev version of the file when
78
	 * JETPACK_AUTOLOAD_DEV is set to true.
79
	 *
80
	 * @runInSeparateProcess
81
	 * @preserveGlobalState disabled
82
	 */
83
	public function test_registering_adds_the_dev_version_to_the_input_array_with_constant() {
84
		$input_array      = array();
85
		$manifest_handler = new Manifest_Handler(
86
			array(
87
				TEST_DATA_PATH . '/plugins/plugin_dev',
88
				TEST_DATA_PATH . '/plugins/plugin_current',
89
			),
90
			new Version_Selector()
91
		);
92
		defined( 'JETPACK_AUTOLOAD_DEV' ) || define( 'JETPACK_AUTOLOAD_DEV', true );
93
94
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $input_array );
95
96
		$this->assertArrayHasKey( Test::class, $input_array );
97
		$this->assertEquals( 'dev-main', $input_array[ Test::class ]['version'] );
98
		$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/plugin_dev/includes/class-test.php' );
99
	}
100
}
101