Passed
Branch master (e8fd46)
by Alexey
03:15
created

ConsoleApplicationTest::canRunApplication()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 46
rs 8.9411
c 0
b 0
f 0
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use Symfony\Component\Console\Input\ArrayInput;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\BufferedOutput;
7
use Symfony\Component\Console\Output\NullOutput;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Venta\Console\Command;
10
use Venta\Contracts\Console\CommandCollector;
11
use Venta\Contracts\Container\Container;
12
use Venta\Contracts\Kernel\Kernel;
13
use Whoops\RunInterface;
14
15
/**
16
 * Class ConsoleApplicationTest
17
 */
18
class ConsoleApplicationTest extends TestCase
19
{
20
21
    public function tearDown()
22
    {
23
        Mockery::close();
24
    }
25
26
    /**
27
     * @test
28
     */
29
    public function canBootKernel()
30
    {
31
        $kernel = Mockery::mock(Kernel::class);
32
        $kernel->shouldReceive('boot')->withNoArgs()->andReturn(Mockery::mock(Container::class))->once();
33
        $kernel->shouldReceive('getVersion')->withNoArgs()->andReturn('test')->once();
34
35
        $app = new \Venta\Console\ConsoleApplication($kernel);
36
37
        $this->assertSame('Venta', $app->getName());
38
        $this->assertSame('test', $app->getVersion());
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function canHandleAndRenderException()
45
    {
46
        // Creating mocks
47
        $kernel = Mockery::mock(Kernel::class);
48
        $container = Mockery::mock(Container::class);
49
        $errorHandler = Mockery::mock(RunInterface::class);
50
        $exception = new Exception('Exception message');
51
        $output = new BufferedOutput();
52
53
        // Mocking method calls
54
        $kernel->shouldReceive('boot')->withNoArgs()->andReturn($container)->once();
55
        $kernel->shouldReceive('getVersion')->withNoArgs()->andReturn('test')->once();
56
        $container->shouldReceive('has')->with('error_handler')->andReturn(true)->once();
57
        $container->shouldReceive('get')->with('error_handler')->andReturn($errorHandler)->once();
58
        $errorHandler->shouldReceive('handleException')->with($exception)->once();
59
        $errorHandler->shouldIgnoreMissing();
60
61
        // Creating application and handling exception
62
        $app = new \Venta\Console\ConsoleApplication($kernel);
63
        // We expect nothing to be echoed directly to output
64
        // Every single string should be passed to output object
65
        $this->expectOutputString('');
66
        $app->renderException($exception, $output);
67
68
        // Assert output contains exception message (exception is actually rendered)
69
        $this->assertContains('Exception message', $output->fetch());
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function canRunApplication()
76
    {
77
        // Creating mock stubs
78
        $kernel = Mockery::mock(Kernel::class);
79
        $container = Mockery::mock(Container::class);
80
        $collector = Mockery::mock(CommandCollector::class);
81
82
        // Creating input and output for test environment
83
        $input = new ArrayInput([]);
84
        $output = new NullOutput();
85
86
        // Mocking stub method calls
87
        $kernel->shouldReceive('boot')->withNoArgs()->andReturn($container)->once();
88
        $kernel->shouldReceive('getVersion')->withNoArgs()->andReturn('test')->once();
89
        $container->shouldReceive('bindInstance')->with(InputInterface::class, $input)->once();
90
        $container->shouldReceive('bindInstance')->with(OutputInterface::class, $output)->once();
91
        $container->shouldReceive('get')->with(CommandCollector::class)->andReturn($collector)->once();
92
        $container->shouldReceive('get')->with(InputInterface::class)->andReturn($input)->once();
93
        $container->shouldReceive('get')->with(OutputInterface::class)->andReturn($output)->once();
94
        $collector->shouldReceive('getCommands')->withNoArgs()->andReturn([
95
            new class extends Command
96
            {
97
98
                public function signature(): string
99
                {
100
                    return 'test';
101
                }
102
103
                public function description(): string
104
                {
105
                    return 'test command';
106
                }
107
108
                public function handle(InputInterface $input, OutputInterface $output)
109
                {
110
                    // this is faux command that does nothing
111
                }
112
113
            },
114
        ])->once();
115
116
        // Creating and running application
117
        $app = new \Venta\Console\ConsoleApplication($kernel);
118
        $app->setAutoExit(false);
119
        $app->run($input, $output);
120
    }
121
122
}
123