Completed
Push — update/node-12191 ( 8875a2...f69c39 )
by
unknown
147:00 queued 137:34
created

Test_Loading_Generated_Manifests   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 149
Duplicated Lines 56.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 84
loc 149
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A test_that_handler_reads_classmap_manifests() 28 28 1
A test_that_handler_reads_psr4_manifests() 28 28 1
A test_that_handler_reads_files_manifests() 28 28 1
A write_test_manifest() 0 6 1
A set_up() 0 8 2
A tear_down() 0 6 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
 * Integration test suite for the manifest reading.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
9
10
use Automattic\Jetpack\Autoloader\ManifestGenerator;
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * Test suite class for verifying that manifests we generate can also be read correctly.
15
 */
16
class Test_Loading_Generated_Manifests extends TestCase {
17
18
	/**
19
	 * The path to the test manifest we want to operate on.
20
	 */
21
	const TEST_MANIFEST_PATH = TEST_DATA_PATH . '/plugins/dummy_current/test-manifest.php';
22
23
	/**
24
	 * The manifest handler we're testing.
25
	 *
26
	 * @var Manifest_Reader
27
	 */
28
	private $manifest_handler;
29
30
	/**
31
	 * Setup runs before each test.
32
	 *
33
	 * @before
34
	 */
35
	public function set_up() {
36
		$this->manifest_handler = new Manifest_Reader( new Version_Selector() );
37
38
		// Make sure the test manifest does not exist.
39
		if ( file_exists( self::TEST_MANIFEST_PATH ) ) {
40
			unlink( self::TEST_MANIFEST_PATH );
41
		}
42
	}
43
44
	/**
45
	 * Teardown runs after each test.
46
	 *
47
	 * @after
48
	 */
49
	public function tear_down() {
50
		// Make sure the test manifest does not exist.
51
		if ( file_exists( self::TEST_MANIFEST_PATH ) ) {
52
			unlink( self::TEST_MANIFEST_PATH );
53
		}
54
	}
55
56
	/**
57
	 * Tests that the classmap manifest we generate can be read by the handler.
58
	 */
59 View Code Duplication
	public function test_that_handler_reads_classmap_manifests() {
60
		$this->write_test_manifest(
61
			'classmap',
62
			array(
63
				'TestFile' => array(
64
					'path'    => '$baseDir . \'/path_to_file.php\'',
65
					'version' => '1.0.0.0',
66
				),
67
			)
68
		);
69
70
		$loaded = array();
71
		$this->manifest_handler->read_manifests(
72
			array( TEST_DATA_PATH . '/plugins/dummy_current' ),
73
			'test-manifest.php',
74
			$loaded
75
		);
76
77
		$this->assertEquals(
78
			array(
79
				'TestFile' => array(
80
					'version' => '1.0.0.0',
81
					'path'    => TEST_REAL_DATA_PATH . '/path_to_file.php',
82
				),
83
			),
84
			$loaded
85
		);
86
	}
87
88
	/**
89
	 * Tests that the PSR-4 manifest we generate can be read by the handler.
90
	 */
91 View Code Duplication
	public function test_that_handler_reads_psr4_manifests() {
92
		$this->write_test_manifest(
93
			'psr-4',
94
			array(
95
				'Automattic\\Jetpack\\' => array(
96
					'path'    => array( '$baseDir . \'/src\'' ),
97
					'version' => '1.2.0.0',
98
				),
99
			)
100
		);
101
102
		$loaded = array();
103
		$this->manifest_handler->read_manifests(
104
			array( TEST_DATA_PATH . '/plugins/dummy_current' ),
105
			'test-manifest.php',
106
			$loaded
107
		);
108
109
		$this->assertEquals(
110
			array(
111
				'Automattic\\Jetpack\\' => array(
112
					'version' => '1.2.0.0',
113
					'path'    => array( TEST_REAL_DATA_PATH . '/src' ),
114
				),
115
			),
116
			$loaded
117
		);
118
	}
119
120
	/**
121
	 * Tests that the files manifest we generate can be read by the handler.
122
	 */
123 View Code Duplication
	public function test_that_handler_reads_files_manifests() {
124
		$this->write_test_manifest(
125
			'files',
126
			array(
127
				'123d5a6s7vd' => array(
128
					'path'    => '$baseDir . \'/path_to_file.php\'',
129
					'version' => '1.3.0.0',
130
				),
131
			)
132
		);
133
134
		$loaded = array();
135
		$this->manifest_handler->read_manifests(
136
			array( TEST_DATA_PATH . '/plugins/dummy_current' ),
137
			'test-manifest.php',
138
			$loaded
139
		);
140
141
		$this->assertEquals(
142
			array(
143
				'123d5a6s7vd' => array(
144
					'version' => '1.3.0.0',
145
					'path'    => TEST_REAL_DATA_PATH . '/path_to_file.php',
146
				),
147
			),
148
			$loaded
149
		);
150
	}
151
152
	/**
153
	 * Writes the test manifest for the tests to use.
154
	 *
155
	 * @param string $autoload_type The type of manifest to generate.
156
	 * @param array  $content The content to write a manifest using.
157
	 */
158
	private function write_test_manifest( $autoload_type, $content ) {
159
		file_put_contents(
160
			self::TEST_MANIFEST_PATH,
161
			ManifestGenerator::buildManifest( $autoload_type, 'test-manifest.php', $content )
162
		);
163
	}
164
}
165