Test Failed
Push — stable ( ba7d8d...8f8e37 )
by Nuno
01:57
created

ApplicationTest::it_guards_the_running_command()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Container\Container as the method environment() does only exist in the following implementations of said interface: LaravelZero\Framework\Container.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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