|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File: ListManufacturersCommandTest.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\Manufacturer\Data\ManufacturerDTO; |
|
12
|
|
|
use MSlwk\Otomoto\App\Manufacturer\Data\ManufacturerDTOArray; |
|
13
|
|
|
use MSlwk\Otomoto\Cli\Command\ListManufacturersCommand; |
|
14
|
|
|
use MSlwk\Otomoto\Middleware\App\Manufacturer\Manufacturer; |
|
15
|
|
|
use MSlwk\Otomoto\Middleware\App\Manufacturer\ManufacturerFactory; |
|
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class ListManufacturersCommandTest |
|
23
|
|
|
* @package MSlwk\Otomoto\Cli\Test\Command |
|
24
|
|
|
*/ |
|
25
|
|
|
class ListManufacturersCommandTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var MockObject|ManufacturerFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $manufacturerMiddlewareFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var MockObject|ListManufacturersCommand |
|
34
|
|
|
*/ |
|
35
|
|
|
private $command; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var MockObject|InputInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $input; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var MockObject|OutputInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $output; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var MockObject|Manufacturer |
|
49
|
|
|
*/ |
|
50
|
|
|
private $manufacturerMiddleware; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function setUp() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->manufacturerMiddlewareFactory = $this->getMockBuilder(ManufacturerFactory::class) |
|
58
|
|
|
->disableOriginalConstructor() |
|
59
|
|
|
->getMock(); |
|
60
|
|
|
$this->input = $this->getMockBuilder(InputInterface::class) |
|
61
|
|
|
->getMock(); |
|
62
|
|
|
$this->output = $this->getMockBuilder(OutputInterface::class) |
|
63
|
|
|
->getMock(); |
|
64
|
|
|
$this->manufacturerMiddleware = $this->getMockBuilder(Manufacturer::class) |
|
65
|
|
|
->disableOriginalConstructor() |
|
66
|
|
|
->getMock(); |
|
67
|
|
|
$this->command = new ListManufacturersCommand($this->manufacturerMiddlewareFactory); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function manufacturersDataProvider(): array |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
[ |
|
77
|
|
|
new ManufacturerDTOArray( |
|
78
|
|
|
new ManufacturerDTO('Audi'), |
|
79
|
|
|
new ManufacturerDTO('Volkswagen'), |
|
80
|
|
|
new ManufacturerDTO('Alpine'), |
|
81
|
|
|
new ManufacturerDTO('Opel'), |
|
82
|
|
|
new ManufacturerDTO('BMW') |
|
83
|
|
|
), |
|
84
|
|
|
4 |
|
85
|
|
|
], |
|
86
|
|
|
[ |
|
87
|
|
|
new ManufacturerDTOArray( |
|
88
|
|
|
new ManufacturerDTO('Audi'), |
|
89
|
|
|
new ManufacturerDTO('Volkswagen'), |
|
90
|
|
|
new ManufacturerDTO('Renault'), |
|
91
|
|
|
new ManufacturerDTO('Opel'), |
|
92
|
|
|
new ManufacturerDTO('BMW') |
|
93
|
|
|
), |
|
94
|
|
|
5 |
|
95
|
|
|
] |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @test |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testCommandHasCorrectName() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->assertEquals(ListManufacturersCommand::COMMAND_NAME, $this->command->getName()); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @test |
|
109
|
|
|
*/ |
|
110
|
|
|
public function testCommandHasCorrectDescription() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->assertEquals(ListManufacturersCommand::COMMAND_DESC, $this->command->getDescription()); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @test |
|
117
|
|
|
* @dataProvider manufacturersDataProvider |
|
118
|
|
|
* @param ManufacturerDTOArray $manufacturers |
|
119
|
|
|
* @param int $expectedCount |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testExecuteCallsWritelnCorrectly(ManufacturerDTOArray $manufacturers, int $expectedCount) |
|
122
|
|
|
{ |
|
123
|
|
|
|
|
124
|
|
|
$this->manufacturerMiddleware->expects($this->once()) |
|
|
|
|
|
|
125
|
|
|
->method('getManufacturers') |
|
126
|
|
|
->will($this->returnValue($manufacturers)); |
|
127
|
|
|
|
|
128
|
|
|
$this->manufacturerMiddlewareFactory->expects($this->once()) |
|
|
|
|
|
|
129
|
|
|
->method('create') |
|
130
|
|
|
->will($this->returnValue($this->manufacturerMiddleware)); |
|
131
|
|
|
|
|
132
|
|
|
$this->output->expects($this->exactly($expectedCount)) |
|
|
|
|
|
|
133
|
|
|
->method('writeln'); |
|
134
|
|
|
|
|
135
|
|
|
$reflection = new \ReflectionClass(get_class($this->command)); |
|
136
|
|
|
$method = $reflection->getMethod('execute'); |
|
137
|
|
|
$method->setAccessible(true); |
|
138
|
|
|
|
|
139
|
|
|
$method->invokeArgs($this->command, [$this->input, $this->output]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @test |
|
144
|
|
|
*/ |
|
145
|
|
|
public function testGroupManufacturersByFirstLetter() |
|
146
|
|
|
{ |
|
147
|
|
|
$manufacturers = new ManufacturerDTOArray( |
|
148
|
|
|
new ManufacturerDTO('Audi'), |
|
149
|
|
|
new ManufacturerDTO('Volkswagen'), |
|
150
|
|
|
new ManufacturerDTO('Renault'), |
|
151
|
|
|
new ManufacturerDTO('Bugatti'), |
|
152
|
|
|
new ManufacturerDTO('BMW') |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
$reflection = new \ReflectionClass(get_class($this->command)); |
|
156
|
|
|
$method = $reflection->getMethod('groupManufacturersByFirstLetter'); |
|
157
|
|
|
$method->setAccessible(true); |
|
158
|
|
|
|
|
159
|
|
|
$result = $method->invokeArgs($this->command, [$manufacturers]); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertNotEmpty($result); |
|
162
|
|
|
$this->assertEquals(4, count($result)); |
|
163
|
|
|
$this->assertEquals('A: Audi', $result['A']); |
|
164
|
|
|
$this->assertEquals('B: Bugatti, BMW', $result['B']); |
|
165
|
|
|
$this->assertEquals('R: Renault', $result['R']); |
|
166
|
|
|
$this->assertEquals('V: Volkswagen', $result['V']); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
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.