1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests; |
4
|
|
|
|
5
|
|
|
use LaravelZero\Framework\Exceptions\NotImplementedException; |
6
|
|
|
|
7
|
|
|
class ContainerTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
/** @test */ |
10
|
|
|
public function it_has_a_version_getter(): void |
11
|
|
|
{ |
12
|
|
|
$this->assertEquals($this->app->getContainer()->version(), 'Test version'); |
|
|
|
|
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** @test */ |
16
|
|
|
public function it_has_a_base_path_getter() |
17
|
|
|
{ |
18
|
|
|
$container = $this->app->getContainer(); |
19
|
|
|
|
20
|
|
|
$this->assertEquals(BASE_PATH, $container->basePath()); |
21
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'Unit', $container->basePath('Unit')); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @test */ |
25
|
|
View Code Duplication |
public function it_has_a_config_path_getter() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$container = $this->app->getContainer(); |
28
|
|
|
|
29
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'config', $container->configPath()); |
30
|
|
|
$this->assertEquals( |
31
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'custom.php', |
32
|
|
|
$container->configPath('custom.php') |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** @test */ |
37
|
|
View Code Duplication |
public function it_has_a_database_path_getter() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$container = $this->app->getContainer(); |
40
|
|
|
|
41
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'database', $container->databasePath()); |
42
|
|
|
$this->assertEquals( |
43
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'migrations', |
44
|
|
|
$container->databasePath('migrations') |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @test */ |
49
|
|
|
public function it_has_a_lang_path_getter() |
50
|
|
|
{ |
51
|
|
|
$this->assertEquals( |
52
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'lang', $this->app->getContainer()->langPath() |
|
|
|
|
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @test */ |
57
|
|
|
public function it_has_a_resource_path_getter() |
58
|
|
|
{ |
59
|
|
|
$container = $this->app->getContainer(); |
60
|
|
|
|
61
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'resources', $container->resourcePath()); |
62
|
|
|
$this->assertEquals( |
63
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'js', |
64
|
|
|
$container->resourcePath('assets'.DIRECTORY_SEPARATOR.'js') |
65
|
|
|
); |
66
|
|
|
$this->assertEquals( |
67
|
|
|
BASE_PATH.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR.'app.php', |
68
|
|
|
$container->resourcePath('assets'.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR.'app.php') |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** @test */ |
73
|
|
|
public function it_has_a_storage_path_getter() |
74
|
|
|
{ |
75
|
|
|
$container = $this->app->getContainer(); |
76
|
|
|
|
77
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'storage', $container->storagePath()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @test */ |
81
|
|
|
public function it_has_environment_getter() |
82
|
|
|
{ |
83
|
|
|
$container = $this->app->getContainer(); |
84
|
|
|
|
85
|
|
|
$this->assertSame('Test version', $container->version()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @test */ |
89
|
|
|
public function it_confirms_the_running_in_console() |
90
|
|
|
{ |
91
|
|
|
$this->assertTrue( |
92
|
|
|
$this->app->getContainer()->runningInConsole() |
|
|
|
|
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @test */ |
97
|
|
|
public function it_has_namespace_getter() |
98
|
|
|
{ |
99
|
|
|
$this->assertEquals($this->app->getContainer()->getNamespace(), 'App\\'); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** @test */ |
103
|
|
|
public function it_confirms_that_is_not_down_for_maintenance() |
104
|
|
|
{ |
105
|
|
|
$this->assertFalse( |
106
|
|
|
$this->app->getContainer()->isDownForMaintenance() |
|
|
|
|
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** @test */ |
111
|
|
|
public function it_dont_implement_method_register_configured_providers() |
112
|
|
|
{ |
113
|
|
|
$this->expectException(NotImplementedException::class); |
114
|
|
|
|
115
|
|
|
$this->app->getContainer()->registerConfiguredProviders(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** @test */ |
119
|
|
|
public function it_dont_implement_method_register() |
120
|
|
|
{ |
121
|
|
|
$this->expectException(NotImplementedException::class); |
122
|
|
|
|
123
|
|
|
$this->app->getContainer()->register('foo'); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** @test */ |
127
|
|
|
public function it_dont_implement_method_register_deferred_provider() |
128
|
|
|
{ |
129
|
|
|
$this->expectException(NotImplementedException::class); |
130
|
|
|
|
131
|
|
|
$this->app->getContainer()->registerDeferredProvider('foo'); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** @test */ |
135
|
|
|
public function it_dont_implement_method_register_boot() |
136
|
|
|
{ |
137
|
|
|
$this->expectException(NotImplementedException::class); |
138
|
|
|
|
139
|
|
|
$this->app->getContainer()->boot(); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** @test */ |
143
|
|
|
public function it_dont_implement_method_register_booting() |
144
|
|
|
{ |
145
|
|
|
$this->expectException(NotImplementedException::class); |
146
|
|
|
|
147
|
|
|
$this->app->getContainer()->booting('foo'); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** @test */ |
151
|
|
|
public function it_dont_implement_method_register_booted() |
152
|
|
|
{ |
153
|
|
|
$this->expectException(NotImplementedException::class); |
154
|
|
|
|
155
|
|
|
$this->app->getContainer()->booted('foo'); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** @test */ |
159
|
|
|
public function it_dont_implement_method_get_cached_services_path() |
160
|
|
|
{ |
161
|
|
|
$this->expectException(NotImplementedException::class); |
162
|
|
|
|
163
|
|
|
$this->app->getContainer()->getCachedServicesPath(); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** @test */ |
167
|
|
|
public function it_configures_monolog() |
168
|
|
|
{ |
169
|
|
|
$container = $this->app->getContainer(); |
170
|
|
|
$callback = function () { |
171
|
|
|
}; |
172
|
|
|
|
173
|
|
|
$this->assertFalse($container->hasMonologConfigurator()); |
174
|
|
|
$this->app->getContainer()->configureMonologUsing($callback); |
|
|
|
|
175
|
|
|
$this->assertTrue($container->hasMonologConfigurator()); |
176
|
|
|
$this->assertSame($container->getMonologConfigurator(), $callback); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
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: