Completed
Push — master ( 89bf6a...4ff99e )
by Joachim
07:05
created

ReportCommandTest::getCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
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);
0 ignored issues
show
Bug introduced by
It seems like $manufacturerRepository defined by $this->getMockBuilder(\L...onstructor()->getMock() on line 49 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainConsi...tCommand::__construct() does only seem to accept object<Loevgaard\Dandoma...ManufacturerRepository>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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