Completed
Push — add/anchor-fm-badge-insertion ( 367060...0b7190 )
by
unknown
180:57 queued 171:53
created

Test_Path_Processor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 36.96 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 34
loc 92
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A test_handles_path_tokenization_and_untokenization() 9 9 1
A test_handles_path_tokenization_and_untokenization_with_windows_paths() 9 9 1
A test_does_not_find_directory_for_non_php_files() 0 8 1
A test_does_not_find_directory_for_not_autoloaded_plugin() 0 8 1
A test_finds_directory_for_autoloaded_plugin() 8 8 1
A test_finds_directory_for_autoloaded_plugin_with_windows_paths() 8 8 1
A set_up() 0 3 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
 * Path processor 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 processing paths.
12
 */
13
class Test_Path_Processor extends TestCase {
14
15
	/**
16
	 * The path processor we're testing.
17
	 *
18
	 * @var Path_Processor
19
	 */
20
	private $processor;
21
22
	/**
23
	 * Setup runs before each test.
24
	 *
25
	 * @before
26
	 */
27
	public function set_up() {
28
		$this->processor = new Path_Processor();
29
	}
30
31
	/**
32
	 * Tests that the process is able to successfully tokenize and untokenize paths.
33
	 */
34 View Code Duplication
	public function test_handles_path_tokenization_and_untokenization() {
35
		$path = $this->processor->tokenize_path_constants( WP_PLUGIN_DIR . '/test/path' );
36
37
		$this->assertEquals( '{{WP_PLUGIN_DIR}}/test/path', $path );
38
39
		$path = $this->processor->untokenize_path_constants( $path );
40
41
		$this->assertEquals( WP_PLUGIN_DIR . '/test/path', $path );
42
	}
43
44
	/**
45
	 * Tests that find_directory_with_autoloader is able to successfully tokenize and untokenize paths on Windows.
46
	 */
47 View Code Duplication
	public function test_handles_path_tokenization_and_untokenization_with_windows_paths() {
48
		$path = $this->processor->tokenize_path_constants( WP_PLUGIN_DIR . '/test/path' );
49
50
		$this->assertEquals( '{{WP_PLUGIN_DIR}}/test/path', $path );
51
52
		$path = $this->processor->untokenize_path_constants( $path );
53
54
		$this->assertEquals( WP_PLUGIN_DIR . '/test/path', $path );
55
	}
56
57
	/**
58
	 * Tests that find_directory_with_autoloader does not work on non-PHP files.
59
	 */
60
	public function test_does_not_find_directory_for_non_php_files() {
61
		$path = $this->processor->find_directory_with_autoloader(
62
			'dummy_current/dummy-file.test',
63
			array( TEST_DATA_PATH . '/plugins' )
64
		);
65
66
		$this->assertFalse( $path );
67
	}
68
69
	/**
70
	 * Tests that find_directory_with_autoloader does not work for files that don't have the autoloader.
71
	 */
72
	public function test_does_not_find_directory_for_not_autoloaded_plugin() {
73
		$path = $this->processor->find_directory_with_autoloader(
74
			'file-plugin.php',
75
			array( TEST_DATA_PATH . '/plugins' )
76
		);
77
78
		$this->assertFalse( $path );
79
	}
80
81
	/**
82
	 * Tests that find_directory_with_autoloader finds directories for plugins that have the autoloader.
83
	 */
84 View Code Duplication
	public function test_finds_directory_for_autoloaded_plugin() {
85
		$path = $this->processor->find_directory_with_autoloader(
86
			'dummy_current/dummy_current.php',
87
			array( TEST_DATA_PATH . '/plugins' )
88
		);
89
90
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current', $path );
91
	}
92
93
	/**
94
	 * Tests that find_directory_with_autoloader finds directories using Windows paths.
95
	 */
96 View Code Duplication
	public function test_finds_directory_for_autoloaded_plugin_with_windows_paths() {
97
		$path = $this->processor->find_directory_with_autoloader(
98
			'dummy_current\dummy_current.php',
99
			array( WP_PLUGIN_DIR )
100
		);
101
102
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current', $path );
103
	}
104
}
105