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

Test_Manifest_Reader::set_up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * File loader 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 part that handles file loading.
13
 */
14
class Test_Manifest_Reader extends TestCase {
15
16
	/**
17
	 * A mock of the version selector used by the reader.
18
	 *
19
	 * @var Version_Selector|\PHPUnit\Framework\MockObject\MockObject
20
	 */
21
	private $version_selector;
22
23
	/**
24
	 * The manifest reader we're testing.
25
	 *
26
	 * @var Manifest_Reader
27
	 */
28
	private $reader;
29
30
	/**
31
	 * Setup runs before each test.
32
	 *
33
	 * @before
34
	 */
35
	public function set_up() {
36
		$this->version_selector = $this->getMockBuilder( Version_Selector::class )
37
			->disableOriginalConstructor()
38
			->getMock();
39
		$this->reader           = new Manifest_Reader( $this->version_selector );
40
	}
41
42
	/**
43
	 * Tests that nothing is read without any plugins.
44
	 */
45
	public function test_reads_nothing_without_plugins() {
46
		$input_array = array();
47
48
		$this->reader->read_manifests(
49
			array(),
50
			'vendor/composer/jetpack_autoload_classmap.php',
51
			$input_array
52
		);
53
54
		$this->assertEmpty( $input_array );
55
	}
56
57
	/**
58
	 * Tests that nothing is read for plugins that have no manifest.
59
	 */
60
	public function test_reads_nothing_for_plugins_without_manifests() {
61
		$input_array = array();
62
63
		$this->reader->read_manifests(
64
			array(),
65
			'vendor/composer/jetpack_autoload_classmap.php',
66
			$input_array
67
		);
68
69
		$this->assertEmpty( $input_array );
70
	}
71
72
	/**
73
	 * Tests that a single plugin manifest can be read successfully.
74
	 */
75
	public function test_reads_single_plugin_manifest() {
76
		$input_array = array();
77
78
		$this->version_selector->expects( $this->exactly( 2 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Version_Selector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
			->method( 'is_version_update_required' )
80
			->withConsecutive(
81
				array( null, '2.0.0.0' ),
82
				array( null, '1.0.0.0' )
83
			)
84
			->willReturnOnConsecutiveCalls(
85
				true,
86
				true
87
			);
88
89
		$this->reader->read_manifests(
90
			array( TEST_DATA_PATH . '/plugins/dummy_current' ),
91
			'vendor/composer/jetpack_autoload_classmap.php',
92
			$input_array
93
		);
94
95
		$this->assertArrayHasKey( Test::class, $input_array );
96
		$this->assertEquals( '1.0.0.0', $input_array[ Test::class ]['version'] );
97
		$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/dummy_current/includes/class-test.php' );
98
	}
99
100
	/**
101
	 * Tests that the reader only keeps the latest version when processing multiple manifests.
102
	 */
103 View Code Duplication
	public function test_read_overwrites_older_version_in_manifest() {
104
		$input_array = array();
105
106
		$this->version_selector->expects( $this->exactly( 4 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Version_Selector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
			->method( 'is_version_update_required' )
108
			->withConsecutive(
109
				array( null, '2.0.0.0' ),
110
				array( null, '1.0.0.0' ),
111
				array( '2.0.0.0', '2.2.0.0' ),
112
				array( '1.0.0.0', '2.0.0.0' )
113
			)
114
			->willReturnOnConsecutiveCalls(
115
				true,
116
				true,
117
				true,
118
				true
119
			);
120
121
		$this->reader->read_manifests(
122
			array(
123
				TEST_DATA_PATH . '/plugins/dummy_current',
124
				TEST_DATA_PATH . '/plugins/dummy_newer',
125
			),
126
			'vendor/composer/jetpack_autoload_classmap.php',
127
			$input_array
128
		);
129
130
		$this->assertArrayHasKey( Test::class, $input_array );
131
		$this->assertEquals( '2.0.0.0', $input_array[ Test::class ]['version'] );
132
		$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/dummy_newer/includes/class-test.php' );
133
	}
134
135
	/**
136
	 * Tests that the reader ignores older versions when a newer version is already set.
137
	 */
138 View Code Duplication
	public function test_read_ignores_older_version_when_newer_already_loaded() {
139
		$input_array = array();
140
141
		$this->version_selector->expects( $this->exactly( 4 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Version_Selector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
			->method( 'is_version_update_required' )
143
			->withConsecutive(
144
				array( null, '2.2.0.0' ),
145
				array( null, '2.0.0.0' ),
146
				array( '2.2.0.0', '2.0.0.0' ),
147
				array( '2.0.0.0', '1.0.0.0' )
148
			)
149
			->willReturnOnConsecutiveCalls(
150
				true,
151
				true,
152
				false,
153
				false
154
			);
155
156
		$this->reader->read_manifests(
157
			array(
158
				TEST_DATA_PATH . '/plugins/dummy_newer',
159
				TEST_DATA_PATH . '/plugins/dummy_current',
160
			),
161
			'vendor/composer/jetpack_autoload_classmap.php',
162
			$input_array
163
		);
164
165
		$this->assertArrayHasKey( Test::class, $input_array );
166
		$this->assertEquals( '2.0.0.0', $input_array[ Test::class ]['version'] );
167
		$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/dummy_newer/includes/class-test.php' );
168
	}
169
}
170