Completed
Pull Request — master (#36)
by Wachter
27:38 queued 25:01
created

TaskExecutionRepositoryTest::testFindAll()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 4
Ratio 21.05 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 4
loc 19
rs 9.4285
cc 3
eloc 12
nc 4
nop 0
1
<?php
2
3
namespace Task\TaskBundle\Tests\Functional\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6
use Task\Storage\TaskExecutionRepositoryInterface;
7
use Task\TaskBundle\Entity\Task;
8
use Task\TaskBundle\Entity\TaskExecution;
9
use Task\TaskBundle\Tests\Functional\TestHandler;
10
use Task\TaskStatus;
11
12
/**
13
 * Tests for TaskExecutionRepository.
14
 */
15
class TaskExecutionRepositoryTest extends KernelTestCase
16
{
17
    /**
18
     * @var TaskExecutionRepositoryInterface
19
     */
20
    protected $taskExecutionRepository;
21
22
    /**
23
     * @var TaskExecutionRepositoryInterface
24
     */
25
    protected $taskRepository;
26
27
    public function setUp()
28
    {
29
        self::bootKernel();
30
31
        $this->taskExecutionRepository = self::$kernel->getContainer()->get('task.storage.task_execution');
32
        $this->taskRepository = self::$kernel->getContainer()->get('task.storage.task');
33
    }
34
35
    public function testSave(Task $task = null, \DateTime $scheduleTime = null, $status = TaskStatus::PLANNED)
36
    {
37
        if (!$scheduleTime) {
38
            $scheduleTime = new \DateTime();
39
        }
40
41
        if (!$task) {
42
            $task = $this->createTask();
43
            $this->taskRepository->save($task);
0 ignored issues
show
Documentation introduced by
$task is of type object<Task\TaskBundle\Entity\Task>, but the function expects a object<Task\Execution\TaskExecutionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        }
45
46
        $execution = $this->createTaskExecution($task, $scheduleTime, $status);
47
        $this->taskExecutionRepository->save($execution);
48
49
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
50
51
        $this->assertEquals($execution->getUuid(), $result->getUuid());
52
        $this->assertEquals($scheduleTime, $result->getScheduleTime());
53
        $this->assertEquals($status, $result->getStatus());
54
        $this->assertEquals($task->getUuid(), $result->getTask()->getUuid());
55
56
        return $execution;
57
    }
58
59
    public function testRemove()
60
    {
61
        $execution = $this->testSave();
62
63
        $this->taskExecutionRepository->remove($execution);
64
65
        $this->assertNull($this->taskExecutionRepository->findByUuid($execution->getUuid()));
66
    }
67
68
    public function testFindAll()
69
    {
70
        $task = $this->createTask();
71
        $this->taskRepository->save($task);
0 ignored issues
show
Documentation introduced by
$task is of type object<Task\TaskBundle\Entity\Task>, but the function expects a object<Task\Execution\TaskExecutionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
73
        $executions = [];
74 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...
75
            $execution = $this->testSave($task);
76
            $executions[$execution->getUuid()] = $execution;
77
        }
78
79
        $result = $this->taskExecutionRepository->findAll();
80
81
        $this->assertCount(3, $result);
82
        foreach ($result as $item) {
83
            $this->assertArrayHasKey($item->getUuid(), $executions);
84
            unset($executions[$item->getUuid()]);
85
        }
86
    }
87
88
    public function testFindAllPaginated()
89
    {
90
        $task = $this->createTask();
91
        $this->taskRepository->save($task);
0 ignored issues
show
Documentation introduced by
$task is of type object<Task\TaskBundle\Entity\Task>, but the function expects a object<Task\Execution\TaskExecutionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
93
        $executions = [];
94 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...
95
            $execution = $this->testSave($task);
96
            $executions[$execution->getUuid()] = $execution;
97
        }
98
99
        $result = $this->taskExecutionRepository->findAll(1, 2);
100
101
        $this->assertCount(2, $result);
102
        foreach ($result as $item) {
103
            $this->assertArrayHasKey($item->getUuid(), $executions);
104
            unset($executions[$item->getUuid()]);
105
        }
106
107
        $result = $this->taskExecutionRepository->findAll(2, 2);
108
109
        $this->assertCount(1, $result);
110
        foreach ($result as $item) {
111
            $this->assertArrayHasKey($item->getUuid(), $executions);
112
            unset($executions[$item->getUuid()]);
113
        }
114
115
        $this->assertEmpty($executions);
116
    }
117
118
    public function testFindPending()
119
    {
120
        $execution = $this->testSave();
121
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
122
123
        $execution = $this->testSave(null, null, TaskStatus::RUNNING);
124
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
125
126
        $execution = $this->testSave(null, null, TaskStatus::COMPLETED);
127
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
128
129
        $execution = $this->testSave(null, null, TaskStatus::FAILED);
130
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
131
    }
132
133
    public function testFindByUuid()
134
    {
135
        $execution = $this->testSave();
136
137
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
138
        $this->assertEquals($execution->getUuid(), $result->getUuid());
139
    }
140
141 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...
142
    {
143
        $execution = $this->testSave();
144
145
        $result = $this->taskExecutionRepository->findByTask($execution->getTask());
146
147
        $this->assertCount(1, $result);
148
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
149
    }
150
151 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...
152
    {
153
        $execution = $this->testSave();
154
155
        $result = $this->taskExecutionRepository->findByTaskUuid($execution->getTask()->getUuid());
156
157
        $this->assertCount(1, $result);
158
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
159
    }
160
161
    public function testFindScheduledPast()
162
    {
163
        $task = $this->createTask();
164
        $this->taskRepository->save($task);
0 ignored issues
show
Documentation introduced by
$task is of type object<Task\TaskBundle\Entity\Task>, but the function expects a object<Task\Execution\TaskExecutionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
166
        $execution = $this->testSave($task, new \DateTime(('-1 minute')));
167
168
        $result = $this->taskExecutionRepository->findScheduled();
169
170
        $this->assertCount(1, $result);
171
        $this->assertEquals($execution->getUuid(), $result[0]->getUuid());
172
    }
173
174
    public function testFindScheduledFuture()
175
    {
176
        $task = $this->createTask();
177
        $this->taskRepository->save($task);
0 ignored issues
show
Documentation introduced by
$task is of type object<Task\TaskBundle\Entity\Task>, but the function expects a object<Task\Execution\TaskExecutionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
179
        $this->testSave($task, new \DateTime(('+1 minute')));
180
181
        $this->assertEmpty($this->taskExecutionRepository->findScheduled());
182
    }
183
184
    private function createTask($handlerClass = TestHandler::class)
185
    {
186
        return new Task($handlerClass);
187
    }
188
189
    private function createTaskExecution(Task $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED)
190
    {
191
        $execution = new TaskExecution($task, $task->getHandlerClass(), $scheduleTime);
192
        $execution->setStatus($status);
193
194
        return $execution;
195
    }
196
}
197