Completed
Push — fix/useBlockEditContext-usage ( 1e6b8b...d10938 )
by
unknown
199:36 queued 191:34
created

WP_Test_File_Loader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 39.73 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 29
loc 73
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 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_adds_the_dev_version_to_the_global_array() 12 12 2
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
 * 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
		$this->files_handler = new Files_Handler( new Plugins_Handler(), new Version_Selector() );
21
	}
22
23
	/**
24
	 * Tests whether enqueueing adds a file to the global array.
25
	 */
26 View Code Duplication
	public function test_enqueueing_adds_to_the_global_array() {
27
		$this->files_handler->enqueue_package_file( 'file_id_10', '1', 'path_to_file.php' );
28
29
		global $jetpack_packages_filemap;
30
		$this->assertTrue( isset( $jetpack_packages_filemap['file_id_10'] ) );
31
		$this->assertEquals( $jetpack_packages_filemap['file_id_10']['version'], '1' );
32
		$this->assertEquals( $jetpack_packages_filemap['file_id_10']['path'], 'path_to_file.php' );
33
	}
34
35
	/**
36
	 * Tests whether enqueueing adds the latest file version to the global array.
37
	 */
38 View Code Duplication
	public function test_enqueueing_adds_the_latest_version_to_the_global_array() {
39
		$this->files_handler->enqueue_package_file( 'file_id', '1', 'path_to_file' );
40
		$this->files_handler->enqueue_package_file( 'file_id', '2', 'path_to_file_v2' );
41
42
		global $jetpack_packages_filemap;
43
		$this->assertTrue( isset( $jetpack_packages_filemap['file_id'] ) );
44
		$this->assertEquals( $jetpack_packages_filemap['file_id']['version'], '2' );
45
		$this->assertEquals( $jetpack_packages_filemap['file_id']['path'], 'path_to_file_v2' );
46
	}
47
48
	/**
49
	 * Tests whether enqueueing prioritizes the dev version of the file when
50
	 * JETPACK_AUTOLOAD_DEV is set to true.
51
	 *
52
	 * @runInSeparateProcess
53
	 * @preserveGlobalState disabled
54
	 */
55 View Code Duplication
	public function test_enqueueing_adds_the_dev_version_to_the_global_array() {
56
		defined( 'JETPACK_AUTOLOAD_DEV' ) || define( 'JETPACK_AUTOLOAD_DEV', true );
57
58
		$this->files_handler->enqueue_package_file( 'file_id', '1', 'path_to_file' );
59
		$this->files_handler->enqueue_package_file( 'file_id', 'dev-howdy', 'path_to_file_dev' );
60
		$this->files_handler->enqueue_package_file( 'file_id', '2', 'path_to_file_v2' );
61
62
		global $jetpack_packages_filemap;
63
		$this->assertTrue( isset( $jetpack_packages_filemap['file_id'] ) );
64
		$this->assertEquals( $jetpack_packages_filemap['file_id']['version'], 'dev-howdy' );
65
		$this->assertEquals( $jetpack_packages_filemap['file_id']['path'], 'path_to_file_dev' );
66
	}
67
68
	/**
69
	 * Tests whether enqueueing works with autoloading.
70
	 */
71
	public function test_enqueued_file_is_actually_loaded() {
72
73
		$this->files_handler->enqueue_package_file( 'file_id', '1', __DIR__ . '/path_to_file.php' );
74
75
		$this->files_handler->file_loader();
76
		$this->assertTrue( function_exists( 'if_i_exist_then_this_test_passed' ) );
77
		$this->assertTrue( if_i_exist_then_this_test_passed() );
78
79
		$this->files_handler->enqueue_package_file( 'file_id', '2', __DIR__ . '/bogus_path_to_file.php' );
80
81
		$this->files_handler->file_loader(); // file_loader should not include same file twice.
82
83
		$this->assertTrue( if_i_exist_then_this_test_passed() );
84
	}
85
}
86