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

Test_Latest_Autoloader_Guard::set_up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * Autoloader guard test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * Test suite class for the Autoloader part responsible for ensuring only the latest autoloader is ever executed.
11
 */
12
class Test_Latest_Autoloader_Guard extends TestCase {
13
14
	/**
15
	 * The mock Plugins_Handler instance.
16
	 *
17
	 * @var Plugins_Handler|\PHPUnit\Framework\MockObject\MockObject
18
	 */
19
	private $plugins_handler;
20
21
	/**
22
	 * The mock Autoloader_Handler instance.
23
	 *
24
	 * @var Autoloader_Handler|\PHPUnit\Framework\MockObject\MockObject
25
	 */
26
	private $autoloader_handler;
27
28
	/**
29
	 * The mock Autoloader_Locator instance.
30
	 *
31
	 * @var Autoloader_Locator|\PHPUnit\Framework\MockObject\MockObject
32
	 */
33
	private $autoloader_locator;
34
35
	/**
36
	 * The class we're testing.
37
	 *
38
	 * @var Latest_Autoloader_Guard
39
	 */
40
	private $guard;
41
42
	/**
43
	 * Setup runs before each test.
44
	 *
45
	 * @before
46
	 */
47
	public function set_up() {
48
		$this->plugins_handler    = $this->getMockBuilder( Plugins_Handler::class )
49
			->disableOriginalConstructor()
50
			->getMock();
51
		$this->autoloader_handler = $this->getMockBuilder( Autoloader_Handler::class )
52
			->disableOriginalConstructor()
53
			->getMock();
54
		$this->autoloader_locator = $this->getMockBuilder( Autoloader_Locator::class )
55
			->disableOriginalConstructor()
56
			->getMock();
57
58
		$this->guard = new Latest_Autoloader_Guard(
59
			$this->plugins_handler,
60
			$this->autoloader_handler,
61
			$this->autoloader_locator
62
		);
63
	}
64
65
	/**
66
	 * Tests that the guard stops initialization when the autoloader has already initialized.
67
	 *
68
	 * @runInSeparateProcess
69
	 * @preserveGlobalState disabled
70
	 */
71
	public function test_should_stop_init_when_autoloader_already_initialized() {
72
		global $jetpack_autoloader_latest_version;
73
		$jetpack_autoloader_latest_version = '2.0.0.0';
74
75
		$this->assertTrue(
76
			$this->guard->should_stop_init(
77
				TEST_DATA_PATH . '/plugins/plugin_current',
78
				array()
79
			)
80
		);
81
	}
82
83
	/**
84
	 * Tests that the guard stops initialization when not the latest autoloader.
85
	 */
86
	public function test_should_stop_init_when_not_latest_autoloader() {
87
		$this->plugins_handler->method( 'have_plugins_changed' )
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Plugins_Handler>.

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...
88
			->with( array() )
89
			->willReturn( true );
90
		$this->autoloader_locator->method( 'find_latest_autoloader' )
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Autoloader_Locator>.

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...
91
			->willReturn( TEST_DATA_PATH . '/plugins/dummy_current' );
92
		$this->autoloader_locator->method( 'get_autoloader_path' )
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Autoloader_Locator>.

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...
93
			->willReturn( TEST_DATA_PATH . '/plugins/dummy_current/dummy_current.php' );
94
95
		$this->assertTrue(
96
			$this->guard->should_stop_init(
97
				TEST_DATA_PATH . '/plugins/dummy_newer',
98
				array()
99
			)
100
		);
101
	}
102
103
	/**
104
	 * Tests that the guard allows initialization when the latest.
105
	 */
106
	public function test_should_allow_init_when_latest() {
107
		$this->plugins_handler->method( 'have_plugins_changed' )
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Plugins_Handler>.

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...
108
			->with( array() )
109
			->willReturn( true );
110
		$this->autoloader_locator->method( 'find_latest_autoloader' )
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Autoloader_Locator>.

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...
111
			->willReturn( null );
112
113
		$this->assertFalse(
114
			$this->guard->should_stop_init(
115
				TEST_DATA_PATH . '/plugins/plugin_current',
116
				array()
117
			)
118
		);
119
	}
120
121
	/**
122
	 * Tests that the guard resets when plugins have changed.
123
	 */
124
	public function test_should_stop_init_should_reset_when_plugins_change() {
125
		$this->plugins_handler->method( 'have_plugins_changed' )
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Plugins_Handler>.

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...
126
			->with( array() )
127
			->willReturn( true );
128
		$this->autoloader_handler->expects( $this->once() )->method( 'reset_autoloader' );
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Autoloader_Handler>.

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...
129
		$this->autoloader_locator->method( 'find_latest_autoloader' )
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Autoloader_Locator>.

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...
130
			->willReturn( null );
131
132
		$this->assertFalse(
133
			$this->guard->should_stop_init(
134
				TEST_DATA_PATH . '/plugins/plugin_current',
135
				array()
136
			)
137
		);
138
	}
139
}
140