Completed
Push — update/phpunit-php-8 ( e34c58...05aa04 )
by
unknown
137:27 queued 129:12
created

WP_Test_Integration_Manifest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 142
Duplicated Lines 50.7 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 72
loc 142
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() 24 24 1
A test_that_handler_reads_psr4_manifests() 24 24 1
A test_that_handler_reads_files_manifests() 24 24 1
A write_test_manifest() 0 6 1
A set_up() 0 13 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 WP_Test_Integration_Manifest 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/plugin_current/test-manifest.php';
22
23
	/**
24
	 * The manifest handler we're testing.
25
	 *
26
	 * @var Manifest_Handler
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_Handler(
37
			array(
38
				TEST_DATA_PATH . '/plugins/plugin_current',
39
			),
40
			new Version_Selector()
41
		);
42
43
		// Make sure the test manifest does not exist.
44
		if ( file_exists( self::TEST_MANIFEST_PATH ) ) {
45
			unlink( self::TEST_MANIFEST_PATH );
46
		}
47
	}
48
49
	/**
50
	 * Teardown runs after each test.
51
	 *
52
	 * @after
53
	 */
54
	public function tear_down() {
55
		// Make sure the test manifest does not exist.
56
		if ( file_exists( self::TEST_MANIFEST_PATH ) ) {
57
			unlink( self::TEST_MANIFEST_PATH );
58
		}
59
	}
60
61
	/**
62
	 * Tests that the classmap manifest we generate can be read by the handler.
63
	 */
64 View Code Duplication
	public function test_that_handler_reads_classmap_manifests() {
65
		$this->write_test_manifest(
66
			'classmap',
67
			array(
68
				'TestFile' => array(
69
					'path'    => '$baseDir . \'/path_to_file.php\'',
70
					'version' => '1.0.0.0',
71
				),
72
			)
73
		);
74
75
		$loaded = array();
76
		$this->manifest_handler->register_plugin_manifests( 'test-manifest.php', $loaded );
77
78
		$this->assertEquals(
79
			array(
80
				'TestFile' => array(
81
					'version' => '1.0.0.0',
82
					'path'    => TEST_DATA_PATH . '/path_to_file.php',
83
				),
84
			),
85
			$loaded
86
		);
87
	}
88
89
	/**
90
	 * Tests that the PSR-4 manifest we generate can be read by the handler.
91
	 */
92 View Code Duplication
	public function test_that_handler_reads_psr4_manifests() {
93
		$this->write_test_manifest(
94
			'psr-4',
95
			array(
96
				'Automattic\\Jetpack\\' => array(
97
					'path'    => array( '$baseDir . \'/src\'' ),
98
					'version' => '1.2.0.0',
99
				),
100
			)
101
		);
102
103
		$loaded = array();
104
		$this->manifest_handler->register_plugin_manifests( 'test-manifest.php', $loaded );
105
106
		$this->assertEquals(
107
			array(
108
				'Automattic\\Jetpack\\' => array(
109
					'version' => '1.2.0.0',
110
					'path'    => array( TEST_DATA_PATH . '/src' ),
111
				),
112
			),
113
			$loaded
114
		);
115
	}
116
117
	/**
118
	 * Tests that the files manifest we generate can be read by the handler.
119
	 */
120 View Code Duplication
	public function test_that_handler_reads_files_manifests() {
121
		$this->write_test_manifest(
122
			'files',
123
			array(
124
				'123d5a6s7vd' => array(
125
					'path'    => '$baseDir . \'/path_to_file.php\'',
126
					'version' => '1.3.0.0',
127
				),
128
			)
129
		);
130
131
		$loaded = array();
132
		$this->manifest_handler->register_plugin_manifests( 'test-manifest.php', $loaded );
133
134
		$this->assertEquals(
135
			array(
136
				'123d5a6s7vd' => array(
137
					'version' => '1.3.0.0',
138
					'path'    => TEST_DATA_PATH . '/path_to_file.php',
139
				),
140
			),
141
			$loaded
142
		);
143
	}
144
145
	/**
146
	 * Writes the test manifest for the tests to use.
147
	 *
148
	 * @param string $autoload_type The type of manifest to generate.
149
	 * @param array  $content The content to write a manifest using.
150
	 */
151
	private function write_test_manifest( $autoload_type, $content ) {
152
		file_put_contents(
153
			self::TEST_MANIFEST_PATH,
154
			ManifestGenerator::buildManifest( $autoload_type, 'test-manifest.php', $content )
155
		);
156
	}
157
}
158