ReportCommandTest::getCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
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);
0 ignored issues
show
Bug introduced by
It seems like $manufacturerRepository defined by $this->getMockBuilder(\L...onstructor()->getMock() on line 50 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...
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