Completed
Push — master ( 058941...ab4289 )
by Wachter
05:40
created

ScheduleTaskCommandTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 2 Features 3
Metric Value
wmc 6
c 4
b 2
f 3
lcom 1
cbo 5
dl 0
loc 85
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testConfigure() 0 9 1
A runProvider() 0 14 1
A testRun() 0 54 4
1
<?php
2
3
namespace Unit\Command;
4
5
use Prophecy\Argument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Task\SchedulerInterface;
9
use Task\TaskBuilderInterface;
10
use Task\TaskBundle\Command\ScheduleTaskCommand;
11
12
class ScheduleTaskCommandTest extends \PHPUnit_Framework_TestCase
13
{
14
    public function testConfigure()
15
    {
16
        $scheduler = $this->prophesize(SchedulerInterface::class);
17
        $command = new ScheduleTaskCommand('task:schedule:task', $scheduler->reveal());
18
19
        $this->assertEquals('task:schedule:task', $command->getName());
20
        $this->assertTrue($command->getDefinition()->hasArgument('handler'));
21
        $this->assertTrue($command->getDefinition()->hasArgument('workload'));
22
    }
23
24
    public function runProvider()
25
    {
26
        return [
27
            ['test-handler'],
28
            ['test-handler', 'test-workload'],
29
            ['test-handler-1', 'test-workload-1'],
30
            ['test-handler', 'test-workload', '1 * * * *'],
31
            ['test-handler', 'test-workload', '1 * * * *', '+1 week'],
32
            ['test-handler', 'test-workload', '1 * * * *', '+1 week', 'test-key'],
33
            ['test-handler', 'test-workload', '1 * * * *', null, 'test-key'],
34
            ['test-handler', 'test-workload', null, null, 'test-key'],
35
            ['test-handler', 'test-workload', null, '+1 week', 'test-key'],
36
        ];
37
    }
38
39
    /**
40
     * @dataProvider runProvider
41
     */
42
    public function testRun($handler, $workload = null, $cronExpression = null, $endDateString = null, $key = null)
43
    {
44
        $taskBuilder = $this->prophesize(TaskBuilderInterface::class);
45
46
        $input = $this->prophesize(InputInterface::class);
47
        $output = $this->prophesize(OutputInterface::class);
48
49
        $input->bind(Argument::any())->willReturn(true);
50
        $input->validate()->willReturn(true);
51
        $input->isInteractive()->willReturn(false);
52
        $input->hasArgument('command')->willReturn(false);
53
54
        $input->getArgument('handler')->willReturn($handler);
55
        $input->getArgument('workload')->willReturn($workload);
56
        $input->getOption('cron-expression')->willReturn($cronExpression);
57
        $input->getOption('end-date')->willReturn($endDateString);
58
        $input->getOption('key')->willReturn($key);
59
60
        $scheduler = $this->prophesize(SchedulerInterface::class);
61
        $command = new ScheduleTaskCommand('task:schedule:task', $scheduler->reveal());
62
63
        $scheduler->createTask($handler, $workload)->shouldBeCalledTimes(1)->willReturn($taskBuilder->reveal());
64
65
        if ($key !== null) {
66
            $taskBuilder->setKey($key)->shouldBeCalled();
67
        } else {
68
            $taskBuilder->setKey(Argument::any())->shouldNotBeCalled();
69
        }
70
        if ($cronExpression !== null) {
71
            $endDate = null;
72
            if ($endDateString !== null) {
73
                $endDate = new \DateTime($endDateString);
74
            }
75
76
            $taskBuilder->cron(
77
                $cronExpression,
78
                Argument::type(\DateTime::class),
79
                Argument::that(
80
                    function ($argument) use ($endDate) {
81
                        $this->assertEquals($endDate, $argument, '', 2);
82
83
                        return true;
84
                    }
85
                )
86
            )->shouldBeCalled()
87
                ->willReturn($taskBuilder->reveal());
88
        } else {
89
            $taskBuilder->cron(Argument::any(), Argument::any(), Argument::any())->shouldNotBeCalled()
90
                ->willReturn($taskBuilder->reveal());
91
        }
92
        $taskBuilder->schedule()->shouldBeCalledTimes(1);
93
94
        $command->run($input->reveal(), $output->reveal());
95
    }
96
}
97