Completed
Push — update/dialogue-support-speake... ( 9c26d6...fd60fa )
by
unknown
107:11 queued 95:18
created

PathProcessorTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 18
loc 126
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A set_up() 0 6 1
A tear_down() 0 4 1
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_path_untokenization_resolves_symlinks() 0 5 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() 0 8 1
A test_finds_directory_for_autoloaded_plugin_with_windows_paths() 0 8 1
A test_finds_directory_realpath_for_symlinked_plugin() 0 8 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
// We live in the namespace of the test autoloader to avoid many use statements.
9
namespace Automattic\Jetpack\Autoloader\jpCurrent;
10
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * Test suite class for the Autoloader part that handles processing paths.
15
 */
16
class PathProcessorTest extends TestCase {
17
18
	/**
19
	 * The path processor we're testing.
20
	 *
21
	 * @var Path_Processor
22
	 */
23
	private $processor;
24
25
	/**
26
	 * Setup runs before each test.
27
	 *
28
	 * @before
29
	 */
30
	public function set_up() {
31
		$this->processor = new Path_Processor();
32
33
		// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
34
		@symlink( TEST_PLUGIN_DIR, WP_PLUGIN_DIR . '/current_symlink' );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
35
	}
36
37
	/**
38
	 * Teardown runs after each test.
39
	 *
40
	 * @after
41
	 */
42
	public function tear_down() {
43
		// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
44
		@unlink( WP_PLUGIN_DIR . '/current_symlink' );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
	}
46
47
	/**
48
	 * Tests that the processor is able to successfully tokenize and untokenize paths.
49
	 */
50 View Code Duplication
	public function test_handles_path_tokenization_and_untokenization() {
51
		$path = $this->processor->tokenize_path_constants( TEST_PLUGIN_DIR );
52
53
		$this->assertEquals( '{{WP_PLUGIN_DIR}}/current', $path );
54
55
		$path = $this->processor->untokenize_path_constants( $path );
56
57
		$this->assertEquals( TEST_PLUGIN_DIR, $path );
58
	}
59
60
	/**
61
	 * Tests that the processor is able to successfully tokenize and untokenize paths on Windows.
62
	 */
63 View Code Duplication
	public function test_handles_path_tokenization_and_untokenization_with_windows_paths() {
64
		$path = $this->processor->tokenize_path_constants( str_replace( '/', '\\', TEST_PLUGIN_DIR ) );
65
66
		$this->assertEquals( '{{WP_PLUGIN_DIR}}/current', $path );
67
68
		$path = $this->processor->untokenize_path_constants( $path );
69
70
		$this->assertEquals( TEST_PLUGIN_DIR, $path );
71
	}
72
73
	/**
74
	 * Tests that the processor resolves symlinks when untokenizing.
75
	 */
76
	public function test_path_untokenization_resolves_symlinks() {
77
		$path = $this->processor->untokenize_path_constants( '{{WP_PLUGIN_DIR}}/current_symlink' );
78
79
		$this->assertEquals( TEST_PLUGIN_DIR, $path );
80
	}
81
82
	/**
83
	 * Tests that find_directory_with_autoloader does not work on non-PHP files.
84
	 */
85
	public function test_does_not_find_directory_for_non_php_files() {
86
		$path = $this->processor->find_directory_with_autoloader(
87
			'current/current.test',
88
			array( WP_PLUGIN_DIR )
89
		);
90
91
		$this->assertFalse( $path );
92
	}
93
94
	/**
95
	 * Tests that find_directory_with_autoloader does not work for files that don't have the autoloader.
96
	 */
97
	public function test_does_not_find_directory_for_not_autoloaded_plugin() {
98
		$path = $this->processor->find_directory_with_autoloader(
99
			'src/autoload.php',
100
			array( dirname( TEST_PLUGIN_DIR ) )
101
		);
102
103
		$this->assertFalse( $path );
104
	}
105
106
	/**
107
	 * Tests that find_directory_with_autoloader finds directories for plugins that have the autoloader.
108
	 */
109
	public function test_finds_directory_for_autoloaded_plugin() {
110
		$path = $this->processor->find_directory_with_autoloader(
111
			'current/current.php',
112
			array( WP_PLUGIN_DIR )
113
		);
114
115
		$this->assertEquals( TEST_PLUGIN_DIR, $path );
116
	}
117
118
	/**
119
	 * Tests that find_directory_with_autoloader finds directories using Windows paths.
120
	 */
121
	public function test_finds_directory_for_autoloaded_plugin_with_windows_paths() {
122
		$path = $this->processor->find_directory_with_autoloader(
123
			'current\\current.php',
124
			array( str_replace( '/', '\\', WP_PLUGIN_DIR ) )
125
		);
126
127
		$this->assertEquals( TEST_PLUGIN_DIR, $path );
128
	}
129
130
	/**
131
	 * Tests that find_directory_with_autoloader finds the realpath of directories that use symlinks.
132
	 */
133
	public function test_finds_directory_realpath_for_symlinked_plugin() {
134
		$path = $this->processor->find_directory_with_autoloader(
135
			'current_symlink\current.php',
136
			array( WP_PLUGIN_DIR )
137
		);
138
139
		$this->assertEquals( TEST_PLUGIN_DIR, $path );
140
	}
141
}
142