Completed
Pull Request — master (#20)
by Wachter
02:52
created

RunCommandTest::testExecuteWithFail()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 29
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 Task\TaskBundle\Tests\Functional\Command;
13
14
use Cron\CronExpression;
15
use Task\Execution\TaskExecutionInterface;
16
use Task\TaskBundle\Tests\Functional\BaseCommandTestCase;
17
use Task\TaskBundle\Tests\Functional\FailTestHandler;
18
use Task\TaskBundle\Tests\Functional\TestHandler;
19
use Task\TaskStatus;
20
21
/**
22
 * Tests for RunCommand.
23
 */
24
class RunCommandTest extends BaseCommandTestCase
25
{
26
    public function testExecute()
27
    {
28
        $singleTask = $this->createTask('Test workload 1');
29
        $laterTask = $this->createTask('Test workload 2');
30
        $intervalTask = $this->createTask('Test workload 3', CronExpression::factory('@daily'));
31
32
        /** @var TaskExecutionInterface[] $executions */
33
        $executions = [
34
            $this->createTaskExecution($singleTask, new \DateTime('-1 hour')),
35
            $this->createTaskExecution($laterTask, new \DateTime('+1 hour')),
36
            $this->createTaskExecution($intervalTask, new \DateTime('-2 hour')),
37
        ];
38
39
        $this->commandTester->execute(
40
            [
41
                'command' => $this->command->getName(),
42
            ]
43
        );
44
45
        $this->assertEquals(TaskStatus::COMPLETE, $executions[0]->getStatus());
46
        $this->assertEquals(strrev('Test workload 1'), $executions[0]->getResult());
47
        $this->assertGreaterThan(0, $executions[0]->getDuration());
48
        $this->assertGreaterThanOrEqual($executions[0]->getStartTime(), $executions[0]->getEndTime());
49
50
        $this->assertEquals(TaskStatus::PLANNED, $executions[1]->getStatus());
51
        $this->assertNull($executions[1]->getResult());
52
        $this->assertNull($executions[1]->getDuration());
53
        $this->assertNull($executions[1]->getStartTime());
54
        $this->assertNull($executions[1]->getEndTime());
55
56
        $this->assertEquals(TaskStatus::COMPLETE, $executions[2]->getStatus());
57
        $this->assertEquals(strrev('Test workload 3'), $executions[2]->getResult());
58
        $this->assertGreaterThan(0, $executions[2]->getDuration());
59
        $this->assertGreaterThanOrEqual($executions[2]->getStartTime(), $executions[2]->getEndTime());
60
61
        $result = $this->taskExecutionRepository->findAll(2, 3);
62
        $this->assertCount(1, $result);
63
64
        $this->assertEquals($intervalTask, $result[0]->getTask());
65
        $this->assertEquals(TaskStatus::PLANNED, $result[0]->getStatus());
66
        $this->assertEquals(TestHandler::class, $result[0]->getHandlerClass());
67
        $this->assertEquals('Test workload 3', $result[0]->getWorkload());
68
    }
69
70
    public function testExecuteWithFail()
71
    {
72
        $singleTask = $this->createTask('Test workload 1', null, FailTestHandler::class);
73
        $laterTask = $this->createTask('Test workload 2', null, FailTestHandler::class);
74
        $intervalTask = $this->createTask('Test workload 3', CronExpression::factory('@daily'), FailTestHandler::class);
75
76
        /** @var TaskExecutionInterface[] $executions */
77
        $executions = [
78
            $this->createTaskExecution($singleTask, new \DateTime('-1 hour')),
79
            $this->createTaskExecution($laterTask, new \DateTime('+1 hour')),
80
            $this->createTaskExecution($intervalTask, new \DateTime('-2 hour')),
81
        ];
82
83
        $this->commandTester->execute(
84
            [
85
                'command' => $this->command->getName(),
86
            ]
87
        );
88
89
        $this->assertEquals(TaskStatus::FAILED, $executions[0]->getStatus());
90
        $this->assertNull($executions[0]->getResult());
91
        $this->assertGreaterThan(0, $executions[0]->getDuration());
92
        $this->assertGreaterThanOrEqual($executions[0]->getStartTime(), $executions[0]->getEndTime());
93
94
        $this->assertEquals(TaskStatus::PLANNED, $executions[1]->getStatus());
95
        $this->assertNull($executions[1]->getResult());
96
        $this->assertNull($executions[1]->getDuration());
97
        $this->assertNull($executions[1]->getStartTime());
98
        $this->assertNull($executions[1]->getEndTime());
99
100
        $this->assertEquals(TaskStatus::FAILED, $executions[2]->getStatus());
101
        $this->assertNull($executions[2]->getResult());
102
        $this->assertGreaterThan(0, $executions[2]->getDuration());
103
        $this->assertGreaterThanOrEqual($executions[2]->getStartTime(), $executions[2]->getEndTime());
104
105
        $result = $this->taskExecutionRepository->findAll(2, 3);
106
        $this->assertCount(1, $result);
107
108
        $this->assertEquals($intervalTask, $result[0]->getTask());
109
        $this->assertEquals(TaskStatus::PLANNED, $result[0]->getStatus());
110
        $this->assertEquals(FailTestHandler::class, $result[0]->getHandlerClass());
111
        $this->assertEquals('Test workload 3', $result[0]->getWorkload());
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    protected function getCommand()
118
    {
119
        return self::$kernel->getContainer()->get('task.command.run');
120
    }
121
}
122