1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* Shutdown 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 Shutdown_Handler. |
15
|
|
|
*/ |
16
|
|
|
class ShutdownHandlerTest extends TestCase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The mock Plugins_Handler instance. |
20
|
|
|
* |
21
|
|
|
* @var Plugins_Handler|\PHPUnit\Framework\MockObject\MockObject |
22
|
|
|
*/ |
23
|
|
|
private $plugins_handler; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Setup runs before each test. |
27
|
|
|
* |
28
|
|
|
* @before |
29
|
|
|
*/ |
30
|
|
|
public function set_up() { |
31
|
|
|
$this->plugins_handler = $this->getMockBuilder( Plugins_Handler::class ) |
32
|
|
|
->disableOriginalConstructor() |
33
|
|
|
->getMock(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Tests that the shutdown handler caches the active plugins. |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function test_shutdown_caches_active_plugins() { |
40
|
|
|
$this->plugins_handler->expects( $this->once() ) |
41
|
|
|
->method( 'get_active_plugins' ) |
42
|
|
|
->with( false, true ) |
43
|
|
|
->willReturn( array( TEST_PLUGIN_DIR ) ); |
44
|
|
|
$this->plugins_handler->expects( $this->once() ) |
45
|
|
|
->method( 'cache_plugins' ) |
46
|
|
|
->with( array( TEST_PLUGIN_DIR ) ); |
47
|
|
|
|
48
|
|
|
// Mark that the plugins have been loaded so that we can perform a safe shutdown. |
49
|
|
|
do_action( 'plugins_loaded' ); |
50
|
|
|
|
51
|
|
|
$handler = new Shutdown_Handler( $this->plugins_handler, array(), false ); |
52
|
|
|
$handler(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Tests that the shutdown handler does not update the cache if it has not changed. |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function test_shutdown_does_not_save_unchanged_cache() { |
59
|
|
|
$this->plugins_handler->expects( $this->once() ) |
60
|
|
|
->method( 'get_active_plugins' ) |
61
|
|
|
->with( false, true ) |
62
|
|
|
->willReturn( array( TEST_PLUGIN_DIR ) ); |
63
|
|
|
$this->plugins_handler->expects( $this->never() ) |
64
|
|
|
->method( 'cache_plugins' ); |
65
|
|
|
|
66
|
|
|
// Mark that the plugins have been loaded so that we can perform a safe shutdown. |
67
|
|
|
do_action( 'plugins_loaded' ); |
68
|
|
|
|
69
|
|
|
$handler = new Shutdown_Handler( $this->plugins_handler, array( TEST_PLUGIN_DIR ), false ); |
70
|
|
|
$handler(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Tests that shutting down early empties the cache. |
75
|
|
|
*/ |
76
|
|
|
public function test_shutdown_early_empties_cache() { |
77
|
|
|
$this->plugins_handler->expects( $this->once() ) |
78
|
|
|
->method( 'cache_plugins' ) |
79
|
|
|
->with( array() ); |
80
|
|
|
|
81
|
|
|
// Do NOT run plugins_loaded so that the shutdown can be considered early. |
82
|
|
|
|
83
|
|
|
$handler = new Shutdown_Handler( $this->plugins_handler, array( TEST_PLUGIN_DIR ), false ); |
84
|
|
|
$handler(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tests that expected exceptions thrown during shutdown aren't propogated. |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function test_shutdown_handles_exceptions() { |
91
|
|
|
$this->plugins_handler->expects( $this->once() ) |
92
|
|
|
->method( 'get_active_plugins' ) |
93
|
|
|
->with( false, true ) |
94
|
|
|
->willThrowException( new \RuntimeException() ); |
95
|
|
|
$this->plugins_handler->expects( $this->once() ) |
96
|
|
|
->method( 'cache_plugins' ) |
97
|
|
|
->with( array() ); |
98
|
|
|
|
99
|
|
|
// Mark that the plugins have been loaded so that we can perform a safe shutdown. |
100
|
|
|
do_action( 'plugins_loaded' ); |
101
|
|
|
|
102
|
|
|
$handler = new Shutdown_Handler( $this->plugins_handler, array( TEST_PLUGIN_DIR ), false ); |
103
|
|
|
$handler(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|