/
PluginCollectionTest.php
140 lines (117 loc) · 3.89 KB
/
PluginCollectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.6.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Core;
use Cake\Core\Exception\MissingPluginException;
use Cake\Core\PluginCollection;
use Cake\Core\PluginInterface;
use Cake\TestSuite\TestCase;
use Company\TestPluginThree\Plugin as TestPluginThree;
use InvalidArgumentException;
use TestPlugin\Plugin as TestPlugin;
/**
* PluginCollection Test
*/
class PluginCollectionTest extends TestCase
{
public function testConstructor()
{
$plugins = new PluginCollection([new TestPlugin()]);
$this->assertCount(1, $plugins);
$this->assertTrue($plugins->has('TestPlugin'));
}
public function testAdd()
{
$plugins = new PluginCollection();
$this->assertCount(0, $plugins);
$plugins->add(new TestPlugin());
$this->assertCount(1, $plugins);
}
public function testAddOperations()
{
$plugins = new PluginCollection();
$plugins->add(new TestPlugin());
$this->assertFalse($plugins->has('Nope'));
$this->assertSame($plugins, $plugins->remove('Nope'));
$this->assertTrue($plugins->has('TestPlugin'));
$this->assertSame($plugins, $plugins->remove('TestPlugin'));
$this->assertCount(0, $plugins);
$this->assertFalse($plugins->has('TestPlugin'));
}
public function testAddVendoredPlugin()
{
$plugins = new PluginCollection();
$plugins->add(new TestPluginThree());
$this->assertTrue($plugins->has('Company/TestPluginThree'));
$this->assertFalse($plugins->has('TestPluginThree'));
$this->assertFalse($plugins->has('Company'));
$this->assertFalse($plugins->has('TestPlugin'));
}
public function testHas()
{
$plugins = new PluginCollection();
$this->assertFalse($plugins->has('TestPlugin'));
$plugins->add(new TestPlugin());
$this->assertTrue($plugins->has('TestPlugin'));
$this->assertFalse($plugins->has('Plugin'));
}
public function testGet()
{
$plugins = new PluginCollection();
$plugin = new TestPlugin();
$plugins->add($plugin);
$this->assertSame($plugin, $plugins->get('TestPlugin'));
}
public function testGetInvalid()
{
$this->expectException(MissingPluginException::class);
$plugins = new PluginCollection();
$plugins->get('Invalid');
}
public function testIterator()
{
$data = [
new TestPlugin(),
new TestPluginThree()
];
$plugins = new PluginCollection($data);
$out = [];
foreach ($plugins as $key => $plugin) {
$this->assertInstanceOf(PluginInterface::class, $plugin);
$out[] = $plugin;
}
$this->assertSame($data, $out);
}
public function testWith()
{
$plugins = new PluginCollection();
$plugin = new TestPlugin();
$plugin->disable('routes');
$pluginThree = new TestPluginThree();
$plugins->add($plugin);
$plugins->add($pluginThree);
$out = [];
foreach ($plugins->with('routes') as $p) {
$out[] = $p;
}
$this->assertCount(1, $out);
$this->assertSame($pluginThree, $out[0]);
}
public function testWithInvalidHook()
{
$this->expectException(InvalidArgumentException::class);
$plugins = new PluginCollection();
foreach ($plugins->with('bad') as $p) {
}
}
}