ListModelsCommandTest::modelsDataProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 22
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * File: ListModelsCommandTest.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\Otomoto\Cli\Test\Unit\Command;
10
11
use MSlwk\Otomoto\App\Model\Data\ModelDTO;
12
use MSlwk\Otomoto\App\Model\Data\ModelDTOArray;
13
use PHPUnit\Framework\TestCase;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use MSlwk\Otomoto\Middleware\App\Model\ModelFactory;
19
use MSlwk\Otomoto\Middleware\App\Model\Model;
20
use MSlwk\Otomoto\Cli\Command\ListModelsCommand;
21
22
/**
23
 * Class ListModelsCommandTest
24
 * @package MSlwk\Otomoto\Cli\Test\Unit\Command
25
 */
26
class ListModelsCommandTest extends TestCase
27
{
28
    /**
29
     * @var MockObject|ModelFactory
30
     */
31
    private $modelMiddlewareFactory;
32
33
    /**
34
     * @var MockObject|ListModelsCommand
35
     */
36
    private $command;
37
38
    /**
39
     * @var MockObject|InputInterface
40
     */
41
    private $input;
42
43
    /**
44
     * @var MockObject|OutputInterface
45
     */
46
    private $output;
47
48
    /**
49
     * @var MockObject|Model
50
     */
51
    private $modelMiddleware;
52
53
    /**
54
     * @return void
55
     */
56
    protected function setUp()
57
    {
58
        $this->modelMiddlewareFactory = $this->getMockBuilder(ModelFactory::class)
59
            ->disableOriginalConstructor()
60
            ->getMock();
61
        $this->input = $this->getMockBuilder(InputInterface::class)
62
            ->getMock();
63
        $this->output = $this->getMockBuilder(OutputInterface::class)
64
            ->getMock();
65
        $this->modelMiddleware = $this->getMockBuilder(Model::class)
66
            ->disableOriginalConstructor()
67
            ->getMock();
68
        $this->command = new ListModelsCommand($this->modelMiddlewareFactory);
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function modelsDataProvider(): array
75
    {
76
        return [
77
            [
78
                new ModelDTOArray(
79
                    new ModelDTO('Giulia'),
80
                    new ModelDTO('Giulietta'),
81
                    new ModelDTO('Stelvio'),
82
                    new ModelDTO('GTV'),
83
                    new ModelDTO('Brera')
84
                ),
85
                3
86
            ],
87
            [
88
                new ModelDTOArray(
89
                    new ModelDTO('Auris'),
90
                    new ModelDTO('Supra'),
91
                    new ModelDTO('RAV4'),
92
                    new ModelDTO('Hilux'),
93
                    new ModelDTO('GT86')
94
                ),
95
                5
96
            ]
97
        ];
98
    }
99
100
    /**
101
     * @test
102
     */
103
    public function testCommandHasCorrectName()
104
    {
105
        $this->assertEquals(ListModelsCommand::COMMAND_NAME, $this->command->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        $this->assertEquals(ListModelsCommand::COMMAND_NAME, $this->command->/** @scrutinizer ignore-call */ getName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function testCommandHasCorrectDescription()
112
    {
113
        $this->assertEquals(ListModelsCommand::COMMAND_DESC, $this->command->getDescription());
0 ignored issues
show
Bug introduced by
The method getDescription() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
        $this->assertEquals(ListModelsCommand::COMMAND_DESC, $this->command->/** @scrutinizer ignore-call */ getDescription());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
    }
115
116
    /**
117
     * @test
118
     */
119
    public function testCommandHasCorrectArguments()
120
    {
121
        $commandDefinition = $this->command->getDefinition();
0 ignored issues
show
Bug introduced by
The method getDefinition() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        /** @scrutinizer ignore-call */ 
122
        $commandDefinition = $this->command->getDefinition();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
123
        $expectedCount = 1;
124
        $expectedType = InputArgument::class;
125
        $expectedDescription = ListModelsCommand::MANUFACTURER_ARG_DESC;
126
        $this->assertEquals($expectedCount, $commandDefinition->getArgumentCount());
127
        $this->assertInstanceOf(
128
            $expectedType,
129
            $commandDefinition->getArgument(ListModelsCommand::MANUFACTURER_ARG_NAME)
130
        );
131
        $this->assertEquals(
132
            $expectedDescription,
133
            $commandDefinition->getArgument(ListModelsCommand::MANUFACTURER_ARG_NAME)->getDescription()
134
        );
135
    }
136
137
    /**
138
     * @test
139
     * @dataProvider modelsDataProvider
140
     * @param ModelDTOArray $models
141
     * @param int $expectedCount
142
     */
143
    public function testExecuteCallsWritelnCorrectly(ModelDTOArray $models, int $expectedCount)
144
    {
145
        $this->input->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Console\Input\InputInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        $this->input->/** @scrutinizer ignore-call */ 
146
                      expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
            ->method('getArgument')
147
            ->will($this->returnValue('Audi'));
148
        $this->modelMiddleware->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on MSlwk\Otomoto\Middleware\App\Model\Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        $this->modelMiddleware->/** @scrutinizer ignore-call */ 
149
                                expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
            ->method('getModels')
150
            ->will($this->returnValue($models));
151
152
        $this->modelMiddlewareFactory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on MSlwk\Otomoto\Middleware\App\Model\ModelFactory. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
        $this->modelMiddlewareFactory->/** @scrutinizer ignore-call */ 
153
                                       expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
            ->method('create')
154
            ->will($this->returnValue($this->modelMiddleware));
155
156
        $this->output->expects($this->exactly($expectedCount))
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Console\Output\OutputInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
        $this->output->/** @scrutinizer ignore-call */ 
157
                       expects($this->exactly($expectedCount))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
            ->method('writeln');
158
159
        $reflection = new \ReflectionClass(get_class($this->command));
160
        $method = $reflection->getMethod('execute');
161
        $method->setAccessible(true);
162
163
        $method->invokeArgs($this->command, [$this->input, $this->output]);
164
    }
165
166
    /**
167
     * @test
168
     */
169
    public function testGroupModelsByFirstLetter()
170
    {
171
        $models = new ModelDTOArray(
172
            new ModelDTO('Gallardo'),
173
            new ModelDTO('Reventon'),
174
            new ModelDTO('Murcielargo'),
175
            new ModelDTO('Miura'),
176
            new ModelDTO('Huracan')
177
        );
178
179
        $reflection = new \ReflectionClass(get_class($this->command));
180
        $method = $reflection->getMethod('groupModelsByFirstLetter');
181
        $method->setAccessible(true);
182
183
        $result = $method->invokeArgs($this->command, [$models]);
184
185
        $this->assertNotEmpty($result);
186
        $this->assertEquals(4, count($result));
187
        $this->assertEquals('G: Gallardo', $result['G']);
188
        $this->assertEquals('H: Huracan', $result['H']);
189
        $this->assertEquals('M: Murcielargo, Miura', $result['M']);
190
        $this->assertEquals('R: Reventon', $result['R']);
191
    }
192
}
193