Completed
Push — master ( 2db41f...aa6ae6 )
by Matthew
15:06 queued 36s
created

CommandTrait::prepCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
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\Model\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
            TestCase::assertEquals($expectedResult, $result);
73
        } catch (\Exception $exception) {
74
            TestCase::fail("Shouldn't throw exception: ".get_class($exception).' - '.$exception->getMessage());
75
        }
76
    }
77
78
    protected function runStubCommand($className, $params, $call, $expectedResult = 0)
79
    {
80
        $managerType = 'job';
81
        if (false !== strrpos($call, 'Runs') || false !== strrpos($call, 'Timings')) {
82
            $managerType = 'run';
83
        }
84
85
        $jobTimingManager = new JobTimingManager(JobTiming::class, false);
86
        $runManager = new StubRunManager($jobTimingManager, \Dtc\QueueBundle\Model\Run::class);
0 ignored issues
show
Unused Code introduced by
The call to StubRunManager::__construct() has too many arguments starting with \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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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