Passed
Pull Request — master (#57)
by Matthew
15:03
created

CommandTrait::runStubCommand()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 4
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Command;
4
5
use Dtc\QueueBundle\Model\Job;
6
use Dtc\QueueBundle\Model\JobTiming;
7
use Dtc\QueueBundle\Manager\JobTimingManager;
8
use Dtc\QueueBundle\Tests\StubJobManager;
9
use Dtc\QueueBundle\Tests\StubRunManager;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
13
use Symfony\Component\DependencyInjection\Container;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
trait CommandTrait
17
{
18
    /**
19
     * @param string             $commandClass
20
     * @param ContainerInterface $container
21
     * @param array              $params
22
     */
23
    protected function runCommand($commandClass, ContainerInterface $container, array $params)
24
    {
25
        $this->runCommandExpect($commandClass, $container, $params, 0);
26
    }
27
28
    /**
29
     * @param string             $commandClass
30
     * @param ContainerInterface $container
31
     * @param array              $params
32
     */
33
    private function prepCommand($commandClass, ContainerInterface $container, array $params)
34
    {
35
        $command = new $commandClass();
36
        $command->setContainer($container);
37
        $input = new ArrayInput($params);
38
        $output = new DummyOutput();
39
40
        return [$command, $input, $output];
41
    }
42
43
    /**
44
     * @param string             $commandClass
45
     * @param ContainerInterface $container
46
     * @param array              $params
47
     */
48
    protected function runCommandException($commandClass, ContainerInterface $container, array $params)
49
    {
50
        list($command, $input, $output) = $this->prepCommand($commandClass, $container, $params);
51
        $failed = false;
52
        try {
53
            $command->run($input, $output);
54
            $failed = true;
55
        } catch (\Exception $exception) {
56
            TestCase::assertTrue(true);
57
        }
58
        TestCase::assertFalse($failed);
59
    }
60
61
    /**
62
     * @param string             $commandClass
63
     * @param ContainerInterface $container
64
     * @param array              $params
65
     * @param int                $expectedResult
66
     */
67
    protected function runCommandExpect($commandClass, ContainerInterface $container, array $params, $expectedResult)
68
    {
69
        list($command, $input, $output) = $this->prepCommand($commandClass, $container, $params);
70
        try {
71
            $result = $command->run($input, $output);
72
        } catch (\Exception $exception) {
73
            TestCase::fail("Shouldn't throw exception: ".get_class($exception).' - '.$exception->getMessage());
74
75
            return;
76
        }
77
        TestCase::assertEquals($expectedResult, $result);
78
    }
79
80
    protected function runStubCommand($className, $params, $call, $expectedResult = 0)
81
    {
82
        $managerType = 'job';
83
        if (false !== strrpos($call, 'Runs') || false !== strrpos($call, 'Timings')) {
84
            $managerType = 'run';
85
        }
86
87
        $jobTimingManager = new JobTimingManager(JobTiming::class, false);
88
        $runManager = new StubRunManager($jobTimingManager, \Dtc\QueueBundle\Model\Run::class);
0 ignored issues
show
Unused Code introduced by
The call to Dtc\QueueBundle\Tests\St...nManager::__construct() has too many arguments starting with Dtc\QueueBundle\Model\Run::class. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $runManager = /** @scrutinizer ignore-call */ new StubRunManager($jobTimingManager, \Dtc\QueueBundle\Model\Run::class);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
89
        $jobManager = new StubJobManager($runManager, $jobTimingManager, Job::class);
90
        $container = new Container();
91
        $container->set('dtc_queue.manager.job', $jobManager);
92
        $container->set('dtc_queue.manager.run', $runManager);
93
        $this->runCommandExpect($className, $container, $params, $expectedResult);
94
        $manager = "${managerType}Manager";
95
        if (0 === $expectedResult) {
96
            self::assertTrue(isset($$manager->calls[$call][0]));
97
            self::assertTrue(!isset($$manager->calls[$call][1]));
98
        }
99
100
        return $$manager;
101
    }
102
}
103