1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* Autoloader handler 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 handler. |
15
|
|
|
* |
16
|
|
|
* @runClassInSeparateProcess |
17
|
|
|
* @preserveGlobalState disabled |
18
|
|
|
*/ |
19
|
|
|
class AutoloaderHandlerTest extends TestCase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The php autoloader mock; |
23
|
|
|
* |
24
|
|
|
* @var PHP_Autoloader|\PHPUnit\Framework\MockObject\MockObject |
25
|
|
|
*/ |
26
|
|
|
private $php_autoloader; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The hook manager mock; |
30
|
|
|
* |
31
|
|
|
* @var Hook_Manager|\PHPUnit\Framework\MockObject\MockObject |
32
|
|
|
*/ |
33
|
|
|
private $hook_manager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The manifest reader mock. |
37
|
|
|
* |
38
|
|
|
* @var Manifest_Reader|\PHPUnit\Framework\MockObject\MockObject |
39
|
|
|
*/ |
40
|
|
|
private $manifest_reader; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The autoloader handler we're testing. |
44
|
|
|
* |
45
|
|
|
* @var Autoloader_Handler |
46
|
|
|
*/ |
47
|
|
|
private $autoloader_handler; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Setup runs before each test. |
51
|
|
|
* |
52
|
|
|
* @before |
53
|
|
|
*/ |
54
|
|
|
public function set_up() { |
55
|
|
|
$this->php_autoloader = $this->getMockBuilder( PHP_Autoloader::class ) |
56
|
|
|
->disableOriginalConstructor() |
57
|
|
|
->getMock(); |
58
|
|
|
$this->hook_manager = $this->getMockBuilder( Hook_Manager::class ) |
59
|
|
|
->disableOriginalConstructor() |
60
|
|
|
->getMock(); |
61
|
|
|
$this->manifest_reader = $this->getMockBuilder( Manifest_Reader::class ) |
62
|
|
|
->disableOriginalConstructor() |
63
|
|
|
->getMock(); |
64
|
|
|
$version_selector = $this->getMockBuilder( Version_Selector::class ) |
65
|
|
|
->disableOriginalConstructor() |
66
|
|
|
->getMock(); |
67
|
|
|
$this->autoloader_handler = new Autoloader_Handler( |
68
|
|
|
$this->php_autoloader, |
69
|
|
|
$this->hook_manager, |
70
|
|
|
$this->manifest_reader, |
71
|
|
|
$version_selector |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tests that the handler is able to activate the autoloader successfully. |
77
|
|
|
*/ |
78
|
|
|
public function test_activates_autoloader() { |
79
|
|
|
$plugins = array( TEST_PLUGIN_DIR ); |
80
|
|
|
|
81
|
|
|
$this->manifest_reader->expects( $this->exactly( 3 ) ) |
82
|
|
|
->method( 'read_manifests' ) |
83
|
|
|
->withConsecutive( |
84
|
|
|
array( $plugins, 'vendor/composer/jetpack_autoload_psr4.php' ), |
85
|
|
|
array( $plugins, 'vendor/composer/jetpack_autoload_classmap.php' ), |
86
|
|
|
array( $plugins, 'vendor/composer/jetpack_autoload_filemap.php' ) |
87
|
|
|
); |
88
|
|
|
$this->php_autoloader->expects( $this->once() ) |
89
|
|
|
->method( 'register_autoloader' ); |
90
|
|
|
|
91
|
|
|
$this->autoloader_handler->activate_autoloader( $plugins ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Tests that the handler is able to reset the autoloader successfully. |
96
|
|
|
*/ |
97
|
|
|
public function test_reset_autoloader() { |
98
|
|
|
global $jetpack_autoloader_loader; |
99
|
|
|
global $jetpack_autoloader_latest_version; |
100
|
|
|
|
101
|
|
|
$jetpack_autoloader_loader = 'test'; |
102
|
|
|
$jetpack_autoloader_latest_version = 'test'; |
103
|
|
|
$this->php_autoloader->expects( $this->once() ) |
104
|
|
|
->method( 'unregister_autoloader' ); |
105
|
|
|
$this->hook_manager->expects( $this->once() ) |
106
|
|
|
->method( 'reset' ); |
107
|
|
|
|
108
|
|
|
$this->autoloader_handler->reset_autoloader(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|