-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModelGeneratorTest.php
143 lines (116 loc) · 4.64 KB
/
ModelGeneratorTest.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
<?php
namespace RonasIT\Support\Tests;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Generators\ModelGenerator;
use RonasIT\Support\Tests\Support\Model\ModelMockTrait;
class ModelGeneratorTest extends TestCase
{
use ModelMockTrait;
public function testModelAlreadyExists()
{
$this->mockClass(ModelGenerator::class, [
$this->classExistsMethodCall(['models', 'Post']),
]);
$this->assertExceptionThrew(
className: ClassAlreadyExistsException::class,
message: 'Cannot create Post Model cause Post Model already exists. Remove Post Model.',
);
app(ModelGenerator::class)
->setModel('Post')
->generate();
}
public function testRelationModelMissing()
{
$this->assertExceptionThrew(
className: ClassNotExistsException::class,
message: "Cannot create Post Model cause relation model Comment does not exist. "
. "Create the Comment Model by himself or run command 'php artisan make:entity Comment --only-model'.",
);
app(ModelGenerator::class)
->setModel('Post')
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => [],
'belongsTo' => [],
'belongsToMany' => [],
])
->generate();
}
public function testCreateModel()
{
$this->mockFilesystem();
app(ModelGenerator::class)
->setModel('Post')
->setFields([
'integer-required' => ['media_id'],
'boolean-required' => ['is_published'],
])
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => ['User'],
'belongsTo' => [],
'belongsToMany' => [],
])
->generate();
$this->assertGeneratedFileEquals('new_model.php', 'app/Models/Post.php');
$this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/Comment.php');
$this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/User.php');
$this->assertEventPushed(
className: SuccessCreateMessage::class,
message: 'Created a new Model: Post',
);
}
public function testCreateModelStubNotExist()
{
$this->mockFilesystem();
config(['entity-generator.stubs.model' => 'incorrect_stub']);
app(ModelGenerator::class)
->setModel('Post')
->setFields([])
->generate();
$this->assertFileDoesNotExist('app/Models/Post.php');
$this->assertFileDoesNotExist('app/Models/Comment.php');
$this->assertFileDoesNotExist('app/Models/User.php');
$this->assertEventPushed(
className: WarningEvent::class,
message: 'Generation of model has been skipped cause the view incorrect_stub from the config entity-generator.stubs.model is not exists. Please check that config has the correct view name value.',
);
}
public function testCreateModelWithoutRelationsRelationStubNotExist()
{
$this->mockFilesystem();
config(['entity-generator.stubs.relation' => 'incorrect_stub']);
app(ModelGenerator::class)
->setModel('Post')
->setFields([])
->generate();
$this->assertGeneratedFileEquals('new_model_without_fields_and_relations.php', 'app/Models/Post.php');
$this->assertEventPushed(
className: SuccessCreateMessage::class,
message: 'Created a new Model: Post',
);
}
public function testCreateModelWithRelationsRelationStubNotExist()
{
$this->mockFilesystem();
config(['entity-generator.stubs.relation' => 'incorrect_stub']);
app(ModelGenerator::class)
->setModel('Post')
->setFields([])
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => ['User'],
'belongsTo' => [],
'belongsToMany' => [],
])
->generate();
$this->assertFileDoesNotExist('new_model.php', 'app/Models/Post.php');
$this->assertEventPushed(
className: WarningEvent::class,
message: 'Generation of model has been skipped cause the view incorrect_stub from the config entity-generator.stubs.relation is not exists. Please check that config has the correct view name value.',
);
}
}