forked from EC-CUBE/ec-cube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginServiceWithEntityExtensionTest.php
327 lines (261 loc) · 10.3 KB
/
PluginServiceWithEntityExtensionTest.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved.
*
* http://www.lockon.co.jp/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
namespace Eccube\Tests\Service;
use Eccube\Repository\PluginRepository;
use Eccube\Service\PluginService;
use Eccube\Service\SchemaService;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
class PluginServiceWithEntityExtensionTest extends AbstractServiceTestCase
{
protected $app;
/**
* @var PluginService
*/
private $service;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $mockSchemaService;
/**
* @var PluginRepository
*/
private $pluginRepository;
public function setUp()
{
parent::setUp();
$this->mockSchemaService = $this->createMock(SchemaService::class);
$this->service = $this->app['eccube.service.plugin'];
$rc = new \ReflectionClass($this->service);
$prop = $rc->getProperty('schemaService');
$prop->setAccessible(true);
$prop->setValue($this->service, $this->mockSchemaService);
$this->pluginRepository = $this->app['eccube.repository.plugin'];
}
public function tearDown()
{
$finder = new Finder();
$iterator = $finder
->in($this->app['config']['plugin_realdir'])
->name('dummy*')
->directories();
$dirs = [];
foreach ($iterator as $dir) {
$dirs[] = $dir->getPathName();
}
foreach ($dirs as $dir) {
$this->deleteFile($dir);
}
foreach (glob($this->app['config']['root_dir'].'/app/proxy/entity/*.php') as $file) {
unlink($file);
}
parent::tearDown();
}
public function deleteFile($path)
{
$f = new Filesystem();
return $f->remove($path);
}
/**
* プラグインをインストールしたときにスキーマが更新される
*/
public function testInstallPlugin()
{
list($configA, $fileA) = $this->createDummyPluginWithEntityExtension();
// スキーマ更新されるはず
$this->mockSchemaService->expects($this->once())->method('updateSchema');
// インストール
$this->service->install($fileA);
// Proxyは生成されない
self::assertFalse(file_exists($this->app['config']['root_dir'].'/app/proxy/entity/Customer.php'));
}
/**
* プラグインを有効化するとプロキシが生成される
*/
public function testEnablePlugin()
{
list($configA, $fileA) = $this->createDummyPluginWithEntityExtension();
// インストール
$this->service->install($fileA);
$pluginA = $this->pluginRepository->findOneBy(array('code'=>$configA['code']));
$this->app['orm.em']->detach($pluginA);
// 有効化
$this->service->enable($pluginA);
// Traitは有効
self::assertContainsTrait(
$this->app['config']['root_dir'].'/app/proxy/entity/Customer.php',
"Plugin\\${configA['code']}\\Entity\\HogeTrait");
}
/**
* プラグインを無効化するとプロキシからTraitが使われなくなる
*/
public function testDisablePlugin()
{
list($configA, $fileA) = $this->createDummyPluginWithEntityExtension();
// インストール
$this->service->install($fileA);
$pluginA = $this->pluginRepository->findOneBy(array('code'=>$configA['code']));
$this->app['orm.em']->detach($pluginA);
// 有効化
$this->service->enable($pluginA);
// 無効化
$this->service->disable($pluginA);
// Traitは無効
self::assertNotContainsTrait(
$this->app['config']['root_dir'].'/app/proxy/entity/Customer.php',
"Plugin\\${configA['code']}\\Entity\\HogeTrait");
}
/**
* プラグインを削除するとスキーマ更新が行われる
*/
public function testUninstallPlugin()
{
list($configA, $fileA) = $this->createDummyPluginWithEntityExtension();
// インストール
$this->service->install($fileA);
$pluginA = $this->pluginRepository->findOneBy(array('code'=>$configA['code']));
$this->app['orm.em']->detach($pluginA);
// 有効化
$this->service->enable($pluginA);
// 無効化
$this->service->disable($pluginA);
// スキーマ更新されるはず
$this->mockSchemaService->expects($this->once())->method('updateSchema');
// 削除
$this->service->uninstall($pluginA);
// Traitは無効
self::assertNotContainsTrait(
$this->app['config']['root_dir'].'/app/proxy/entity/Customer.php',
"Plugin\\${configA['code']}\\Entity\\HogeTrait");
}
/**
* プラグインを無効化せずに削除してもプロキシの再生成とスキーマ更新が行われる
*/
public function testImmediatelyUninstallPlugin()
{
list($configA, $fileA) = $this->createDummyPluginWithEntityExtension();
// インストール
$this->service->install($fileA);
$pluginA = $this->pluginRepository->findOneBy(array('code'=>$configA['code']));
$this->app['orm.em']->detach($pluginA);
// 有効化
$this->service->enable($pluginA);
// スキーマ更新されるはず
$this->mockSchemaService->expects($this->once())->method('updateSchema');
// 削除
$this->service->uninstall($pluginA);
// Traitは無効
self::assertNotContainsTrait(
$this->app['config']['root_dir'].'/app/proxy/entity/Customer.php',
"Plugin\\${configA['code']}\\Entity\\HogeTrait");
}
/**
* インストール済み(無効)のプラグインがある状態で別のプラグインをインストールして有効化する
*/
public function testInstallWithEntityExtension_with_disabled_plugin()
{
list($configDisabled, $fileDisabled) = $this->createDummyPluginWithEntityExtension();
list($configEnabled, $fileEnabled) = $this->createDummyPluginWithEntityExtension();
// スキーマ更新は2回行われるはず
$this->mockSchemaService->expects($this->exactly(2))->method('updateSchema');
// プラグイン1はインストールのみ
{
$this->service->install($fileDisabled);
}
// プラグイン2をインストール&有効化
{
$this->service->install($fileEnabled);
$pluginEnabled = $this->pluginRepository->findOneBy(array('code'=>$configEnabled['code']));
$this->app['orm.em']->detach($pluginEnabled);
// 有効化
$this->service->enable($pluginEnabled);
}
self::assertNotContainsTrait($this->app['config']['root_dir'].'/app/proxy/entity/Customer.php',
"Plugin\\${configDisabled['code']}\\Entity\\HogeTrait",
'無効状態プラグインのTraitは利用されないはず');
// 無効化状態のTraitは利用されないはず
self::assertContainsTrait($this->app['config']['root_dir'].'/app/proxy/entity/Customer.php',
"Plugin\\${configEnabled['code']}\\Entity\\HogeTrait",
'有効状態のプラグインは利用されるはず');
}
private static function assertContainsTrait($file, $trait, $message = 'Traitが有効になっているはず')
{
$tokens = Tokens::fromCode(file_get_contents($file));
$useTraitStart = $tokens->getNextTokenOfKind(0, [[CT::T_USE_TRAIT]]);
$useTraitEnd = $tokens->getNextTokenOfKind($useTraitStart, [';']);
$useStatement = $tokens->generatePartialCode($useTraitStart, $useTraitEnd);
self::assertContains($trait, $useStatement, $message);
}
private static function assertNotContainsTrait($file, $trait, $message = 'Traitが有効になっているはず')
{
$tokens = Tokens::fromCode(file_get_contents($file));
$useTraitStart = $tokens->getNextTokenOfKind(0, [[CT::T_USE_TRAIT]]);
$useTraitEnd = $tokens->getNextTokenOfKind($useTraitStart, [';']);
$useStatement = $tokens->generatePartialCode($useTraitStart, $useTraitEnd);
self::assertNotContains($trait, $useStatement, $message);
}
// テスト用のダミープラグインを配置する
private function createTempDir(){
$t = sys_get_temp_dir()."/plugintest.".sha1(mt_rand());
if(!mkdir($t)){
throw new \Exception("$t ".$php_errormsg);
}
return $t;
}
private function createDummyPluginConfig()
{
$tmpname="dummy".sha1(mt_rand());
$config=array();
$config['name'] = $tmpname."_name";
$config['code'] = $tmpname;
$config['version'] = $tmpname;
$config['event'] = 'DummyEvent';
return $config;
}
private function createDummyPluginWithEntityExtension()
{
// インストールするプラグインを作成する
$config = $this->createDummyPluginConfig();
$tmpname = $config['code'];
$tmpdir = $this->createTempDir();
$tmpfile = $tmpdir.'/plugin.tar';
$tar = new \PharData($tmpfile);
$tar->addFromString('config.yml',Yaml::dump($config));
$tar->addFromString('Entity/HogeTrait.php', <<< EOT
<?php
namespace Plugin\\${tmpname}\\Entity;
use Eccube\Annotation\EntityExtension;
/**
* @EntityExtension("Eccube\Entity\Customer")
*/
trait HogeTrait
{
}
EOT
);
return [$config, $tmpfile];
}
}