Completed
Push — add/86-changelog ( 142a60...d9a8fc )
by Jeremy
19:49 queued 12:19
created

Test_Plugin::test_is_only_active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
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
		Plugin_Storage::configure();
58
	}
59
60
	/**
61
	 * Clean up the test environment.
62
	 */
63
	protected function tearDown() {
64
		parent::tearDown();
65
66
		Mock::disableAll();
67
	}
68
69
	/**
70
	 * Unit test for the `Plugin::add()` method.
71
	 *
72
	 * @covers Automattic\Jetpack\Connection\Plugin::add
73
	 */
74
	public function test_add() {
75
		$plugin = new Plugin( self::PLUGIN_SLUG );
76
77
		$plugin->add( self::PLUGIN_NAME, $this->plugin_args + array( 'invalid_key' => 'value' ) );
78
79
		$this->assertEquals( array( 'name' => self::PLUGIN_NAME ) + $this->plugin_args, Plugin_Storage::get_one( self::PLUGIN_SLUG ) );
80
	}
81
82
	/**
83
	 * Unit test for the `Plugin::remove()` method.
84
	 *
85
	 * @depends test_add
86
	 * @covers Automattic\Jetpack\Connection\Plugin::remove
87
	 */
88
	public function test_remove() {
89
		$plugin = new Plugin( self::PLUGIN_SLUG );
90
		$plugin->remove();
91
92
		$this->assertArrayNotHasKey( self::PLUGIN_SLUG, Plugin_Storage::get_all() );
93
	}
94
95
	/**
96
	 * Unit test for the `Plugin:is_only()` method.
97
	 * Make sure that the method returns true if either is true:
98
	 * 1. It's the last active plugin connection.
99
	 * 2. There are no active connections, assuming the plugin has just been removed.
100
	 *
101
	 * @depends test_remove
102
	 * @covers Automattic\Jetpack\Connection\Plugin::is_only
103
	 */
104
	public function test_is_only_active() {
105
		$plugin1 = ( new Plugin( self::PLUGIN_SLUG ) )
106
			->add( self::PLUGIN_NAME, $this->plugin_args );
107
108
		$plugin2 = ( new Plugin( 'plugin-slug-2' ) )
109
			->add( 'Plugin Name 2' );
110
111
		$this->assertFalse( $plugin1->is_only() );
112
		$this->assertFalse( $plugin2->is_only() );
113
114
		$plugin2->remove();
115
		$this->assertTrue( $plugin1->is_only() );
116
117
		$plugin1->remove();
118
		$this->assertTrue( $plugin1->is_only() );
119
	}
120
121
}
122