Completed
Push — add/handling-connection-errors ( 76997e...1b4c4d )
by
unknown
331:58 queued 323:55
created

WP_Test_File_Loader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 32.56 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 6 1
A tearDown() 0 6 1
A test_enqueueing_adds_to_the_global_array() 8 8 1
A test_enqueueing_adds_the_latest_version_to_the_global_array() 9 9 1
A test_enqueueing_always_adds_the_dev_version_to_the_global_array() 11 11 1
A test_enqueued_file_is_actually_loaded() 0 14 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
 * Testing file for the autoloader package.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use Automattic\Jetpack\Autoloader as Autoloader;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Class WP_Test_File_Loader
13
 */
14
class WP_Test_File_Loader extends TestCase {
15
	/**
16
	 * Jetpack Package files.
17
	 *
18
	 * @var $jetpack_packages_files
19
	 */
20
	public static $jetpack_packages_files;
21
22
	/**
23
	 * Test setup.
24
	 */
25
	public function setup() {
26
		parent::setup();
27
		global $jetpack_packages_files;
28
		self::$jetpack_packages_files = $jetpack_packages_files;
29
		$jetpack_packages_files       = array();
30
	}
31
32
	/**
33
	 * Test tear down.
34
	 */
35
	public function tearDown() {
36
		parent::tearDown();
37
		// re-apply the global.
38
		global $jetpack_packages_files;
39
		$jetpack_packages_files = self::$jetpack_packages_files;
40
	}
41
42
	/**
43
	 * Does enqueuing add to the global array?
44
	 */
45 View Code Duplication
	public function test_enqueueing_adds_to_the_global_array() {
46
		Autoloader\enqueue_package_file( 'file_id_10', '1', 'path_to_file.php' );
47
48
		global $jetpack_packages_files;
49
		$this->assertTrue( isset( $jetpack_packages_files['file_id_10'] ) );
50
		$this->assertEquals( $jetpack_packages_files['file_id_10']['version'], '1' );
51
		$this->assertEquals( $jetpack_packages_files['file_id_10']['path'], 'path_to_file.php' );
52
	}
53
54
	/**
55
	 * Tests that latest version is added to the global array.
56
	 */
57 View Code Duplication
	public function test_enqueueing_adds_the_latest_version_to_the_global_array() {
58
		Autoloader\enqueue_package_file( 'file_id', '1', 'path_to_file' );
59
		Autoloader\enqueue_package_file( 'file_id', '2', 'path_to_file_v2' );
60
61
		global $jetpack_packages_files;
62
		$this->assertTrue( isset( $jetpack_packages_files['file_id'] ) );
63
		$this->assertEquals( $jetpack_packages_files['file_id']['version'], '2' );
64
		$this->assertEquals( $jetpack_packages_files['file_id']['path'], 'path_to_file_v2' );
65
	}
66
67
	/**
68
	 * Tests that dev version is added to array.
69
	 */
70 View Code Duplication
	public function test_enqueueing_always_adds_the_dev_version_to_the_global_array() {
71
72
		Autoloader\enqueue_package_file( 'file_id', '1', 'path_to_file' );
73
		Autoloader\enqueue_package_file( 'file_id', 'dev-howdy', 'path_to_file_dev' );
74
		Autoloader\enqueue_package_file( 'file_id', '2', 'path_to_file_v2' );
75
76
		global $jetpack_packages_files;
77
		$this->assertTrue( isset( $jetpack_packages_files['file_id'] ) );
78
		$this->assertEquals( $jetpack_packages_files['file_id']['version'], 'dev-howdy' );
79
		$this->assertEquals( $jetpack_packages_files['file_id']['path'], 'path_to_file_dev' );
80
	}
81
82
	/**
83
	 * Tests that a file is loaded.
84
	 */
85
	public function test_enqueued_file_is_actually_loaded() {
86
87
		Autoloader\enqueue_package_file( 'file_id', '1', __DIR__ . '/path_to_file.php' );
88
89
		Autoloader\file_loader();
90
		$this->assertTrue( function_exists( 'if_i_exist_then_this_test_passed' ) );
91
		$this->assertTrue( if_i_exist_then_this_test_passed() );
92
93
		Autoloader\enqueue_package_file( 'file_id', '2', __DIR__ . '/bogus_path_to_file.php' );
94
95
		Autoloader\file_loader(); // file_loader should not include same file twice.
96
97
		$this->assertTrue( if_i_exist_then_this_test_passed() );
98
	}
99
}
100