Completed
Pull Request — master (#20)
by Wachter
04:37
created

testExecuteWithExecutionDate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 24
rs 8.9713
c 1
b 0
f 1
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Functional\Command;
13
14
use Task\TaskBundle\Tests\Functional\BaseCommandTestCase;
15
use Task\TaskBundle\Tests\Functional\TestHandler;
16
17
/**
18
 * Tests for ScheduleTaskCommand.
19
 */
20
class ScheduleTaskCommandTest extends BaseCommandTestCase
21
{
22
    public function testExecute()
23
    {
24
        $this->commandTester->execute(
25
            [
26
                'command' => $this->command->getName(),
27
                'handlerClass' => TestHandler::class,
28
            ]
29
        );
30
31
        $tasks = $this->taskRepository->findAll();
32
        $this->assertCount(1, $tasks);
33
34
        $this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass());
35
    }
36
37
    public function testExecuteWithWorkload()
38
    {
39
        $this->commandTester->execute(
40
            [
41
                'command' => $this->command->getName(),
42
                'handlerClass' => TestHandler::class,
43
                'workload' => 'Test workload 1',
44
            ]
45
        );
46
47
        $tasks = $this->taskRepository->findAll();
48
        $this->assertCount(1, $tasks);
49
50
        $this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass());
51
        $this->assertEquals('Test workload 1', $tasks[0]->getWorkload());
52
    }
53
54
    public function testExecuteWithWorkloadAndInterval()
55
    {
56
        $this->commandTester->execute(
57
            [
58
                'command' => $this->command->getName(),
59
                'handlerClass' => TestHandler::class,
60
                'workload' => 'Test workload 1',
61
                '--cron-expression' => '0 * * * *',
62
            ]
63
        );
64
65
        $tasks = $this->taskRepository->findAll();
66
        $this->assertCount(1, $tasks);
67
68
        $this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass());
69
        $this->assertEquals('Test workload 1', $tasks[0]->getWorkload());
70
        $this->assertEquals('0 * * * *', $tasks[0]->getInterval());
71
    }
72
73
    public function testExecuteWithWorkloadAndIntervalAndEndDate()
74
    {
75
        $date = new \DateTime('+1 day');
76
        $this->commandTester->execute(
77
            [
78
                'command' => $this->command->getName(),
79
                'handlerClass' => TestHandler::class,
80
                'workload' => 'Test workload 1',
81
                '--cron-expression' => '0 * * * *',
82
                '--end-date' => $date->format(\DateTime::RFC3339),
83
            ]
84
        );
85
86
        $tasks = $this->taskRepository->findAll();
87
        $this->assertCount(1, $tasks);
88
89
        $this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass());
90
        $this->assertEquals('Test workload 1', $tasks[0]->getWorkload());
91
        $this->assertEquals('0 * * * *', $tasks[0]->getInterval());
92
        $this->assertEquals($date, $tasks[0]->getLastExecution());
93
    }
94
95
    public function testExecuteWithExecutionDate()
96
    {
97
        $date = new \DateTime('+1 day');
98
        $this->commandTester->execute(
99
            [
100
                'command' => $this->command->getName(),
101
                'handlerClass' => TestHandler::class,
102
                'workload' => 'Test workload 1',
103
                '--execution-date' => '+1 day',
104
            ]
105
        );
106
107
        $tasks = $this->taskRepository->findAll();
108
        $this->assertCount(1, $tasks);
109
110
        $this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass());
111
        $this->assertEquals($date, $tasks[0]->getFirstExecution());
112
113
        $executions = $this->taskExecutionRepository->findAll();
114
        $this->assertCount(1, $executions);
115
116
        $this->assertEquals(TestHandler::class, $executions[0]->getHandlerClass());
117
        $this->assertEquals($date, $executions[0]->getScheduleTime());
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    protected function getCommand()
124
    {
125
        return self::$kernel->getContainer()->get('task.command.schedule_task');
126
    }
127
}
128