Completed
Push — renovate/mocha-8.x ( 07030e...e8e64c )
by
unknown
28:17 queued 19:09
created

WP_Test_Autoloader_Handler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 27 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 27
loc 100
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_is_latest_autoloader_does_nothing_if_this_is_it() 0 13 1
A test_is_latest_autoloader_requires_latest_if_this_is_not_it() 0 17 1
A test_should_autoloader_reset_known_plugin() 13 13 1
A test_should_autoloader_reset_unknown_plugin() 14 14 1
A test_builds_autoloader() 0 15 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
2
/**
3
 * Autoloader handler test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * Test suite class for the Autoloader handler.
12
 */
13
class WP_Test_Autoloader_Handler extends TestCase {
14
15
	/**
16
	 * Tests that the latest autoloader can be recognized as the current.
17
	 *
18
	 * @runInSeparateProcess
19
	 * @preserveGlobalState disabled
20
	 */
21
	public function test_is_latest_autoloader_does_nothing_if_this_is_it() {
22
		$autoloader_handler = new Autoloader_Handler(
23
			TEST_DATA_PATH . '/plugins/plugin_current',
24
			array( TEST_DATA_PATH . '/plugins/plugin_current' ),
25
			new Autoloader_Locator( new Version_Selector() ),
26
			new Version_Selector()
27
		);
28
29
		$this->assertTrue( $autoloader_handler->is_latest_autoloader() );
30
31
		global $jetpack_autoloader_latest_version;
32
		$this->assertEquals( '2.0.0.0', $jetpack_autoloader_latest_version );
33
	}
34
35
	/**
36
	 * Tests that the latest autoloader will be required if not this.
37
	 *
38
	 * @runInSeparateProcess
39
	 * @preserveGlobalState disabled
40
	 */
41
	public function test_is_latest_autoloader_requires_latest_if_this_is_not_it() {
42
		$autoloader_handler = new Autoloader_Handler(
43
			TEST_DATA_PATH . '/plugins/plugin_current',
44
			array(
45
				TEST_DATA_PATH . '/plugins/plugin_current',
46
				TEST_DATA_PATH . '/plugins/plugin_newer',
47
			),
48
			new Autoloader_Locator( new Version_Selector() ),
49
			new Version_Selector()
50
		);
51
52
		$this->assertFalse( $autoloader_handler->is_latest_autoloader() );
53
54
		global $jetpack_autoloader_latest_version;
55
		$this->assertEquals( '2.2.0.0', $jetpack_autoloader_latest_version );
56
		$this->assertContains( TEST_DATA_PATH . '/plugins/plugin_newer/vendor/autoload_packages.php', get_included_files() );
57
	}
58
59
	/**
60
	 * Tests should_autoloader_reset() with an already active plugin.
61
	 */
62 View Code Duplication
	public function test_should_autoloader_reset_known_plugin() {
63
		global $jetpack_autoloader_activating_plugins_paths;
64
65
		$autoloader_handler = new Autoloader_Handler(
66
			TEST_DATA_PATH . '/plugins/plugin_current',
67
			array( TEST_DATA_PATH . '/plugins/plugin_current' ),
68
			new Autoloader_Locator( new Version_Selector() ),
69
			new Version_Selector()
70
		);
71
72
		$this->assertFalse( $autoloader_handler->should_autoloader_reset() );
73
		$this->assertEmpty( $jetpack_autoloader_activating_plugins_paths );
74
	}
75
76
	/**
77
	 * Tests should_autoloader_reset() with an activating, unknown plugin.
78
	 */
79 View Code Duplication
	public function test_should_autoloader_reset_unknown_plugin() {
80
		global $jetpack_autoloader_activating_plugins_paths;
81
82
		$autoloader_handler = new Autoloader_Handler(
83
			TEST_DATA_PATH . '/plugins/plugin_current',
84
			array(),
85
			new Autoloader_Locator( new Version_Selector() ),
86
			new Version_Selector()
87
		);
88
89
		$this->assertTrue( $autoloader_handler->should_autoloader_reset() );
90
		$this->assertCount( 1, $jetpack_autoloader_activating_plugins_paths );
91
		$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current', $jetpack_autoloader_activating_plugins_paths[0] );
92
	}
93
94
	/**
95
	 * Tests that the handler is able to build a loader.
96
	 */
97
	public function test_builds_autoloader() {
98
		$autoloader_handler = new Autoloader_Handler(
99
			TEST_DATA_PATH . '/plugins/plugin_current',
100
			array( TEST_DATA_PATH . '/plugins/plugin_current' ),
101
			new Autoloader_Locator( new Version_Selector() ),
102
			new Version_Selector()
103
		);
104
105
		$loader = $autoloader_handler->build_autoloader();
106
107
		$file = $loader->find_class_file( \Automattic\Jetpack\Autoloader\AutoloadGenerator::class );
108
109
		$this->assertFileExists( $file );
110
		$this->assertContains( 'AutoloadGenerator.php', $file );
111
	}
112
}
113