1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests; |
4
|
|
|
|
5
|
|
|
use LaravelZero\Framework\Exceptions\NotImplementedException; |
6
|
|
|
use LaravelZero\Framework\Contracts\Exceptions\ConsoleException; |
7
|
|
|
use Symfony\Component\Console\Exception\CommandNotFoundException; |
8
|
|
|
|
9
|
|
|
class ContainerTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** @test */ |
12
|
|
|
public function it_has_a_version_getter(): void |
13
|
|
|
{ |
14
|
|
|
$this->assertEquals($this->app->getContainer()->version(), 'Test version'); |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** @test */ |
18
|
|
|
public function it_has_a_base_path_getter() |
19
|
|
|
{ |
20
|
|
|
$container = $this->app->getContainer(); |
21
|
|
|
|
22
|
|
|
$this->assertEquals(BASE_PATH, $container->basePath()); |
23
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'Unit', $container->basePath('Unit')); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** @test */ |
27
|
|
View Code Duplication |
public function it_has_a_config_path_getter() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$container = $this->app->getContainer(); |
30
|
|
|
|
31
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'config', $container->configPath()); |
32
|
|
|
$this->assertEquals( |
33
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'custom.php', |
34
|
|
|
$container->configPath('custom.php') |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @test */ |
39
|
|
View Code Duplication |
public function it_has_a_database_path_getter() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$container = $this->app->getContainer(); |
42
|
|
|
|
43
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'database', $container->databasePath()); |
44
|
|
|
$this->assertEquals( |
45
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'migrations', |
46
|
|
|
$container->databasePath('migrations') |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @test */ |
51
|
|
|
public function it_has_a_lang_path_getter() |
52
|
|
|
{ |
53
|
|
|
$this->assertEquals( |
54
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'lang', $this->app->getContainer()->langPath() |
|
|
|
|
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @test */ |
59
|
|
|
public function it_has_a_resource_path_getter() |
60
|
|
|
{ |
61
|
|
|
$container = $this->app->getContainer(); |
62
|
|
|
|
63
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'resources', $container->resourcePath()); |
64
|
|
|
$this->assertEquals( |
65
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'js', |
66
|
|
|
$container->resourcePath('assets'.DIRECTORY_SEPARATOR.'js') |
67
|
|
|
); |
68
|
|
|
$this->assertEquals( |
69
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR.'app.php', |
70
|
|
|
$container->resourcePath('assets'.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR.'app.php') |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @test */ |
75
|
|
|
public function it_has_a_storage_path_getter() |
76
|
|
|
{ |
77
|
|
|
$container = $this->app->getContainer(); |
78
|
|
|
|
79
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'storage', $container->storagePath()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** @test */ |
83
|
|
|
public function it_has_environment_getter() |
84
|
|
|
{ |
85
|
|
|
$container = $this->app->getContainer(); |
86
|
|
|
|
87
|
|
|
$this->assertSame('Test version', $container->version()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @test */ |
91
|
|
|
public function it_confirms_the_running_in_console() |
92
|
|
|
{ |
93
|
|
|
$this->assertTrue( |
94
|
|
|
$this->app->getContainer()->runningInConsole() |
|
|
|
|
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @test */ |
99
|
|
|
public function it_has_namespace_getter() |
100
|
|
|
{ |
101
|
|
|
$this->assertEquals($this->app->getContainer()->getNamespace(), 'App\\'); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** @test */ |
105
|
|
|
public function it_confirms_that_is_not_down_for_maintenance() |
106
|
|
|
{ |
107
|
|
|
$this->assertFalse( |
108
|
|
|
$this->app->getContainer()->isDownForMaintenance() |
|
|
|
|
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @test */ |
113
|
|
|
public function it_can_abort(): void |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
$this->app->getContainer()->abort(404, 'Foo'); |
|
|
|
|
117
|
|
|
} catch (CommandNotFoundException $notFoundException) { |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->assertInstanceOf(CommandNotFoundException::class, $notFoundException); |
|
|
|
|
121
|
|
|
$this->assertEquals($notFoundException->getMessage(), 'Foo'); |
122
|
|
|
|
123
|
|
|
try { |
124
|
|
|
abort(200, 'Bar', ['Foo' => 'Bar']); |
125
|
|
|
} catch (ConsoleException $consoleException) { |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->assertInstanceOf(ConsoleException::class, $consoleException); |
|
|
|
|
129
|
|
|
$this->assertEquals($consoleException->getExitCode(), 200); |
130
|
|
|
$this->assertEquals($consoleException->getMessage(), 'Bar'); |
131
|
|
|
$this->assertEquals($consoleException->getHeaders(), ['Foo' => 'Bar']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** @test */ |
135
|
|
|
public function it_dont_implement_method_register_configured_providers() |
136
|
|
|
{ |
137
|
|
|
$this->expectException(NotImplementedException::class); |
138
|
|
|
|
139
|
|
|
$this->app->getContainer()->registerConfiguredProviders(); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** @test */ |
143
|
|
|
public function it_dont_implement_method_register() |
144
|
|
|
{ |
145
|
|
|
$this->expectException(NotImplementedException::class); |
146
|
|
|
|
147
|
|
|
$this->app->getContainer()->register('foo'); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** @test */ |
151
|
|
|
public function it_dont_implement_method_register_deferred_provider() |
152
|
|
|
{ |
153
|
|
|
$this->expectException(NotImplementedException::class); |
154
|
|
|
|
155
|
|
|
$this->app->getContainer()->registerDeferredProvider('foo'); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** @test */ |
159
|
|
|
public function it_dont_implement_method_register_boot() |
160
|
|
|
{ |
161
|
|
|
$this->expectException(NotImplementedException::class); |
162
|
|
|
|
163
|
|
|
$this->app->getContainer()->boot(); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** @test */ |
167
|
|
|
public function it_dont_implement_method_register_booting() |
168
|
|
|
{ |
169
|
|
|
$this->expectException(NotImplementedException::class); |
170
|
|
|
|
171
|
|
|
$this->app->getContainer()->booting('foo'); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** @test */ |
175
|
|
|
public function it_dont_implement_method_register_booted() |
176
|
|
|
{ |
177
|
|
|
$this->expectException(NotImplementedException::class); |
178
|
|
|
|
179
|
|
|
$this->app->getContainer()->booted('foo'); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** @test */ |
183
|
|
|
public function it_dont_implement_method_get_cached_services_path() |
184
|
|
|
{ |
185
|
|
|
$this->expectException(NotImplementedException::class); |
186
|
|
|
|
187
|
|
|
$this->app->getContainer()->getCachedServicesPath(); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** @test */ |
191
|
|
|
public function it_configures_monolog() |
192
|
|
|
{ |
193
|
|
|
$container = $this->app->getContainer(); |
194
|
|
|
$callback = function () { |
195
|
|
|
}; |
196
|
|
|
|
197
|
|
|
$this->assertFalse($container->hasMonologConfigurator()); |
198
|
|
|
$this->app->getContainer()->configureMonologUsing($callback); |
|
|
|
|
199
|
|
|
$this->assertTrue($container->hasMonologConfigurator()); |
200
|
|
|
$this->assertSame($container->getMonologConfigurator(), $callback); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: