Test Failed
Push — stable ( 9139cf...ac521b )
by Nuno
02:03
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 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Tests\Integration;
4
5
use Tests\TestCase;
6
use Illuminate\Container\Container;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Contracts\Config\Repository;
9
use Tests\Application\App\Commands\FakeFooCommand;
10
use Tests\Application\App\Commands\FakeDefaultCommand;
11
use Tests\Application\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
        // @todo Test production config.
34
35
        $this->assertSame('Test name', $this->app->getName());
36
        $this->assertSame('Test version', $this->app->getVersion());
37
        $this->assertEquals(
38
            $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...
39
                ->environment(),
40
            'development'
41
        );
42
43
        $this->assertEquals(
44
            $this->app->getContainer()
45
                ->make('config')
46
                ->get('foo.bar'),
47
            10
48
        );
49
    }
50
51
    /** @test */
52
    public function it_register_commands()
53
    {
54
        $commands = [
55
            FakeDefaultCommand::class,
56
            FakeFooCommand::class,
57
            FakeOtherCommand::class,
58
        ];
59
60
        $appCommands = collect($this->app->all())
61
            ->map(
62
                function ($command) {
63
                    return get_class($command);
64
                }
65
            )
66
            ->toArray();
67
68
        foreach ($commands as $command) {
69
            $this->assertContains($command, $appCommands);
70
        }
71
    }
72
73
    /** @test */
74
    public function it_defines_core_constants()
75
    {
76
        $this->assertEquals(ARTISAN_BINARY, base_path('phpunit'));
77
    }
78
79
    /** @test */
80
    public function it_allows_using_facades()
81
    {
82
        $this->assertEquals(
83
            Config::get('app.name'),
84
            'Test name'
85
        );
86
    }
87
88
    /** @test */
89
    public function it_register_service_providers()
90
    {
91
        $app = $this->createApplication();
92
93
        $this->assertSame(
94
            'bar',
95
            $app->getContainer()
96
                ->make('foo')
97
        );
98
    }
99
100
    /** @test */
101
    public function it_guards_the_running_command()
102
    {
103
        $app = $this->createApplication();
104
105
        $app->call('fake:default');
106
107
        $this->assertInstanceOf(FakeDefaultCommand::class, $app->getRunningCommand());
108
    }
109
}
110