Completed
Push — fix/18996 ( 60942a )
by
unknown
69:20 queued 58:41
created

test_shutdown_handles_exceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
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
		( new Shutdown_Handler( $this->plugins_handler, array(), false ) )();
52
	}
53
54
	/**
55
	 * Tests that the shutdown handler does not update the cache if it has not changed.
56
	 */
57 View Code Duplication
	public function test_shutdown_does_not_save_unchanged_cache() {
58
		$this->plugins_handler->expects( $this->once() )
59
			->method( 'get_active_plugins' )
60
			->with( false, true )
61
			->willReturn( array( TEST_PLUGIN_DIR ) );
62
		$this->plugins_handler->expects( $this->never() )
63
			->method( 'cache_plugins' );
64
65
		// Mark that the plugins have been loaded so that we can perform a safe shutdown.
66
		do_action( 'plugins_loaded' );
67
68
		( new Shutdown_Handler( $this->plugins_handler, array( TEST_PLUGIN_DIR ), false ) )();
69
	}
70
71
	/**
72
	 * Tests that shutting down early empties the cache.
73
	 */
74
	public function test_shutdown_early_empties_cache() {
75
		$this->plugins_handler->expects( $this->once() )
76
			->method( 'cache_plugins' )
77
			->with( array() );
78
79
		// Do NOT run plugins_loaded so that the shutdown can be considered early.
80
81
		( new Shutdown_Handler( $this->plugins_handler, array( TEST_PLUGIN_DIR ), false ) )();
82
	}
83
84
	/**
85
	 * Tests that expected exceptions thrown during shutdown aren't propogated.
86
	 */
87 View Code Duplication
	public function test_shutdown_handles_exceptions() {
88
		$this->plugins_handler->expects( $this->once() )
89
			->method( 'get_active_plugins' )
90
			->with( false, true )
91
			->willThrowException( new \RuntimeException() );
92
		$this->plugins_handler->expects( $this->once() )
93
			->method( 'cache_plugins' )
94
			->with( array() );
95
96
		// Mark that the plugins have been loaded so that we can perform a safe shutdown.
97
		do_action( 'plugins_loaded' );
98
99
		( new Shutdown_Handler( $this->plugins_handler, array( TEST_PLUGIN_DIR ), false ) )();
100
	}
101
}
102