Completed
Push — master ( b6fd36...b52328 )
by Daniel
05:33
created

TaskExecutionRepositoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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()
39
    {
40
        $execution = $this->save();
41
42
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
43
44
        $this->assertEquals($execution->getUuid(), $result->getUuid());
45
        $this->assertEquals($execution->getScheduleTime(), $result->getScheduleTime());
46
        $this->assertEquals($execution->getStatus(), $result->getStatus());
47
        $this->assertEquals($execution->getTask()->getUuid(), $result->getTask()->getUuid());
48
    }
49
50
    public function testRemove()
51
    {
52
        $execution = $this->save();
53
54
        $this->taskExecutionRepository->remove($execution);
55
56
        $this->assertNull($this->taskExecutionRepository->findByUuid($execution->getUuid()));
57
    }
58
59
    public function testFindAll()
60
    {
61
        $task = $this->createTask();
62
        $this->taskRepository->save($task);
63
64
        $executions = [];
65 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...
66
            $execution = $this->save($task);
67
            $executions[$execution->getUuid()] = $execution;
68
        }
69
70
        $result = $this->taskExecutionRepository->findAll();
71
72
        $this->assertCount(3, $result);
73
        foreach ($result as $item) {
74
            $this->assertArrayHasKey($item->getUuid(), $executions);
75
            unset($executions[$item->getUuid()]);
76
        }
77
    }
78
79
    public function testFindAllPaginated()
80
    {
81
        $task = $this->createTask();
82
        $this->taskRepository->save($task);
83
84
        $executions = [];
85 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...
86
            $execution = $this->save($task);
87
            $executions[$execution->getUuid()] = $execution;
88
        }
89
90
        $result = $this->taskExecutionRepository->findAll(1, 2);
91
92
        $this->assertCount(2, $result);
93
        foreach ($result as $item) {
94
            $this->assertArrayHasKey($item->getUuid(), $executions);
95
            unset($executions[$item->getUuid()]);
96
        }
97
98
        $result = $this->taskExecutionRepository->findAll(2, 2);
99
100
        $this->assertCount(1, $result);
101
        foreach ($result as $item) {
102
            $this->assertArrayHasKey($item->getUuid(), $executions);
103
            unset($executions[$item->getUuid()]);
104
        }
105
106
        $this->assertEmpty($executions);
107
    }
108
109
    public function testFindPending()
110
    {
111
        $execution = $this->save();
112
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
113
114
        $execution = $this->save(null, null, TaskStatus::RUNNING);
115
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
116
117
        $execution = $this->save(null, null, TaskStatus::COMPLETED);
118
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
119
120
        $execution = $this->save(null, null, TaskStatus::FAILED);
121
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
122
    }
123
124
    public function testFindByUuid()
125
    {
126
        $execution = $this->save();
127
128
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
129
        $this->assertEquals($execution->getUuid(), $result->getUuid());
130
    }
131
132 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...
133
    {
134
        $execution = $this->save();
135
136
        $result = $this->taskExecutionRepository->findByTask($execution->getTask());
137
138
        $this->assertCount(1, $result);
139
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
140
    }
141
142 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...
143
    {
144
        $execution = $this->save();
145
146
        $result = $this->taskExecutionRepository->findByTaskUuid($execution->getTask()->getUuid());
147
148
        $this->assertCount(1, $result);
149
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
150
    }
151
152
    public function testFindScheduledPast()
153
    {
154
        $task = $this->createTask();
155
        $this->taskRepository->save($task);
156
157
        $execution = $this->save($task, new \DateTime('-1 hour'));
158
159
        $result = $this->taskExecutionRepository->findScheduled();
160
161
        $this->assertCount(1, $result);
162
        $this->assertEquals($execution->getUuid(), $result[0]->getUuid());
163
    }
164
165
    public function testFindScheduledFuture()
166
    {
167
        $task = $this->createTask();
168
        $this->taskRepository->save($task);
169
170
        $this->save($task, new \DateTime('+1 hour'));
171
172
        $this->assertEmpty($this->taskExecutionRepository->findScheduled());
173
    }
174
175
    /**
176
     * Save a new execution to database.
177
     *
178
     * @param TaskInterface $task
179
     * @param \DateTime $scheduleTime
180
     * @param string $status
181
     *
182
     * @return TaskExecutionInterface
183
     */
184
    private function save(TaskInterface $task = null, \DateTime $scheduleTime = null, $status = TaskStatus::PLANNED)
185
    {
186
        if (!$scheduleTime) {
187
            $scheduleTime = new \DateTime();
188
        }
189
190
        if (!$task) {
191
            $task = $this->createTask();
192
            $this->taskRepository->save($task);
193
        }
194
195
        $execution = $this->createTaskExecution($task, $scheduleTime, $status);
196
        $this->taskExecutionRepository->save($execution);
197
198
        return $execution;
199
    }
200
201
    /**
202
     * Create a new task.
203
     *
204
     * @param string $handlerClass
205
     *
206
     * @return TaskInterface
207
     */
208
    private function createTask($handlerClass = TestHandler::class)
209
    {
210
        return new Task($handlerClass);
211
    }
212
213
    /**
214
     * Create a new task-execution.
215
     *
216
     * @param TaskInterface $task
217
     * @param \DateTime $scheduleTime
218
     * @param string $status
219
     *
220
     * @return TaskExecutionInterface
221
     */
222
    private function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED)
223
    {
224
        $execution = new TaskExecution($task, $task->getHandlerClass(), $scheduleTime);
225
        $execution->setStatus($status);
226
227
        return $execution;
228
    }
229
}
230