Completed
Push — sync/checksum-sort ( b32790...6aaf1a )
by
unknown
07:51
created

WP_Test_Autoloader_Handler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 120
Duplicated Lines 24.17 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 29
loc 120
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

6 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() 15 15 1
A test_should_autoloader_reset_unknown_plugin() 0 14 1
A test_should_autoloader_reset_invalid_cache() 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
		global $jetpack_autoloader_cached_plugin_paths;
65
		$jetpack_autoloader_cached_plugin_paths = array( TEST_DATA_PATH . '/plugins/plugin_current' );
66
67
		$autoloader_handler = new Autoloader_Handler(
68
			TEST_DATA_PATH . '/plugins/plugin_current',
69
			array( TEST_DATA_PATH . '/plugins/plugin_current' ),
70
			new Autoloader_Locator( new Version_Selector() ),
71
			new Version_Selector()
72
		);
73
74
		$this->assertFalse( $autoloader_handler->should_autoloader_reset() );
75
		$this->assertEmpty( $jetpack_autoloader_activating_plugins_paths );
76
	}
77
78
	/**
79
	 * Tests should_autoloader_reset() with an activating, unknown plugin.
80
	 */
81
	public function test_should_autoloader_reset_unknown_plugin() {
82
		global $jetpack_autoloader_activating_plugins_paths;
83
84
		$autoloader_handler = new Autoloader_Handler(
85
			TEST_DATA_PATH . '/plugins/plugin_current',
86
			array(),
87
			new Autoloader_Locator( new Version_Selector() ),
88
			new Version_Selector()
89
		);
90
91
		$this->assertTrue( $autoloader_handler->should_autoloader_reset() );
92
		$this->assertCount( 1, $jetpack_autoloader_activating_plugins_paths );
93
		$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current', $jetpack_autoloader_activating_plugins_paths[0] );
94
	}
95
96
	/**
97
	 * Tests should_autoloader_reset() with an old cache set of plugin paths.
98
	 */
99 View Code Duplication
	public function test_should_autoloader_reset_invalid_cache() {
100
		global $jetpack_autoloader_cached_plugin_paths;
101
		$jetpack_autoloader_cached_plugin_paths = array();
102
103
		$autoloader_handler = new Autoloader_Handler(
104
			TEST_DATA_PATH . '/plugins/plugin_current',
105
			array( TEST_DATA_PATH . '/plugins/plugin_current' ),
106
			new Autoloader_Locator( new Version_Selector() ),
107
			new Version_Selector()
108
		);
109
110
		$this->assertTrue( $autoloader_handler->should_autoloader_reset() );
111
		$this->assertEquals( array( TEST_DATA_PATH . '/plugins/plugin_current' ), $jetpack_autoloader_cached_plugin_paths );
112
	}
113
114
	/**
115
	 * Tests that the handler is able to build a loader.
116
	 */
117
	public function test_builds_autoloader() {
118
		$autoloader_handler = new Autoloader_Handler(
119
			TEST_DATA_PATH . '/plugins/plugin_current',
120
			array( TEST_DATA_PATH . '/plugins/plugin_current' ),
121
			new Autoloader_Locator( new Version_Selector() ),
122
			new Version_Selector()
123
		);
124
125
		$loader = $autoloader_handler->build_autoloader();
126
127
		$file = $loader->find_class_file( \Automattic\Jetpack\Autoloader\AutoloadGenerator::class );
128
129
		$this->assertFileExists( $file );
130
		$this->assertContains( 'AutoloadGenerator.php', $file );
131
	}
132
}
133