1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests; |
4
|
|
|
|
5
|
|
|
use Tests\TestCase; |
6
|
|
|
use Illuminate\Container\Container; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use Illuminate\Contracts\Config\Repository; |
9
|
|
|
use App\Commands\FakeFooCommand; |
10
|
|
|
use App\Commands\FakeDefaultCommand; |
11
|
|
|
use App\OtherCommands\FakeOtherCommand; |
12
|
|
|
use LaravelZero\Framework\Contracts\Application as ApplicationContract; |
13
|
|
|
|
14
|
|
|
class ApplicationTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** @test */ |
17
|
|
|
public function it_binds_core_alias(): void |
18
|
|
|
{ |
19
|
|
|
$container = $this->app->getContainer(); |
20
|
|
|
|
21
|
|
|
$this->assertSame($container, Container::getInstance()); |
22
|
|
|
$this->assertSame($container, $container->make('app')); |
23
|
|
|
$this->assertSame($container, $container->make(Container::class)); |
24
|
|
|
$this->assertSame($this->app, $container->make(ApplicationContract::class)); |
25
|
|
|
$this->assertInstanceOf(Repository::class, $container->make('config')); |
26
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'app', $container->make('path')); |
27
|
|
|
$this->assertEquals(BASE_PATH.DIRECTORY_SEPARATOR.'storage', $container->make('path.storage')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** @test */ |
31
|
|
|
public function it_reads_configuration_files() |
32
|
|
|
{ |
33
|
|
|
$this->assertSame('Test name', $this->app->getName()); |
34
|
|
|
$this->assertSame('Test version', $this->app->getVersion()); |
35
|
|
|
$this->assertEquals( |
36
|
|
|
$this->app->getContainer() |
|
|
|
|
37
|
|
|
->environment(), |
38
|
|
|
'development' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$this->assertEquals( |
42
|
|
|
$this->app->getContainer() |
43
|
|
|
->make('config') |
44
|
|
|
->get('foo.bar'), |
45
|
|
|
10 |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @test */ |
50
|
|
|
public function it_register_commands() |
51
|
|
|
{ |
52
|
|
|
$commands = [ |
53
|
|
|
FakeDefaultCommand::class, |
54
|
|
|
FakeFooCommand::class, |
55
|
|
|
FakeOtherCommand::class, |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$appCommands = collect($this->app->all()) |
59
|
|
|
->map( |
60
|
|
|
function ($command) { |
61
|
|
|
return get_class($command); |
62
|
|
|
} |
63
|
|
|
) |
64
|
|
|
->toArray(); |
65
|
|
|
|
66
|
|
|
foreach ($commands as $command) { |
67
|
|
|
$this->assertContains($command, $appCommands); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @test */ |
72
|
|
|
public function it_defines_core_constants() |
73
|
|
|
{ |
74
|
|
|
$this->assertEquals(ARTISAN_BINARY, base_path('phpunit')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @test */ |
78
|
|
|
public function it_allows_using_facades() |
79
|
|
|
{ |
80
|
|
|
$this->assertEquals( |
81
|
|
|
Config::get('app.name'), |
82
|
|
|
'Test name' |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @test */ |
87
|
|
|
public function it_register_service_providers() |
88
|
|
|
{ |
89
|
|
|
$app = $this->createApplication(); |
90
|
|
|
|
91
|
|
|
$this->assertSame( |
92
|
|
|
'bar', |
93
|
|
|
$app->getContainer() |
94
|
|
|
->make('foo') |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @test */ |
99
|
|
|
public function it_guards_the_running_command() |
100
|
|
|
{ |
101
|
|
|
$app = $this->createApplication(); |
102
|
|
|
|
103
|
|
|
$app->call('fake:default'); |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf(FakeDefaultCommand::class, $app->getRunningCommand()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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: