1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainConsignmentBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Loevgaard\DandomainConsignmentBundle\Command\ReportCommand; |
6
|
|
|
use Loevgaard\DandomainConsignmentBundle\ConsignmentService\ConsignmentServiceCollection; |
7
|
|
|
use Loevgaard\DandomainFoundation\Repository\ManufacturerRepository; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\Console\Application; |
11
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
12
|
|
|
|
13
|
|
|
class ReportCommandTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testExpectNonExistentManufacturer() |
16
|
|
|
{ |
17
|
|
|
$this->expectException(\InvalidArgumentException::class); |
18
|
|
|
$this->expectExceptionMessage('The manufacturer does not exist'); |
19
|
|
|
|
20
|
|
|
$command = $this->getCommand(); |
21
|
|
|
$this->execute($command); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testExpectWrongStartDateFormat() |
25
|
|
|
{ |
26
|
|
|
$this->expectException(\InvalidArgumentException::class); |
27
|
|
|
$this->expectExceptionMessage('The format for start is invalid'); |
28
|
|
|
|
29
|
|
|
$command = $this->getCommand(); |
30
|
|
|
$this->execute($command, 'm', '20180101'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testExpectWrongEndDateFormat() |
34
|
|
|
{ |
35
|
|
|
$this->expectException(\InvalidArgumentException::class); |
36
|
|
|
$this->expectExceptionMessage('The format for end is invalid'); |
37
|
|
|
|
38
|
|
|
$command = $this->getCommand(); |
39
|
|
|
$this->execute($command, 'm', null, '20180101'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function getCommand($container = null): ReportCommand |
43
|
|
|
{ |
44
|
|
|
if (!$container) { |
45
|
|
|
$container = $this->getContainer(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ManufacturerRepository $manufacturerRepository */ |
49
|
|
|
$manufacturerRepository = $this->getMockBuilder(ManufacturerRepository::class)->disableOriginalConstructor()->getMock(); |
50
|
|
|
|
51
|
|
|
$consignmentServiceCollection = new ConsignmentServiceCollection(); |
52
|
|
|
|
53
|
|
|
$command = new ReportCommand($manufacturerRepository, $consignmentServiceCollection); |
|
|
|
|
54
|
|
|
$command->setContainer($container); |
55
|
|
|
|
56
|
|
|
return $command; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function execute(ReportCommand $command, $manufacturer = 'manufacturer', $start = null, $end = null) : CommandTester |
60
|
|
|
{ |
61
|
|
|
$application = new Application(); |
62
|
|
|
$application->setAutoExit(false); |
63
|
|
|
$application->add($command); |
64
|
|
|
|
65
|
|
|
$input = [ |
66
|
|
|
'command' => $command->getName(), |
67
|
|
|
'manufacturer' => $manufacturer, |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
if($start) { |
71
|
|
|
$input['--start'] = $start; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if($end) { |
75
|
|
|
$input['--end'] = $end; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$command = $application->find('loevgaard:dandomain-consignment:report'); |
79
|
|
|
$commandTester = new CommandTester($command); |
80
|
|
|
$commandTester->execute($input); |
81
|
|
|
|
82
|
|
|
return $commandTester; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface |
87
|
|
|
*/ |
88
|
|
|
private function getContainer() |
89
|
|
|
{ |
90
|
|
|
return $this->createMock(ContainerInterface::class); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.