Completed
Push — master ( a5a753...454108 )
by Wachter
07:05
created

TaskExecutionRepositoryTest::createTaskExecution()   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 3
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\Tests\Functional\BaseDatabaseTestCase;
10
use Task\TaskInterface;
11
use Task\TaskStatus;
12
13
/**
14
 * Tests for TaskExecutionRepository.
15
 */
16
class TaskExecutionRepositoryTest extends BaseDatabaseTestCase
17
{
18
    /**
19
     * @var TaskExecutionRepositoryInterface
20
     */
21
    protected $taskExecutionRepository;
22
23
    /**
24
     * @var TaskRepositoryInterface
25
     */
26
    protected $taskRepository;
27
28
    public function setUp()
29
    {
30
        parent::setUp();
31
32
        $this->taskExecutionRepository = self::$kernel->getContainer()->get('task.storage.task_execution');
33
        $this->taskRepository = self::$kernel->getContainer()->get('task.storage.task');
34
    }
35
36
    public function testSave()
37
    {
38
        $execution = $this->save();
39
40
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
41
42
        $this->assertEquals($execution->getUuid(), $result->getUuid());
43
        $this->assertEquals($execution->getScheduleTime(), $result->getScheduleTime());
44
        $this->assertEquals($execution->getStatus(), $result->getStatus());
45
        $this->assertEquals($execution->getTask()->getUuid(), $result->getTask()->getUuid());
46
    }
47
48
    public function testRemove()
49
    {
50
        $execution = $this->save();
51
52
        $this->taskExecutionRepository->remove($execution);
53
54
        $this->assertNull($this->taskExecutionRepository->findByUuid($execution->getUuid()));
55
    }
56
57
    public function testFindAll()
58
    {
59
        $task = $this->createTask();
60
        $this->taskRepository->save($task);
61
62
        $executions = [];
63 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...
64
            $execution = $this->save($task);
65
            $executions[$execution->getUuid()] = $execution;
66
        }
67
68
        $result = $this->taskExecutionRepository->findAll();
69
70
        $this->assertCount(3, $result);
71
        foreach ($result as $item) {
72
            $this->assertArrayHasKey($item->getUuid(), $executions);
73
            unset($executions[$item->getUuid()]);
74
        }
75
    }
76
77
    public function testFindAllPaginated()
78
    {
79
        $task = $this->createTask();
80
        $this->taskRepository->save($task);
81
82
        $executions = [];
83 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...
84
            $execution = $this->save($task);
85
            $executions[$execution->getUuid()] = $execution;
86
        }
87
88
        $result = $this->taskExecutionRepository->findAll(1, 2);
89
90
        $this->assertCount(2, $result);
91
        foreach ($result as $item) {
92
            $this->assertArrayHasKey($item->getUuid(), $executions);
93
            unset($executions[$item->getUuid()]);
94
        }
95
96
        $result = $this->taskExecutionRepository->findAll(2, 2);
97
98
        $this->assertCount(1, $result);
99
        foreach ($result as $item) {
100
            $this->assertArrayHasKey($item->getUuid(), $executions);
101
            unset($executions[$item->getUuid()]);
102
        }
103
104
        $this->assertEmpty($executions);
105
    }
106
107
    public function testFindPending()
108
    {
109
        $execution = $this->save();
110
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
111
112
        $execution = $this->save(null, null, TaskStatus::RUNNING);
113
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
114
115
        $execution = $this->save(null, null, TaskStatus::COMPLETED);
116
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
117
118
        $execution = $this->save(null, null, TaskStatus::FAILED);
119
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
120
    }
121
122
    public function testFindByUuid()
123
    {
124
        $execution = $this->save();
125
126
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
127
        $this->assertEquals($execution->getUuid(), $result->getUuid());
128
    }
129
130 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...
131
    {
132
        $execution = $this->save();
133
134
        $result = $this->taskExecutionRepository->findByTask($execution->getTask());
135
136
        $this->assertCount(1, $result);
137
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
138
    }
139
140 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...
141
    {
142
        $execution = $this->save();
143
144
        $result = $this->taskExecutionRepository->findByTaskUuid($execution->getTask()->getUuid());
145
146
        $this->assertCount(1, $result);
147
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
148
    }
149
150
    public function testFindScheduledPast()
151
    {
152
        $task = $this->createTask();
153
        $this->taskRepository->save($task);
154
155
        $execution = $this->save($task, new \DateTime('-1 hour'));
156
157
        $result = $this->taskExecutionRepository->findScheduled();
158
159
        $this->assertCount(1, $result);
160
        $this->assertEquals($execution->getUuid(), $result[0]->getUuid());
161
    }
162
163
    public function testFindScheduledFuture()
164
    {
165
        $task = $this->createTask();
166
        $this->taskRepository->save($task);
167
168
        $this->save($task, new \DateTime('+1 hour'));
169
170
        $this->assertEmpty($this->taskExecutionRepository->findScheduled());
171
    }
172
173
    /**
174
     * Save a new execution to database.
175
     *
176
     * @param TaskInterface $task
177
     * @param \DateTime $scheduleTime
178
     * @param string $status
179
     *
180
     * @return TaskExecutionInterface
181
     */
182
    private function save(TaskInterface $task = null, \DateTime $scheduleTime = null, $status = TaskStatus::PLANNED)
183
    {
184
        if (!$scheduleTime) {
185
            $scheduleTime = new \DateTime();
186
        }
187
188
        if (!$task) {
189
            $task = $this->createTask();
190
            $this->taskRepository->save($task);
191
        }
192
193
        $execution = $this->createTaskExecution($task, $scheduleTime, $status);
194
        $this->taskExecutionRepository->save($execution);
195
196
        return $execution;
197
    }
198
}
199