Completed
Push — fix/15025-map-block-infinite-s... ( 9a8b21...52d6c3 )
by
unknown
63:16 queued 56:01
created

Test_Plugin::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Unit tests for the Connection Plugin Manager class.
4
 *
5
 * @package automattic/jetpack-connection
6
 * @see \Automattic\Jetpack\Connection\Plugin
7
 */
8
9
namespace Automattic\Jetpack\Connection;
10
11
require_once __DIR__ . '/mock/trait-options.php';
12
require_once __DIR__ . '/mock/trait-hooks.php';
13
14
use Automattic\Jetpack\Connection\Test\Mock\Hooks;
15
use Automattic\Jetpack\Connection\Test\Mock\Options;
16
use phpmock\Mock;
17
use phpmock\MockEnabledException;
18
use PHPUnit\Framework\TestCase;
19
20
/**
21
 * Unit tests for the Connection Plugin Manager class.
22
 *
23
 * @see \Automattic\Jetpack\Connection\Plugin
24
 */
25
class Test_Plugin extends TestCase {
26
27
	use Options, Hooks;
28
29
	const PLUGIN_SLUG = 'sample-plugin-slug';
30
31
	const PLUGIN_NAME = 'Sample Plugin Name';
32
33
	/**
34
	 * Sample plugin arguments.
35
	 *
36
	 * @var array
37
	 */
38
	private $plugin_args = array(
39
		'url_info' => 'https://example.org/',
40
	);
41
42
	/**
43
	 * Initialization of the test class
44
	 *
45
	 * @throws MockEnabledException PHPUnit wasn't able to enable mock functions  ¯\_(⊙︿⊙)_/¯.
46
	 */
47
	protected function setUp() {
48
		parent::setUp();
49
50
		$this->build_mock_options();
51
		$this->build_mock_actions();
52
53
		$this->update_option->enable();
54
		$this->get_option->enable();
55
		$this->do_action->enable();
56
	}
57
58
	/**
59
	 * Clean up the test environment.
60
	 */
61
	protected function tearDown() {
62
		parent::tearDown();
63
64
		Mock::disableAll();
65
	}
66
67
	/**
68
	 * Unit test for the `Plugin::add()` method.
69
	 *
70
	 * @covers Automattic\Jetpack\Connection\Plugin::add
71
	 */
72
	public function test_add() {
73
		$plugin = new Plugin( self::PLUGIN_SLUG );
74
75
		$plugin->add( self::PLUGIN_NAME, $this->plugin_args + array( 'invalid_key' => 'value' ) );
76
77
		$this->assertEquals( array( 'name' => self::PLUGIN_NAME ) + $this->plugin_args, Plugin_Storage::get_one( self::PLUGIN_SLUG ) );
78
	}
79
80
	/**
81
	 * Unit test for the `Plugin::remove()` method.
82
	 *
83
	 * @depends test_add
84
	 * @covers Automattic\Jetpack\Connection\Plugin::remove
85
	 */
86
	public function test_remove() {
87
		$plugin = new Plugin( self::PLUGIN_SLUG );
88
		$plugin->remove();
89
90
		$this->assertArrayNotHasKey( self::PLUGIN_SLUG, Plugin_Storage::get_all() );
91
	}
92
93
	/**
94
	 * Unit test for the `Plugin:is_only()` method.
95
	 * Make sure that the method returns true if either is true:
96
	 * 1. It's the last active plugin connection.
97
	 * 2. There are no active connections, assuming the plugin has just been removed.
98
	 *
99
	 * @depends test_remove
100
	 * @covers Automattic\Jetpack\Connection\Plugin::is_only
101
	 */
102
	public function test_is_only_active() {
103
		$plugin1 = ( new Plugin( self::PLUGIN_SLUG ) )
104
			->add( self::PLUGIN_NAME, $this->plugin_args );
105
106
		$plugin2 = ( new Plugin( 'plugin-slug-2' ) )
107
			->add( 'Plugin Name 2' );
108
109
		$this->assertFalse( $plugin1->is_only() );
110
		$this->assertFalse( $plugin2->is_only() );
111
112
		$plugin2->remove();
113
		$this->assertTrue( $plugin1->is_only() );
114
115
		$plugin1->remove();
116
		$this->assertTrue( $plugin1->is_only() );
117
	}
118
119
}
120