Completed
Pull Request — master (#36)
by Wachter
04:26
created

TaskExecutionRepositoryTest::testFindByTaskUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Task\TaskBundle\Tests\Functional\Command;
4
5
use Task\Execution\TaskExecutionInterface;
6
use Task\Storage\TaskExecutionRepositoryInterface;
7
use Task\Storage\TaskRepositoryInterface;
8
use Task\TaskBundle\Entity\Task;
9
use Task\TaskBundle\Entity\TaskExecution;
10
use Task\TaskBundle\Tests\Functional\BaseDatabaseTestCase;
11
use Task\TaskBundle\Tests\Functional\TestHandler;
12
use Task\TaskInterface;
13
use Task\TaskStatus;
14
15
/**
16
 * Tests for TaskExecutionRepository.
17
 */
18
class TaskExecutionRepositoryTest extends BaseDatabaseTestCase
19
{
20
    /**
21
     * @var TaskExecutionRepositoryInterface
22
     */
23
    protected $taskExecutionRepository;
24
25
    /**
26
     * @var TaskRepositoryInterface
27
     */
28
    protected $taskRepository;
29
30
    public function setUp()
31
    {
32
        parent::setUp();
33
34
        $this->taskExecutionRepository = self::$kernel->getContainer()->get('task.storage.task_execution');
35
        $this->taskRepository = self::$kernel->getContainer()->get('task.storage.task');
36
    }
37
38
    public function testSave(TaskInterface $task = null, \DateTime $scheduleTime = null, $status = TaskStatus::PLANNED)
39
    {
40
        if (!$scheduleTime) {
41
            $scheduleTime = new \DateTime();
42
        }
43
44
        if (!$task) {
45
            $task = $this->createTask();
46
            $this->taskRepository->save($task);
47
        }
48
49
        $execution = $this->createTaskExecution($task, $scheduleTime, $status);
50
        $this->taskExecutionRepository->save($execution);
51
52
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
53
54
        $this->assertEquals($execution->getUuid(), $result->getUuid());
55
        $this->assertEquals($scheduleTime, $result->getScheduleTime());
56
        $this->assertEquals($status, $result->getStatus());
57
        $this->assertEquals($task->getUuid(), $result->getTask()->getUuid());
58
59
        return $execution;
60
    }
61
62
    public function testRemove()
63
    {
64
        $execution = $this->testSave();
65
66
        $this->taskExecutionRepository->remove($execution);
67
68
        $this->assertNull($this->taskExecutionRepository->findByUuid($execution->getUuid()));
69
    }
70
71
    public function testFindAll()
72
    {
73
        $task = $this->createTask();
74
        $this->taskRepository->save($task);
75
76
        $executions = [];
77 View Code Duplication
        for ($i = 0; $i < 3; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
            $execution = $this->testSave($task);
79
            $executions[$execution->getUuid()] = $execution;
80
        }
81
82
        $result = $this->taskExecutionRepository->findAll();
83
84
        $this->assertCount(3, $result);
85
        foreach ($result as $item) {
86
            $this->assertArrayHasKey($item->getUuid(), $executions);
87
            unset($executions[$item->getUuid()]);
88
        }
89
    }
90
91
    public function testFindAllPaginated()
92
    {
93
        $task = $this->createTask();
94
        $this->taskRepository->save($task);
95
96
        $executions = [];
97 View Code Duplication
        for ($i = 0; $i < 3; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
            $execution = $this->testSave($task);
99
            $executions[$execution->getUuid()] = $execution;
100
        }
101
102
        $result = $this->taskExecutionRepository->findAll(1, 2);
103
104
        $this->assertCount(2, $result);
105
        foreach ($result as $item) {
106
            $this->assertArrayHasKey($item->getUuid(), $executions);
107
            unset($executions[$item->getUuid()]);
108
        }
109
110
        $result = $this->taskExecutionRepository->findAll(2, 2);
111
112
        $this->assertCount(1, $result);
113
        foreach ($result as $item) {
114
            $this->assertArrayHasKey($item->getUuid(), $executions);
115
            unset($executions[$item->getUuid()]);
116
        }
117
118
        $this->assertEmpty($executions);
119
    }
120
121
    public function testFindPending()
122
    {
123
        $execution = $this->testSave();
124
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
125
126
        $execution = $this->testSave(null, null, TaskStatus::RUNNING);
127
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
128
129
        $execution = $this->testSave(null, null, TaskStatus::COMPLETED);
130
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
131
132
        $execution = $this->testSave(null, null, TaskStatus::FAILED);
133
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
134
    }
135
136
    public function testFindByUuid()
137
    {
138
        $execution = $this->testSave();
139
140
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
141
        $this->assertEquals($execution->getUuid(), $result->getUuid());
142
    }
143
144 View Code Duplication
    public function testFindByTask()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        $execution = $this->testSave();
147
148
        $result = $this->taskExecutionRepository->findByTask($execution->getTask());
149
150
        $this->assertCount(1, $result);
151
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
152
    }
153
154 View Code Duplication
    public function testFindByTaskUuid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        $execution = $this->testSave();
157
158
        $result = $this->taskExecutionRepository->findByTaskUuid($execution->getTask()->getUuid());
159
160
        $this->assertCount(1, $result);
161
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
162
    }
163
164
    public function testFindScheduledPast()
165
    {
166
        $task = $this->createTask();
167
        $this->taskRepository->save($task);
168
169
        $execution = $this->testSave($task, new \DateTime('-1 hour'));
170
171
        $result = $this->taskExecutionRepository->findScheduled();
172
173
        $this->assertCount(1, $result);
174
        $this->assertEquals($execution->getUuid(), $result[0]->getUuid());
175
    }
176
177
    public function testFindScheduledFuture()
178
    {
179
        $task = $this->createTask();
180
        $this->taskRepository->save($task);
181
182
        $this->testSave($task, new \DateTime('+1 hour'));
183
184
        $this->assertEmpty($this->taskExecutionRepository->findScheduled());
185
    }
186
187
    /**
188
     * Create a new task.
189
     *
190
     * @param string $handlerClass
191
     *
192
     * @return TaskInterface
193
     */
194
    private function createTask($handlerClass = TestHandler::class)
195
    {
196
        return new Task($handlerClass);
197
    }
198
199
    /**
200
     * Create a new task-execution.
201
     *
202
     * @param TaskInterface $task
203
     * @param \DateTime $scheduleTime
204
     * @param string $status
205
     *
206
     * @return TaskExecutionInterface
207
     */
208
    private function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED)
209
    {
210
        $execution = new TaskExecution($task, $task->getHandlerClass(), $scheduleTime);
211
        $execution->setStatus($status);
212
213
        return $execution;
214
    }
215
}
216