forked from lamtranb/laravel-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaravelModuleTest.php
252 lines (207 loc) · 7.04 KB
/
LaravelModuleTest.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
<?php
namespace Nwidart\Modules\Tests;
use Modules\Recipe\Providers\DeferredServiceProvider;
use Modules\Recipe\Providers\RecipeServiceProvider;
use Nwidart\Modules\Contracts\ActivatorInterface;
use Nwidart\Modules\Json;
class ModuleTest extends BaseTestCase
{
/**
* @var TestingModule
*/
private $module;
/**
* @var ActivatorInterface
*/
private $activator;
public function setUp(): void
{
parent::setUp();
$this->module = new TestingModule($this->app, 'Recipe Name', __DIR__ . '/stubs/valid/Recipe');
$this->activator = $this->app[ActivatorInterface::class];
}
public function tearDown(): void
{
$this->activator->reset();
parent::tearDown();
}
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
symlink(__DIR__ . '/stubs/valid', __DIR__ . '/stubs/valid_symlink');
}
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
unlink(__DIR__ . '/stubs/valid_symlink');
}
/** @test */
public function it_gets_module_name()
{
$this->assertEquals('Recipe Name', $this->module->getName());
}
/** @test */
public function it_gets_lowercase_module_name()
{
$this->assertEquals('recipe name', $this->module->getLowerName());
}
/** @test */
public function it_gets_studly_name()
{
$this->assertEquals('RecipeName', $this->module->getStudlyName());
}
/** @test */
public function it_gets_snake_name()
{
$this->assertEquals('recipe_name', $this->module->getSnakeName());
}
/** @test */
public function it_gets_module_description()
{
$this->assertEquals('recipe module', $this->module->getDescription());
}
/** @test */
public function it_gets_module_alias()
{
$this->assertEquals('recipe', $this->module->getAlias());
}
/** @test */
public function it_gets_module_path()
{
$this->assertEquals(__DIR__ . '/stubs/valid/Recipe', $this->module->getPath());
}
/** @test */
public function it_gets_module_path_with_symlink()
{
// symlink created in setUpBeforeClass
$this->module = new TestingModule($this->app, 'Recipe Name', __DIR__ . '/stubs/valid_symlink/Recipe');
$this->assertEquals(__DIR__ . '/stubs/valid_symlink/Recipe', $this->module->getPath());
// symlink deleted in tearDownAfterClass
}
/** @test */
public function it_gets_required_modules()
{
$this->assertEquals(['required_module'], $this->module->getRequires());
}
/** @test */
public function it_loads_module_translations()
{
(new TestingModule($this->app, 'Recipe', __DIR__ . '/stubs/valid/Recipe'))->boot();
$this->assertEquals('Recipe', trans('recipe::recipes.title.recipes'));
}
/** @test */
public function it_reads_module_json_files()
{
$jsonModule = $this->module->json();
$composerJson = $this->module->json('composer.json');
$this->assertInstanceOf(Json::class, $jsonModule);
$this->assertEquals('0.1', $jsonModule->get('version'));
$this->assertInstanceOf(Json::class, $composerJson);
$this->assertEquals('asgard-module', $composerJson->get('type'));
}
/** @test */
public function it_reads_key_from_module_json_file_via_helper_method()
{
$this->assertEquals('Recipe', $this->module->get('name'));
$this->assertEquals('0.1', $this->module->get('version'));
$this->assertEquals('my default', $this->module->get('some-thing-non-there', 'my default'));
$this->assertEquals(['required_module'], $this->module->get('requires'));
}
/** @test */
public function it_reads_key_from_composer_json_file_via_helper_method()
{
$this->assertEquals('nwidart/recipe', $this->module->getComposerAttr('name'));
}
/** @test */
public function it_casts_module_to_string()
{
$this->assertEquals('RecipeName', (string) $this->module);
}
/** @test */
public function it_module_status_check()
{
$this->assertFalse($this->module->isStatus(true));
$this->assertTrue($this->module->isStatus(false));
}
/** @test */
public function it_checks_module_enabled_status()
{
$this->assertFalse($this->module->isEnabled());
$this->assertTrue($this->module->isDisabled());
}
/** @test */
public function it_sets_active_status(): void
{
$this->module->setActive(true);
$this->assertTrue($this->module->isEnabled());
$this->module->setActive(false);
$this->assertFalse($this->module->isEnabled());
}
/** @test */
public function it_fires_events_when_module_is_enabled()
{
$this->expectsEvents([
sprintf('modules.%s.enabling', $this->module->getLowerName()),
sprintf('modules.%s.enabled', $this->module->getLowerName()),
]);
$this->module->enable();
}
/** @test */
public function it_fires_events_when_module_is_disabled()
{
$this->expectsEvents([
sprintf('modules.%s.disabling', $this->module->getLowerName()),
sprintf('modules.%s.disabled', $this->module->getLowerName()),
]);
$this->module->disable();
}
/** @test */
public function it_has_a_good_providers_manifest_path()
{
$this->assertEquals(
$this->app->bootstrapPath("cache/{$this->module->getSnakeName()}_module.php"),
$this->module->getCachedServicesPath()
);
}
/** @test */
public function it_makes_a_manifest_file_when_providers_are_loaded()
{
$cachedServicesPath = $this->module->getCachedServicesPath();
@unlink($cachedServicesPath);
$this->assertFileNotExists($cachedServicesPath);
$this->module->registerProviders();
$this->assertFileExists($cachedServicesPath);
$manifest = require $cachedServicesPath;
$this->assertEquals([
'providers' => [
RecipeServiceProvider::class,
DeferredServiceProvider::class,
],
'eager' => [RecipeServiceProvider::class],
'deferred' => ['deferred' => DeferredServiceProvider::class],
'when' =>
[DeferredServiceProvider::class => []],
], $manifest);
}
/** @test */
public function it_can_load_a_deferred_provider()
{
@unlink($this->module->getCachedServicesPath());
$this->module->registerProviders();
try {
app('foo');
$this->assertTrue(false, "app('foo') should throw an exception.");
} catch (\Exception $e) {
$this->assertEquals('Target class [foo] does not exist.', $e->getMessage());
}
app('deferred');
$this->assertEquals('bar', app('foo'));
}
}
class TestingModule extends \Nwidart\Modules\Laravel\Module
{
public function registerProviders(): void
{
parent::registerProviders();
}
}