Completed
Push — update/phpunit-php-8 ( e34c58...05aa04 )
by
unknown
137:27 queued 129:12
created

WP_Test_Autoloader::test_autoloader_works()   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
 * Autoloader test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
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...
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Test suite class for the Autoloader.
13
 */
14
class WP_Test_Autoloader extends TestCase {
15
16
	/**
17
	 * Setup runs before each test.
18
	 *
19
	 * @before
20
	 */
21
	public function set_up() {
22
		spl_autoload_register( 'autoloader' );
23
	}
24
25
	/**
26
	 * Tests whether manifest registration works with autoloading.
27
	 */
28
	public function test_autoloader_works() {
29
		global $jetpack_autoloader_loader;
30
		$jetpack_autoloader_loader = new Version_Loader(
31
			new Version_Selector(),
32
			array(
33
				Test::class => array(
34
					'version' => '1.0.0.0',
35
					'path'    => TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php',
36
				),
37
			),
38
			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...
39
			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...
40
		);
41
42
		$class = new Test();
43
44
		$this->assertInstanceOf( Test::class, $class );
45
	}
46
}
47