testCommandHasCorrectParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 37
nc 1
nop 0
dl 0
loc 51
rs 9.328
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File:GetModelDetailsCommandTest.php
6
 *
7
 * @author Maciej Sławik <[email protected]>
8
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
9
 */
10
11
namespace MSlwk\Otomoto\Cli\Test\Unit\Command;
12
13
use MSlwk\Otomoto\App\Manufacturer\Data\ManufacturerDTOInterface;
14
use MSlwk\Otomoto\App\Model\Data\ModelDTOInterface;
15
use MSlwk\Otomoto\App\Stats\Data\StatsDTOInterface;
16
use MSlwk\Otomoto\App\Stats\Filter\FilterFactory;
17
use MSlwk\Otomoto\App\Stats\Filter\FilterInterface;
18
use MSlwk\Otomoto\Cli\Command\GetModelDetailsCommand;
19
use MSlwk\Otomoto\Middleware\App\Stats\Stats;
20
use MSlwk\Otomoto\Middleware\App\Stats\StatsFactory;
21
use PHPUnit\Framework\MockObject\MockObject;
22
use PHPUnit\Framework\TestCase;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
/**
29
 * Class GetModelDetailsCommandTest
30
 * @package MSlwk\Otomoto\Cli\Test\Unit\Command
31
 */
32
class GetModelDetailsCommandTest extends TestCase
33
{
34
    /**
35
     * @var MockObject|StatsFactory
36
     */
37
    private $statsFactory;
38
39
    /**
40
     * @var MockObject|FilterFactory
41
     */
42
    private $filterFactory;
43
44
    /**
45
     * @var MockObject|GetModelDetailsCommand
46
     */
47
    private $command;
48
49
    /**
50
     * @var MockObject|InputInterface
51
     */
52
    private $input;
53
54
    /**
55
     * @var MockObject|OutputInterface
56
     */
57
    private $output;
58
59
    /**
60
     * @return void
61
     */
62
    protected function setUp()
63
    {
64
        $this->statsFactory = $this->getMockBuilder(StatsFactory::class)
65
            ->disableOriginalConstructor()
66
            ->getMock();
67
        $this->filterFactory = $this->getMockBuilder(FilterFactory::class)
68
            ->disableOriginalConstructor()
69
            ->getMock();
70
        $this->input = $this->getMockBuilder(InputInterface::class)
71
            ->getMock();
72
        $this->output = $this->getMockBuilder(OutputInterface::class)
73
            ->getMock();
74
75
        $this->command = new GetModelDetailsCommand(
76
            $this->statsFactory,
77
            $this->filterFactory
78
        );
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function testCommandHasCorrectName()
85
    {
86
        $this->assertEquals(GetModelDetailsCommand::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

86
        $this->assertEquals(GetModelDetailsCommand::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...
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function testCommandHasCorrectDescription()
93
    {
94
        $this->assertEquals(GetModelDetailsCommand::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

94
        $this->assertEquals(GetModelDetailsCommand::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...
95
    }
96
97
    /**
98
     * @test
99
     */
100
    public function testCommandHasCorrectParams()
101
    {
102
        $expectedArgumentCount = 2;
103
        $expectedOptionCount = 2;
104
        $expectedArgumentType = InputArgument::class;
105
        $expectedOptionType = InputOption::class;
106
107
        $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

107
        /** @scrutinizer ignore-call */ 
108
        $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...
108
109
        $this->assertEquals($expectedArgumentCount, $commandDefinition->getArgumentCount());
110
        $this->assertInstanceOf(
111
            $expectedArgumentType,
112
            $commandDefinition->getArgument(GetModelDetailsCommand::MANUFACTURER_ARG_NAME)
113
        );
114
        $this->assertInstanceOf(
115
            $expectedArgumentType,
116
            $commandDefinition->getArgument(GetModelDetailsCommand::MODEL_ARG_NAME)
117
        );
118
        $this->assertEquals(
119
            GetModelDetailsCommand::MANUFACTURER_ARG_DESC,
120
            $commandDefinition->getArgument(GetModelDetailsCommand::MANUFACTURER_ARG_NAME)->getDescription()
121
        );
122
        $this->assertEquals(
123
            GetModelDetailsCommand::MODEL_ARG_DESC,
124
            $commandDefinition->getArgument(GetModelDetailsCommand::MODEL_ARG_NAME)->getDescription()
125
        );
126
127
        $this->assertEquals($expectedOptionCount, count($commandDefinition->getOptions()));
128
        $this->assertInstanceOf(
129
            $expectedOptionType,
130
            $commandDefinition->getOption(GetModelDetailsCommand::FROM_YEAR_OPTION_NAME)
131
        );
132
        $this->assertInstanceOf(
133
            $expectedOptionType,
134
            $commandDefinition->getOption(GetModelDetailsCommand::TO_YEAR_OPTION_NAME)
135
        );
136
        $this->assertEquals(
137
            GetModelDetailsCommand::FROM_YEAR_OPTION_DESC,
138
            $commandDefinition->getOption(GetModelDetailsCommand::FROM_YEAR_OPTION_NAME)->getDescription()
139
        );
140
        $this->assertEquals(
141
            GetModelDetailsCommand::FROM_YEAR_OPTION_SHORTCUT,
142
            $commandDefinition->getOption(GetModelDetailsCommand::FROM_YEAR_OPTION_NAME)->getShortcut()
143
        );
144
        $this->assertEquals(
145
            GetModelDetailsCommand::TO_YEAR_OPTION_DESC,
146
            $commandDefinition->getOption(GetModelDetailsCommand::TO_YEAR_OPTION_NAME)->getDescription()
147
        );
148
        $this->assertEquals(
149
            GetModelDetailsCommand::TO_YEAR_OPTION_SHORTCUT,
150
            $commandDefinition->getOption(GetModelDetailsCommand::TO_YEAR_OPTION_NAME)->getShortcut()
151
        );
152
    }
153
154
    /**
155
     * @test
156
     */
157
    public function testExecuteWithFilters()
158
    {
159
        $this->input->expects($this->exactly(2))
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

159
        $this->input->/** @scrutinizer ignore-call */ 
160
                      expects($this->exactly(2))

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...
160
            ->method('getArgument')
161
            ->willReturnOnConsecutiveCalls('BMW', 'M3');
162
        $this->input->expects($this->once())
163
            ->method('getOptions')
164
            ->will(
165
                $this->returnValue(
166
                    [
167
                        'from' => 2015
168
                    ]
169
                )
170
            );
171
172
        $filter = $this->getMockBuilder(FilterInterface::class)
173
            ->getMock();
174
175
        $this->filterFactory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on MSlwk\Otomoto\App\Stats\Filter\FilterFactory. ( Ignorable by Annotation )

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

175
        $this->filterFactory->/** @scrutinizer ignore-call */ 
176
                              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...
176
            ->method('create')
177
            ->with('from', 2015)
178
            ->will($this->returnValue($filter));
179
180
        $statsMiddleware = $this->getMockBuilder(Stats::class)
181
            ->disableOriginalConstructor()
182
            ->getMock();
183
        $this->statsFactory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on MSlwk\Otomoto\Middleware\App\Stats\StatsFactory. ( Ignorable by Annotation )

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

183
        $this->statsFactory->/** @scrutinizer ignore-call */ 
184
                             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...
184
            ->method('create')
185
            ->will($this->returnValue($statsMiddleware));
186
187
        $stats = $this->getMockBuilder(StatsDTOInterface::class)
188
            ->disableOriginalConstructor()
189
            ->getMock();
190
        $statsMiddleware->expects($this->once())
191
            ->method('getStats')
192
            ->will($this->returnValue($stats));
193
194
        $stats->expects($this->once())
195
            ->method('getAverageMileage')
196
            ->will($this->returnValue(300.21));
197
        $stats->expects($this->once())
198
            ->method('getAveragePrice')
199
            ->will($this->returnValue(300.21));
200
        $stats->expects($this->once())
201
            ->method('getAverageYear')
202
            ->will($this->returnValue(300.21));
203
204
        $this->output->expects($this->exactly(6))
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

204
        $this->output->/** @scrutinizer ignore-call */ 
205
                       expects($this->exactly(6))

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...
205
            ->method('writeln');
206
207
        $reflection = new \ReflectionClass(get_class($this->command));
208
        $method = $reflection->getMethod('execute');
209
        $method->setAccessible(true);
210
211
        $method->invokeArgs($this->command, [$this->input, $this->output]);
212
    }
213
}
214