Completed
Push — master ( 454108...fbd2bc )
by Alexander
09:20 queued 03:33
created

testFindAllPaginated()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 29
Code Lines 18

Duplication

Lines 4
Ratio 13.79 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 4
loc 29
rs 8.5806
cc 4
eloc 18
nc 8
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\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->findNextScheduled();
158
        $this->assertEquals($execution->getUuid(), $result->getUuid());
159
    }
160
161 View Code Duplication
    public function testFindScheduledFuture()
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...
162
    {
163
        $task = $this->createTask();
164
        $this->taskRepository->save($task);
165
166
        $this->save($task, new \DateTime('+1 hour'));
167
168
        $this->assertNull($this->taskExecutionRepository->findNextScheduled());
169
    }
170
171 View Code Duplication
    public function testFindScheduledSkipped()
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...
172
    {
173
        $task = $this->createTask();
174
        $this->taskRepository->save($task);
175
176
        $this->save($task, new \DateTime('+1 hour'));
177
178
        $this->assertNull($this->taskExecutionRepository->findNextScheduled());
179
    }
180
181
    /**
182
     * Save a new execution to database.
183
     *
184
     * @param TaskInterface $task
185
     * @param \DateTime $scheduleTime
186
     * @param string $status
187
     *
188
     * @return TaskExecutionInterface
189
     */
190
    private function save(TaskInterface $task = null, \DateTime $scheduleTime = null, $status = TaskStatus::PLANNED)
191
    {
192
        if (!$scheduleTime) {
193
            $scheduleTime = new \DateTime();
194
        }
195
196
        if (!$task) {
197
            $task = $this->createTask();
198
            $this->taskRepository->save($task);
199
        }
200
201
        $execution = $this->createTaskExecution($task, $scheduleTime, $status);
202
        $this->taskExecutionRepository->save($execution);
203
204
        return $execution;
205
    }
206
}
207