forked from keiosweb/oc-plugin-testsuite
/
OctoberPluginTestCase.php
executable file
·222 lines (186 loc) · 5.29 KB
/
OctoberPluginTestCase.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Class OctoberPluginTestCase
*/
abstract class OctoberPluginTestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
* @var array
*/
protected $refreshPlugins = [];
/**
* @var bool
*/
protected $requiresOctoberMigration = false;
/**
* Creates the application.
*
* @return Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
/*
* Build application instance, bootstap it
*/
// __DIR__ = base_path() . '/plugins/vendor/plugin/vendor/keios/oc-plugin-testsuite
$app = require __DIR__.'/../../../../../../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
$app->setLocale('en');
/*
* Use the sqlite memory driver during the unit testing
*/
$app['config']->set('database.default', 'sqlite');
$app['config']->set(
'database.connections.sqlite',
[
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
]
);
return $app;
}
/**
* Perform test case set up
*
* @deprecated use pluginTestSetUp method
*/
public final function setUp()
{
/*
* Create application instance
*/
parent::setUp();
/*
* Reboot October Singletons' dependencies
*/
\System\Classes\PluginManager::instance()->bindContainerObjects();
\System\Classes\UpdateManager::instance()->bindContainerObjects();
/*
* If test maker wants to create functional test, migrate october and given plugins
*/
if (count($this->refreshPlugins) > 0) {
foreach ($this->refreshPlugins as $pluginCode) {
$this->refreshPlugin($pluginCode);
}
}
/*
* If test maker wants october environment for some reason, migrate october
*/
if ((count($this->refreshPlugins) === 0) && $this->requiresOctoberMigration) {
$this->migrateOctober();
}
/*
* Prevent mailer from actually sending emails
*/
Mail::pretend(true);
$this->pluginTestSetUp();
}
/**
* Alternative test case boot method
*/
public function pluginTestSetUp()
{
}
/**
* Clean up after tests
*/
public function tearDown()
{
$this->rebootAllModels();
parent::tearDown();
unset ($this->app);
}
/**
* Boot given plugin in OctoberCMS environment
*/
protected final function refreshPlugin($code)
{
$this->assertPluginCode($code);
$path = $this->createPluginPath($code);
$namespace = $this->createNamespace($code);
$this->migrateOctober();
\System\Classes\PluginManager::instance()->loadPlugin(
$code,
$path
);
Artisan::call('plugin:refresh', ['name' => $code]);
}
/**
* Clears event listeners of available plugin models and boots them
*/
protected final function rebootAllModels()
{
foreach (get_declared_classes() as $class) {
/*
* Reboot event listeners on all October\Rain\Database\Model subclasses
* with exception of Pivot model that is a special, internal model for pivot tables
* and should not be initialised outside of relation context.
*/
if (is_subclass_of($class, 'October\Rain\Database\Model') && $class !== 'October\Rain\Database\Pivot') {
$class::flushEventListeners();
}
}
\October\Rain\Database\Model::flushEventListeners();
}
/**
* Call october:up command to migrate whole OctoberCMS to in-memory SQLite Database
*/
protected final function migrateOctober()
{
Artisan::call('october:up');
}
/**
* Check that given plugin code is valid
*
* @param string $code
*
* @throws \RuntimeException
*/
protected final function assertPluginCode($code)
{
if (!preg_match('/[A-Za-z0-9-_]*\.[A-Za-z0-9-_]*/', $code)) {
throw new \RuntimeException(
'Plugin name '.$code.' is invalid - should follow vendor.pluginName schema.'
);
}
}
/**
* Creates full plugin path from plugin code
*
* @param string $code
*
* @returns string
*/
protected final function createPluginPath($code)
{
$parts = explode('.', $code);
$parts = array_map(
function ($part) {
return strtolower($part);
},
$parts
);
list($vendor, $plugin) = $parts;
return plugins_path().'/'.$vendor.'/'.$plugin;
}
/**
* Creates namespace from plugin code
*
* @param string $code
*
* @returns string
*/
protected final function createNamespace($code)
{
$parts = explode('.', $code);
$parts = array_map(
function ($part) {
return ucfirst(strtolower($part));
},
$parts
);
list($vendor, $plugin) = $parts;
return '\\'.$vendor.'\\'.$plugin;
}
}