1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Loevgaard\DandomainConsignmentBundle\Tests\Command; |
6
|
|
|
|
7
|
|
|
use Loevgaard\DandomainConsignmentBundle\Command\ReportCommand; |
8
|
|
|
use Loevgaard\DandomainConsignmentBundle\ConsignmentService\ConsignmentServiceCollection; |
9
|
|
|
use Loevgaard\DandomainConsignmentBundle\Exception\InvalidDateFormatException; |
10
|
|
|
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentManufacturerException; |
11
|
|
|
use Loevgaard\DandomainFoundation\Repository\ManufacturerRepository; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\Console\Application; |
14
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
16
|
|
|
|
17
|
|
|
class ReportCommandTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testExpectNonExistentManufacturer() |
20
|
|
|
{ |
21
|
|
|
$this->expectException(NonExistentManufacturerException::class); |
22
|
|
|
|
23
|
|
|
$command = $this->getCommand(); |
24
|
|
|
$this->execute($command); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testExpectWrongStartDateFormat() |
28
|
|
|
{ |
29
|
|
|
$this->expectException(InvalidDateFormatException::class); |
30
|
|
|
|
31
|
|
|
$command = $this->getCommand(); |
32
|
|
|
$this->execute($command, 'm', '20180101'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testExpectWrongEndDateFormat() |
36
|
|
|
{ |
37
|
|
|
$this->expectException(InvalidDateFormatException::class); |
38
|
|
|
|
39
|
|
|
$command = $this->getCommand(); |
40
|
|
|
$this->execute($command, 'm', null, '20180101'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function getCommand($container = null): ReportCommand |
44
|
|
|
{ |
45
|
|
|
if (!$container) { |
46
|
|
|
$container = $this->getContainer(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ManufacturerRepository $manufacturerRepository */ |
50
|
|
|
$manufacturerRepository = $this->getMockBuilder(ManufacturerRepository::class)->disableOriginalConstructor()->getMock(); |
51
|
|
|
$manufacturerRepository->method('findOneByExternalId')->willReturn(null); |
52
|
|
|
|
53
|
|
|
$consignmentServiceCollection = new ConsignmentServiceCollection(); |
54
|
|
|
|
55
|
|
|
$command = new ReportCommand($manufacturerRepository, $consignmentServiceCollection); |
|
|
|
|
56
|
|
|
$command->setContainer($container); |
57
|
|
|
|
58
|
|
|
return $command; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function execute(ReportCommand $command, $manufacturer = 'manufacturer', $start = null, $end = null): CommandTester |
62
|
|
|
{ |
63
|
|
|
$application = new Application(); |
64
|
|
|
$application->setAutoExit(false); |
65
|
|
|
$application->add($command); |
66
|
|
|
|
67
|
|
|
$input = [ |
68
|
|
|
'command' => $command->getName(), |
69
|
|
|
'manufacturer' => $manufacturer, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
if ($start) { |
73
|
|
|
$input['--start'] = $start; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($end) { |
77
|
|
|
$input['--end'] = $end; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$command = $application->find('loevgaard:dandomain-consignment:report'); |
81
|
|
|
$commandTester = new CommandTester($command); |
82
|
|
|
$commandTester->execute($input); |
83
|
|
|
|
84
|
|
|
return $commandTester; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface |
89
|
|
|
*/ |
90
|
|
|
private function getContainer() |
91
|
|
|
{ |
92
|
|
|
return $this->createMock(ContainerInterface::class); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.