Completed
Push — update/calendly-embed-code ( 001c02...b82934 )
by
unknown
08:51
created

WP_Test_File_Loader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 45.16 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 28
loc 62
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
2
3
use Automattic\Jetpack\Autoloader as Autoloader;
4
use PHPUnit\Framework\TestCase;
5
6
class WP_Test_File_Loader extends TestCase {
7
	static $jetpack_packages_files;
8
	function setup() {
9
		parent::setup();
10
		global $jetpack_packages_files;
11
		self::$jetpack_packages_files = $jetpack_packages_files;
12
		$jetpack_packages_files = array();
13
	}
14
15
	function tearDown() {
16
		parent::tearDown();
17
		// re-apply the global
18
		global $jetpack_packages_files;
19
		$jetpack_packages_files = self::$jetpack_packages_files;
20
	}
21
22 View Code Duplication
	function test_enqueueing_adds_to_the_global_array() {
23
		Autoloader\enqueue_package_file( 'file_id_10', '1', 'path_to_file.php' );
24
25
		global $jetpack_packages_files;
26
		$this->assertTrue( isset( $jetpack_packages_files['file_id_10'] ) );
27
		$this->assertEquals( $jetpack_packages_files['file_id_10']['version'], '1' );
28
		$this->assertEquals( $jetpack_packages_files['file_id_10']['path'], 'path_to_file.php' );
29
	}
30
31 View Code Duplication
	function test_enqueueing_adds_the_latest_version_to_the_global_array() {
32
		Autoloader\enqueue_package_file( 'file_id', '1', 'path_to_file' );
33
		Autoloader\enqueue_package_file( 'file_id', '2', 'path_to_file_v2' );
34
35
		global $jetpack_packages_files;
36
		$this->assertTrue( isset( $jetpack_packages_files['file_id'] ) );
37
		$this->assertEquals( $jetpack_packages_files['file_id']['version'], '2' );
38
		$this->assertEquals( $jetpack_packages_files['file_id']['path'], 'path_to_file_v2' );
39
	}
40
41 View Code Duplication
	function test_enqueueing_always_adds_the_dev_version_to_the_global_array() {
42
43
		Autoloader\enqueue_package_file( 'file_id', '1', 'path_to_file' );
44
		Autoloader\enqueue_package_file( 'file_id', 'dev-howdy', 'path_to_file_dev' );
45
		Autoloader\enqueue_package_file( 'file_id', '2', 'path_to_file_v2' );
46
47
		global $jetpack_packages_files;
48
		$this->assertTrue( isset( $jetpack_packages_files['file_id'] ) );
49
		$this->assertEquals( $jetpack_packages_files['file_id']['version'], 'dev-howdy' );
50
		$this->assertEquals( $jetpack_packages_files['file_id']['path'], 'path_to_file_dev' );
51
	}
52
53
	function test_enqueued_file_is_actually_loaded() {
54
55
		Autoloader\enqueue_package_file( 'file_id', '1', __DIR__ . '/path_to_file.php' );
56
57
		Autoloader\file_loader();
58
		$this->assertTrue( function_exists( 'if_i_exist_then_this_test_passed' ) );
59
		$this->assertTrue( if_i_exist_then_this_test_passed() );
60
61
		Autoloader\enqueue_package_file( 'file_id', '2', __DIR__ . '/bogus_path_to_file.php' );
62
63
		Autoloader\file_loader(); // file_loader should not include same file twice.
64
65
		$this->assertTrue( if_i_exist_then_this_test_passed() );
66
	}
67
}
68