Completed
Push — try/find_latest_autoloader ( 4ce307...807460 )
by
unknown
06:53
created

WP_Test_File_Loader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 40.58 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 28
loc 69
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 5 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.NotHyphenatedLowerCase
2
/**
3
 * File loader test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * Test suite class for the Autoloader part that handles file loading.
12
 */
13
class WP_Test_File_Loader extends TestCase {
14
15
	/**
16
	 * Setup runs before each test.
17
	 */
18
	public function setup() {
19
		parent::setup();
20
21
		spl_autoload_register( 'autoloader' );
22
	}
23
24
	/**
25
	 * Tests whether enqueueing adds a file to the global array.
26
	 */
27 View Code Duplication
	public function test_enqueueing_adds_to_the_global_array() {
28
		enqueue_package_file( 'file_id_10', '1', 'path_to_file.php' );
29
30
		global $jetpack_packages_filemap;
31
		$this->assertTrue( isset( $jetpack_packages_filemap['file_id_10'] ) );
32
		$this->assertEquals( $jetpack_packages_filemap['file_id_10']['version'], '1' );
33
		$this->assertEquals( $jetpack_packages_filemap['file_id_10']['path'], 'path_to_file.php' );
34
	}
35
36
	/**
37
	 * Tests whether enqueueing adds the latest file version to the global array.
38
	 */
39 View Code Duplication
	public function test_enqueueing_adds_the_latest_version_to_the_global_array() {
40
		enqueue_package_file( 'file_id', '1', 'path_to_file' );
41
		enqueue_package_file( 'file_id', '2', 'path_to_file_v2' );
42
43
		global $jetpack_packages_filemap;
44
		$this->assertTrue( isset( $jetpack_packages_filemap['file_id'] ) );
45
		$this->assertEquals( $jetpack_packages_filemap['file_id']['version'], '2' );
46
		$this->assertEquals( $jetpack_packages_filemap['file_id']['path'], 'path_to_file_v2' );
47
	}
48
49
	/**
50
	 * Tests whether enqueueing prioritizes the dev version of the file.
51
	 */
52 View Code Duplication
	public function test_enqueueing_always_adds_the_dev_version_to_the_global_array() {
53
54
		enqueue_package_file( 'file_id', '1', 'path_to_file' );
55
		enqueue_package_file( 'file_id', 'dev-howdy', 'path_to_file_dev' );
56
		enqueue_package_file( 'file_id', '2', 'path_to_file_v2' );
57
58
		global $jetpack_packages_filemap;
59
		$this->assertTrue( isset( $jetpack_packages_filemap['file_id'] ) );
60
		$this->assertEquals( $jetpack_packages_filemap['file_id']['version'], 'dev-howdy' );
61
		$this->assertEquals( $jetpack_packages_filemap['file_id']['path'], 'path_to_file_dev' );
62
	}
63
64
	/**
65
	 * Tests whether enqueueing works with autoloading.
66
	 */
67
	public function test_enqueued_file_is_actually_loaded() {
68
69
		enqueue_package_file( 'file_id', '1', __DIR__ . '/path_to_file.php' );
70
71
		file_loader();
72
		$this->assertTrue( function_exists( 'if_i_exist_then_this_test_passed' ) );
73
		$this->assertTrue( if_i_exist_then_this_test_passed() );
74
75
		enqueue_package_file( 'file_id', '2', __DIR__ . '/bogus_path_to_file.php' );
76
77
		file_loader(); // file_loader should not include same file twice.
78
79
		$this->assertTrue( if_i_exist_then_this_test_passed() );
80
	}
81
}
82