1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Gii\Tests\Generators; |
4
|
|
|
|
5
|
|
|
use Yiisoft\Aliases\Aliases; |
6
|
|
|
use Yiisoft\View\View; |
7
|
|
|
use Yiisoft\Yii\Gii\CodeFile; |
8
|
|
|
use Yiisoft\Yii\Gii\Generator\Controller\Generator as ControllerGenerator; |
9
|
|
|
use Yiisoft\Yii\Gii\Tests\GiiTestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* ControllerGeneratorTest checks that Gii controller generator produces valid results |
13
|
|
|
*/ |
14
|
|
|
class ControllerGeneratorTest extends GiiTestCase |
15
|
|
|
{ |
16
|
|
|
public function testValidGenerator(): void |
17
|
|
|
{ |
18
|
|
|
$generator = new ControllerGenerator( |
19
|
|
|
$this->getContainer()->get(Aliases::class), |
20
|
|
|
$this->getContainer()->get(View::class) |
21
|
|
|
); |
22
|
|
|
$generator->load( |
23
|
|
|
[ |
24
|
|
|
'template' => 'default', |
25
|
|
|
'controllerClass' => 'TestController', |
26
|
|
|
'actions' => 'index,edit,view' |
27
|
|
|
] |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$generator->validate(); |
31
|
|
|
|
32
|
|
|
$this->assertNotEmpty($generator->generate()); |
33
|
|
|
$this->assertContainsOnlyInstancesOf(CodeFile::class, $generator->generate()); |
34
|
|
|
$this->assertCount(4, $generator->generate()); |
35
|
|
|
$this->assertFalse($generator->hasErrors()); |
36
|
|
|
$this->assertEquals(['edit', 'index', 'view'], $generator->getActionIDs()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testInvalidGenerator(): void |
40
|
|
|
{ |
41
|
|
|
$generator = new ControllerGenerator( |
42
|
|
|
$this->getContainer()->get(Aliases::class), |
43
|
|
|
$this->getContainer()->get(View::class) |
44
|
|
|
); |
45
|
|
|
$generator->load( |
46
|
|
|
[ |
47
|
|
|
'template' => 'test', |
48
|
|
|
'controllerClass' => 'Wr0ngContr0ller', |
49
|
|
|
'actions' => 'index,ed1t,view', |
50
|
|
|
'templates' => [ |
51
|
|
|
'default' => dirname(__DIR__ . '../templates') |
52
|
|
|
] |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$generator->validate(); |
57
|
|
|
|
58
|
|
|
$this->assertTrue($generator->hasErrors()); |
59
|
|
|
$this->assertNotEmpty($generator->getErrors()['template']); |
60
|
|
|
$this->assertNotEmpty($generator->getErrors()['controllerClass']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testCustomTemplate(): void |
64
|
|
|
{ |
65
|
|
|
$generator = new ControllerGenerator( |
66
|
|
|
$this->getContainer()->get(Aliases::class), |
67
|
|
|
$this->getContainer()->get(View::class) |
68
|
|
|
); |
69
|
|
|
$generator->load( |
70
|
|
|
[ |
71
|
|
|
'template' => 'custom', |
72
|
|
|
'controllerClass' => 'TestController', |
73
|
|
|
'actions' => 'index,edit,view', |
74
|
|
|
'templates' => [ |
75
|
|
|
'custom' => '@app/templates/custom' |
76
|
|
|
] |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$validationResult = $generator->validate(); |
81
|
|
|
|
82
|
|
|
$this->assertFalse( |
83
|
|
|
$generator->hasErrors(), |
84
|
|
|
implode("\n", array_values($validationResult->getResult('template')->getErrors())) |
85
|
|
|
); |
86
|
|
|
$this->assertContainsOnlyInstancesOf(CodeFile::class, $generator->generate()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|