Passed
Push — master ( 4820b9...c7459e )
by Arthur
29:06 queued 21:14
created

GeneratorTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 145
dl 0
loc 235
rs 10
c 0
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateSqlMigration() 0 19 1
A testCreateMiddleware() 0 20 1
A testCreateFactory() 0 19 1
A testCreateProvider() 0 20 1
A testCreateNotification() 0 20 1
A testCreateQueuedListener() 0 7 1
A testCreateCommand() 0 22 1
A testCreateSynchronousJob() 0 7 1
A testCreateMongoMigration() 0 7 1
A testCreateController() 0 18 1
A testCreateJob() 0 19 1
A testCreateListener() 0 20 1
1
<?php
2
3
namespace Foundation\Tests;
4
5
use Foundation\Core\Larapi;
6
use Foundation\Generator\Events\FileGeneratedEvent;
7
use Foundation\Generator\Managers\GeneratorManager;
8
use Foundation\Traits\DisableRefreshDatabase;
9
use \Illuminate\Support\Facades\Event;
10
11
/**
12
 * Created by PhpStorm.
13
 * User: arthur
14
 * Date: 10.03.19
15
 * Time: 18:35
16
 */
17
class GeneratorTest extends \Foundation\Abstracts\Tests\TestCase
18
{
19
    use DisableRefreshDatabase;
20
21
    public function testCreateSqlMigration()
22
    {
23
        Event::fake();
24
        GeneratorManager::createMigration("User", "CreateUserTable", 'users', false);
25
26
        $module = Larapi::getModule("User");
27
        $expectedDirectoryPath = $module->getMigrations()->getPath();
28
        $expectedStubName = "migration.stub";
29
        $expectedStubOptions = [
30
            'CLASS' => 'CreateUserTable',
31
            'NAMESPACE' => $module->getMigrations()->getNamespace(),
32
            'TABLE' => 'users'
33
        ];
34
35
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedDirectoryPath, $expectedStubName, $expectedStubOptions) {
36
            $this->assertStringContainsString($expectedDirectoryPath, $event->getFilePath());
37
            $this->assertEquals($expectedStubName, $event->getStubName());
38
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
39
            return true;
40
        });
41
    }
42
43
    public function testCreateMongoMigration()
44
    {
45
        Event::fake();
46
        GeneratorManager::createMigration("User", "CreateUserCollection", 'users', true);
47
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) {
48
            $this->assertEquals("migration-mongo.stub", $event->getStubName());
49
            return true;
50
        });
51
    }
52
53
    public function testCreateFactory()
54
    {
55
        $moduleName = "User";
56
        Event::fake();
57
        GeneratorManager::createFactory($moduleName, "User");
58
59
        $expectedFileName = Larapi::getModule($moduleName)->getFactories()->getPath() . '/UserFactory.php';
60
        $expectedStubName = "factory.stub";
61
        $expectedStubOptions = [
62
            'CLASS' => 'UserFactory',
63
            'MODEL' => 'User',
64
            'MODEL_NAMESPACE' => 'Modules\User\Entities\User'
65
        ];
66
67
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedFileName, $expectedStubName, $expectedStubOptions) {
68
            $this->assertEquals($expectedFileName, $event->getFilePath());
69
            $this->assertEquals($expectedStubName, $event->getStubName());
70
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
71
            return true;
72
        });
73
    }
74
75
    public function testCreateController()
76
    {
77
        $moduleName = "User";
78
        Event::fake();
79
        GeneratorManager::createController($moduleName, "UserController");
80
81
        $expectedFileName = Larapi::getModule($moduleName)->getControllers()->getPath() . '/UserController.php';
82
        $expectedStubName = "controller.stub";
83
        $expectedStubOptions = [
84
            'CLASS' => 'UserController',
85
            'NAMESPACE' => 'Modules\User\Http\Controllers'
86
        ];
87
88
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedFileName, $expectedStubName, $expectedStubOptions) {
89
            $this->assertEquals($expectedFileName, $event->getFilePath());
90
            $this->assertEquals($expectedStubName, $event->getStubName());
91
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
92
            return true;
93
        });
94
    }
95
96
    public function testCreateListener()
97
    {
98
        $moduleName = "User";
99
        Event::fake();
100
        GeneratorManager::createListener($moduleName, "SendWelcomeMail", "UserRegisteredEvent");
101
102
        $expectedFileName = Larapi::getModule($moduleName)->getListeners()->getPath() . '/SendWelcomeMail.php';
103
        $expectedStubName = "listener.stub";
104
        $expectedStubOptions = [
105
            'CLASS' => 'SendWelcomeMail',
106
            'NAMESPACE' => 'Modules\User\Listeners',
107
            'EVENTNAME' => 'Modules\User\Events\UserRegisteredEvent',
108
            'SHORTEVENTNAME' => 'UserRegisteredEvent',
109
        ];
110
111
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedFileName, $expectedStubName, $expectedStubOptions) {
112
            $this->assertEquals($expectedFileName, $event->getFilePath());
113
            $this->assertEquals($expectedStubName, $event->getStubName());
114
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
115
            return true;
116
        });
117
    }
118
119
    public function testCreateQueuedListener()
120
    {
121
        Event::fake();
122
        GeneratorManager::createListener("User", "SendWelcomeMail", "UserRegisteredEvent", true);
123
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) {
124
            $this->assertEquals("listener-queued.stub", $event->getStubName());
125
            return true;
126
        });
127
    }
128
129
    public function testCreateJob()
130
    {
131
        $moduleName = "User";
132
        $fileName = "RandomUserJob";
133
        Event::fake();
134
        GeneratorManager::createJob($moduleName, $fileName, false);
135
136
        $expectedFileName = Larapi::getModule($moduleName)->getJobs()->getPath() . "/$fileName.php";
137
        $expectedStubName = "job-queued.stub";
138
        $expectedStubOptions = [
139
            'NAMESPACE' => 'Modules\User\Jobs',
140
            'CLASS' => 'RandomUserJob'
141
        ];
142
143
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedFileName, $expectedStubName, $expectedStubOptions) {
144
            $this->assertEquals($expectedFileName, $event->getFilePath());
145
            $this->assertEquals($expectedStubName, $event->getStubName());
146
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
147
            return true;
148
        });
149
    }
150
151
    public function testCreateSynchronousJob()
152
    {
153
        Event::fake();
154
        GeneratorManager::createJob("User", "AJob", true);
155
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) {
156
            $this->assertEquals("job.stub", $event->getStubName());
157
            return true;
158
        });
159
    }
160
161
    public function testCreateCommand()
162
    {
163
        $moduleName = "User";
164
        $fileName = "RandomCommand";
165
        $commandName = "user:dosomethingrandom";
166
167
        Event::fake();
168
        GeneratorManager::createCommand($moduleName, $fileName, $commandName);
169
170
        $expectedFileName = Larapi::getModule($moduleName)->getCommands()->getPath() . "/$fileName.php";
171
        $expectedStubName = "command.stub";
172
        $expectedStubOptions = [
173
            'NAMESPACE' => 'Modules\User\Console',
174
            'CLASS' => 'RandomCommand',
175
            'COMMAND_NAME' => $commandName
176
        ];
177
178
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedFileName, $expectedStubName, $expectedStubOptions) {
179
            $this->assertEquals($expectedFileName, $event->getFilePath());
180
            $this->assertEquals($expectedStubName, $event->getStubName());
181
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
182
            return true;
183
        });
184
    }
185
186
    public function testCreateMiddleware()
187
    {
188
        $moduleName = "User";
189
        $fileName = "RandomMiddleware";
190
191
        Event::fake();
192
        GeneratorManager::createMiddleware($moduleName, $fileName);
193
194
        $expectedFileName = Larapi::getModule($moduleName)->getMiddleWare()->getPath() . "/$fileName.php";
195
        $expectedStubName = "middleware.stub";
196
        $expectedStubOptions = [
197
            'CLASS' => 'RandomMiddleware',
198
            'NAMESPACE' => 'Modules\User\Http\Middleware'
199
        ];
200
201
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedFileName, $expectedStubName, $expectedStubOptions) {
202
            $this->assertEquals($expectedFileName, $event->getFilePath());
203
            $this->assertEquals($expectedStubName, $event->getStubName());
204
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
205
            return true;
206
        });
207
    }
208
209
    public function testCreateProvider()
210
    {
211
        $moduleName = "User";
212
        $fileName = "RandomServiceProvider";
213
214
        Event::fake();
215
        GeneratorManager::createServiceProvider($moduleName, $fileName);
216
217
        $expectedFileName = Larapi::getModule($moduleName)->getServiceProviders()->getPath() . "/$fileName.php";
218
        $expectedStubName = "provider.stub";
219
        $expectedStubOptions = [
220
            'NAMESPACE' => 'Modules\User\Providers',
221
            'CLASS' => 'RandomServiceProvider'
222
        ];
223
224
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedFileName, $expectedStubName, $expectedStubOptions) {
225
            $this->assertEquals($expectedFileName, $event->getFilePath());
226
            $this->assertEquals($expectedStubName, $event->getStubName());
227
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
228
            return true;
229
        });
230
    }
231
232
    public function testCreateNotification()
233
    {
234
        $moduleName = "User";
235
        $fileName = "RandomNotification";
236
237
        Event::fake();
238
        GeneratorManager::createNotification($moduleName, $fileName);
239
240
        $expectedFileName = Larapi::getModule($moduleName)->getNotifications()->getPath() . "/$fileName.php";
241
        $expectedStubName = "notification.stub";
242
        $expectedStubOptions = [
243
            'NAMESPACE' => 'Modules\User\Notifications',
244
            'CLASS' => 'RandomNotification'
245
        ];
246
247
        Event::assertDispatched(FileGeneratedEvent::class, function (FileGeneratedEvent $event) use ($expectedFileName, $expectedStubName, $expectedStubOptions) {
248
            $this->assertEquals($expectedFileName, $event->getFilePath());
249
            $this->assertEquals($expectedStubName, $event->getStubName());
250
            $this->assertEquals($expectedStubOptions, $event->getStubOptions());
251
            return true;
252
        });
253
    }
254
}
255