Completed
Push — try/e2e-check-if-chrome-instal... ( 24dbcf...296824 )
by Yaroslav
23:03 queued 13:54
created

AutoloadProcessorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 240
Duplicated Lines 35.83 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 86
loc 240
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A test_process_functions_return_null_when_empty() 0 9 1
A test_process_classmap_does_not_scan_psr_packages() 0 41 1
A test_process_classmap_scans_psr_packages_when_requested() 43 43 1
A test_process_classmap_uses_blacklist() 43 43 1
A test_process_psr_packages() 0 29 1
A test_process_psr_packages_does_nothing_when_converting_to_classmap() 0 18 1
A test_process_files() 0 27 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 test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use Automattic\Jetpack\Autoloader\AutoloadProcessor;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Test suite class for the Autoload processor.
13
 */
14
class AutoloadProcessorTest extends TestCase {
15
16
	/**
17
	 * Tests that all of the process functions are safe when not given an autoload they're expecting.
18
	 */
19
	public function test_process_functions_return_null_when_empty() {
20
		$processor = new AutoloadProcessor( null, null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a callable.

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...
21
22
		$this->assertNull( $processor->processClassmap( array(), false ) );
23
		$this->assertNull( $processor->processClassmap( array(), true ) );
24
		$this->assertNull( $processor->processPsr4Packages( array(), false ) );
25
		$this->assertNull( $processor->processPsr4Packages( array(), true ) );
26
		$this->assertNull( $processor->processFiles( array() ) );
27
	}
28
29
	/**
30
	 * Tests that `processClassmap` does not scan PSR-4 packages unless requested.
31
	 */
32
	public function test_process_classmap_does_not_scan_psr_packages() {
33
		$classmap_scanner      = function ( $path, $class_blacklist, $namespace ) {
34
			$this->assertEquals( 'src', $path );
35
			$this->assertNull( $class_blacklist );
36
			$this->assertNull( $namespace );
37
38
			return array(
39
				'TestClass'  => 'src/TestClass.php',
40
				'TestClass2' => 'src/TestClass2.php',
41
			);
42
		};
43
		$path_code_transformer = function ( $path ) {
44
			return 'converted-' . $path;
45
		};
46
		$processor             = new AutoloadProcessor( $classmap_scanner, $path_code_transformer );
47
48
		$autoloads = array(
49
			'classmap' => array(
50
				array(
51
					'path'    => 'src',
52
					'version' => 'dev-test',
53
				),
54
			),
55
		);
56
57
		$processed = $processor->processClassmap( $autoloads, false );
58
59
		$this->assertEquals(
60
			array(
61
				'TestClass'  => array(
62
					'version' => 'dev-test',
63
					'path'    => 'converted-src/TestClass.php',
64
				),
65
				'TestClass2' => array(
66
					'version' => 'dev-test',
67
					'path'    => 'converted-src/TestClass2.php',
68
				),
69
			),
70
			$processed
71
		);
72
	}
73
74
	/**
75
	 * Tests that `processClassmap` scans PSR-4 packages when requested.
76
	 */
77 View Code Duplication
	public function test_process_classmap_scans_psr_packages_when_requested() {
78
		$classmap_scanner      = function ( $path, $class_blacklist, $namespace ) {
79
			$this->assertEquals( 'src', $path );
80
			$this->assertNull( $class_blacklist );
81
			$this->assertEquals( 'Jetpack\\Autoloader\\', $namespace );
82
83
			return array(
84
				'TestClass'  => 'src/TestClass.php',
85
				'TestClass2' => 'src/TestClass2.php',
86
			);
87
		};
88
		$path_code_transformer = function ( $path ) {
89
			return 'converted2-' . $path;
90
		};
91
		$processor             = new AutoloadProcessor( $classmap_scanner, $path_code_transformer );
92
93
		$autoloads = array(
94
			'psr-4' => array(
95
				'Jetpack\\Autoloader\\' => array(
96
					array(
97
						'path'    => 'src',
98
						'version' => 'dev-test2',
99
					),
100
				),
101
			),
102
		);
103
104
		$processed = $processor->processClassmap( $autoloads, true );
105
106
		$this->assertEquals(
107
			array(
108
				'TestClass'  => array(
109
					'version' => 'dev-test2',
110
					'path'    => 'converted2-src/TestClass.php',
111
				),
112
				'TestClass2' => array(
113
					'version' => 'dev-test2',
114
					'path'    => 'converted2-src/TestClass2.php',
115
				),
116
			),
117
			$processed
118
		);
119
	}
120
121
	/**
122
	 * Tests that `processClassmap` passes the blacklist correctly when given one.
123
	 */
124 View Code Duplication
	public function test_process_classmap_uses_blacklist() {
125
		$classmap_scanner      = function ( $path, $class_blacklist, $namespace ) {
126
			$this->assertEquals( 'src', $path );
127
			$this->assertEquals( '{(TestClass)}', $class_blacklist );
128
			$this->assertNull( $namespace );
129
130
			return array(
131
				'TestClass'  => 'src/TestClass.php',
132
				'TestClass2' => 'src/TestClass2.php',
133
			);
134
		};
135
		$path_code_transformer = function ( $path ) {
136
			return 'converted-' . $path;
137
		};
138
		$processor             = new AutoloadProcessor( $classmap_scanner, $path_code_transformer );
139
140
		$autoloads = array(
141
			'classmap' => array(
142
				array(
143
					'path'    => 'src',
144
					'version' => 'dev-test',
145
				),
146
			),
147
		);
148
149
		$autoloads['exclude-from-classmap'] = array( 'TestClass' );
150
151
		$processed = $processor->processClassmap( $autoloads, false );
152
153
		$this->assertEquals(
154
			array(
155
				'TestClass'  => array(
156
					'version' => 'dev-test',
157
					'path'    => 'converted-src/TestClass.php',
158
				),
159
				'TestClass2' => array(
160
					'version' => 'dev-test',
161
					'path'    => 'converted-src/TestClass2.php',
162
				),
163
			),
164
			$processed
165
		);
166
	}
167
168
	/**
169
	 * Tests that `processPsr4Packages` returns the expected format.
170
	 */
171
	public function test_process_psr_packages() {
172
		$path_code_transformer = function ( $path ) {
173
			return 'converted-' . $path;
174
		};
175
		$processor             = new AutoloadProcessor( null, $path_code_transformer );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a callable.

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...
176
177
		$autoloads = array(
178
			'psr-4' => array(
179
				'Jetpack\\Autoloader\\' => array(
180
					array(
181
						'path'    => 'src',
182
						'version' => 'dev-test',
183
					),
184
				),
185
			),
186
		);
187
188
		$processed = $processor->processPsr4Packages( $autoloads, false );
189
190
		$this->assertEquals(
191
			array(
192
				'Jetpack\\Autoloader\\' => array(
193
					'path'    => array( 'converted-src' ),
194
					'version' => 'dev-test',
195
				),
196
			),
197
			$processed
198
		);
199
	}
200
201
	/**
202
	 * Tests that `processPsr4Packages` does not when we're indicating that we want to make them a classmap.
203
	 */
204
	public function test_process_psr_packages_does_nothing_when_converting_to_classmap() {
205
		$processor = new AutoloadProcessor( null, null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a callable.

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...
206
207
		$autoloads = array(
208
			'psr-4' => array(
209
				'Jetpack\\Autoloader\\' => array(
210
					array(
211
						'path'    => 'src',
212
						'version' => 'dev-test',
213
					),
214
				),
215
			),
216
		);
217
218
		$processed = $processor->processPsr4Packages( $autoloads, true );
219
220
		$this->assertNull( $processed );
221
	}
222
223
	/**
224
	 * Tests that `processFiles` returns the expected format.
225
	 */
226
	public function test_process_files() {
227
		$path_code_transformer = function ( $path ) {
228
			return 'converted-' . $path;
229
		};
230
		$processor             = new AutoloadProcessor( null, $path_code_transformer );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a callable.

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...
231
232
		$autoloads = array(
233
			'files' => array(
234
				'abcdef' => array(
235
					'path'    => 'src/file.php',
236
					'version' => 'dev-test',
237
				),
238
			),
239
		);
240
241
		$processed = $processor->processFiles( $autoloads );
242
243
		$this->assertEquals(
244
			array(
245
				'abcdef' => array(
246
					'path'    => 'converted-src/file.php',
247
					'version' => 'dev-test',
248
				),
249
			),
250
			$processed
251
		);
252
	}
253
}
254