Completed
Push — fix/admin-menu-api-capabilitie... ( 188fee...28a29e )
by Jeremy
440:49 queued 432:35
created

Test_Version_Loader::test_loads_filemap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * Class loader test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use \Jetpack\AutoloaderTestData\Plugin\Psr4\Test as Psr4Test;
9
use \Jetpack\AutoloaderTestData\Plugin\Test;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Test.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * Test suite class for the Autoloader part that handles file loading.
14
 */
15
class Test_Version_Loader extends TestCase {
16
17
	/**
18
	 * Tests that `find_class_file` returns null when the given class is not known.
19
	 */
20
	public function test_find_class_file_returns_null_for_unknown_class() {
21
		$version_loader = new Version_Loader( new Version_Selector(), null, null, null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
22
23
		$file_path = $version_loader->find_class_file( Test::class );
24
25
		$this->assertNull( $file_path );
26
	}
27
28
	/**
29
	 * Tests that `find_class_file` returns the path to the class when present in the classmap.
30
	 */
31 View Code Duplication
	public function test_find_class_file_returns_path_for_classmap() {
32
		$version_loader = new Version_Loader(
33
			new Version_Selector(),
34
			array(
35
				Test::class => array(
36
					'version' => '1.0.0.0',
37
					'path'    => TEST_DATA_PATH . '/plugins/dummy_current/includes/class-test.php',
38
				),
39
			),
40
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
		);
43
44
		$file_path = $version_loader->find_class_file( Test::class );
45
46
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current/includes/class-test.php', $file_path );
47
	}
48
49
	/**
50
	 * Test that `find_class_file` returns the path to the class when present in the PSR-4 map.
51
	 */
52 View Code Duplication
	public function test_find_class_file_returns_path_for_psr4() {
53
		$version_loader = new Version_Loader(
54
			new Version_Selector(),
55
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
			array(
57
				'Jetpack\\AutoloaderTestData\\Plugin\\' => array(
58
					'version' => '1.0.0.0',
59
					'path'    => array( TEST_DATA_PATH . '/plugins/dummy_current/src' ),
60
				),
61
			),
62
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
		);
64
65
		$file_path = $version_loader->find_class_file( Psr4Test::class );
66
67
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current/src/Psr4/Test.php', $file_path );
68
	}
69
70
	/**
71
	 * Tests that `find_class_file` returns the path to the class when presented
72
	 * with less-specific namespaces first in the PSR-4 map.
73
	 */
74 View Code Duplication
	public function test_find_class_file_checks_returns_path_for_psr4_with_less_specific_namespace() {
75
		$version_loader = new Version_Loader(
76
			new Version_Selector(),
77
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
			array(
79
				'Jetpack\\AutoloaderTestData\\'         => array(
80
					'version' => '1.0.0.0',
81
					'path'    => array( TEST_DATA_PATH . '/plugins/dummy_current' ),
82
				),
83
				'Jetpack\\AutoloaderTestData\\Plugin\\' => array(
84
					'version' => '1.0.0.0',
85
					'path'    => array( TEST_DATA_PATH . '/plugins/dummy_current/src' ),
86
				),
87
			),
88
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
		);
90
91
		$file_path = $version_loader->find_class_file( Psr4Test::class );
92
93
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current/src/Psr4/Test.php', $file_path );
94
	}
95
96
	/**
97
	 * Test that `find_class_file` returns the classmap version when newer.
98
	 */
99 View Code Duplication
	public function test_find_class_file_returns_newer_classmap() {
100
		$version_loader = new Version_Loader(
101
			new Version_Selector(),
102
			array(
103
				Psr4Test::class => array(
104
					'version' => '2.0.0.0',
105
					'path'    => TEST_DATA_PATH . '/plugins/dummy_newer/src/Psr4/Test.php',
106
				),
107
			),
108
			array(
109
				'Jetpack\\AutoloaderTestData\\Plugin\\' => array(
110
					'version' => '1.0.0.0',
111
					'path'    => array( TEST_DATA_PATH . '/plugins/dummy_current/src' ),
112
				),
113
			),
114
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
		);
116
117
		$file_path = $version_loader->find_class_file( Psr4Test::class );
118
119
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_newer/src/Psr4/Test.php', $file_path );
120
	}
121
122
	/**
123
	 * Test that `find_class_file` returns the PSR-4 version when newer.
124
	 */
125 View Code Duplication
	public function test_find_class_file_returns_newer_psr4() {
126
		$version_loader = new Version_Loader(
127
			new Version_Selector(),
128
			array(
129
				Psr4Test::class => array(
130
					'version' => '1.0.0.0',
131
					'path'    => TEST_DATA_PATH . '/plugins/dummy_current/src/Psr4/Test.php',
132
				),
133
			),
134
			array(
135
				'Jetpack\\AutoloaderTestData\\Plugin\\' => array(
136
					'version' => '2.0.0.0',
137
					'path'    => array( TEST_DATA_PATH . '/plugins/dummy_newer/src' ),
138
				),
139
			),
140
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
		);
142
143
		$file_path = $version_loader->find_class_file( Psr4Test::class );
144
145
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_newer/src/Psr4/Test.php', $file_path );
146
	}
147
148
	/**
149
	 * Tests that `load_filemap` correctly loads all of the files.
150
	 *
151
	 * @preserveGlobalState disabled
152
	 * @runInSeparateProcess
153
	 */
154
	public function test_loads_filemap() {
155
		$version_loader = new Version_Loader(
156
			new Version_Selector(),
157
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
			array(
160
				'123456acbdefg' => array(
161
					'version' => '1.0.0.0',
162
					'path'    => TEST_DATA_PATH . '/plugins/dummy_current/includes/functions.php',
163
				),
164
			)
165
		);
166
167
		$version_loader->load_filemap();
168
169
		$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] );
170
		$this->assertTrue( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginCurrent\\if_i_exist_then_this_test_passed' ) );
171
	}
172
173
	/**
174
	 * Tests that `load_filemap` does not load files that have already been loaded.
175
	 */
176
	public function test_loads_filemap_skips_existing_files() {
177
		$version_loader = new Version_Loader(
178
			new Version_Selector(),
179
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
180
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
			array(
182
				'123456acbdefg' => array(
183
					'version' => '1.0.0.0',
184
					'path'    => TEST_DATA_PATH . '/plugins/dummy_current/includes/functions.php',
185
				),
186
			)
187
		);
188
189
		// Pretend it was already loaded!
190
		$GLOBALS['__composer_autoload_files']['123456acbdefg'] = true;
191
192
		$version_loader->load_filemap();
193
194
		$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] );
195
		$this->assertFalse( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginCurrent\\if_i_exist_then_this_test_passed' ) );
196
	}
197
}
198