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

TaskExecutionRepositoryTest::testFindByTask()   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 Doctrine\Common\DataFixtures\Executor\ORMExecutor;
6
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
7
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
8
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
9
use Task\Storage\TaskExecutionRepositoryInterface;
10
use Task\TaskBundle\Entity\Task;
11
use Task\TaskBundle\Entity\TaskExecution;
12
use Task\TaskBundle\Tests\Functional\TestHandler;
13
use Task\TaskStatus;
14
15
/**
16
 * Tests for TaskExecutionRepository.
17
 */
18
class TaskExecutionRepositoryTest extends KernelTestCase
19
{
20
    /**
21
     * @var TaskExecutionRepositoryInterface
22
     */
23
    protected $taskExecutionRepository;
24
25
    /**
26
     * @var TaskExecutionRepositoryInterface
27
     */
28
    protected $taskRepository;
29
30
    public function setUp()
31
    {
32
        self::bootKernel();
33
34
        $this->purgeDatabase();
35
36
        $this->taskExecutionRepository = self::$kernel->getContainer()->get('task.storage.task_execution');
37
        $this->taskRepository = self::$kernel->getContainer()->get('task.storage.task');
38
    }
39
40
    public function testSave(Task $task = null, \DateTime $scheduleTime = null, $status = TaskStatus::PLANNED)
41
    {
42
        if (!$scheduleTime) {
43
            $scheduleTime = new \DateTime();
44
        }
45
46
        if (!$task) {
47
            $task = $this->createTask();
48
            $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...
49
        }
50
51
        $execution = $this->createTaskExecution($task, $scheduleTime, $status);
52
        $this->taskExecutionRepository->save($execution);
53
54
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
55
56
        $this->assertEquals($execution->getUuid(), $result->getUuid());
57
        $this->assertEquals($scheduleTime, $result->getScheduleTime());
58
        $this->assertEquals($status, $result->getStatus());
59
        $this->assertEquals($task->getUuid(), $result->getTask()->getUuid());
60
61
        return $execution;
62
    }
63
64
    public function testRemove()
65
    {
66
        $execution = $this->testSave();
67
68
        $this->taskExecutionRepository->remove($execution);
69
70
        $this->assertNull($this->taskExecutionRepository->findByUuid($execution->getUuid()));
71
    }
72
73
    public function testFindAll()
74
    {
75
        $task = $this->createTask();
76
        $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...
77
78
        $executions = [];
79 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...
80
            $execution = $this->testSave($task);
81
            $executions[$execution->getUuid()] = $execution;
82
        }
83
84
        $result = $this->taskExecutionRepository->findAll();
85
86
        $this->assertCount(3, $result);
87
        foreach ($result as $item) {
88
            $this->assertArrayHasKey($item->getUuid(), $executions);
89
            unset($executions[$item->getUuid()]);
90
        }
91
    }
92
93
    public function testFindAllPaginated()
94
    {
95
        $task = $this->createTask();
96
        $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...
97
98
        $executions = [];
99 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...
100
            $execution = $this->testSave($task);
101
            $executions[$execution->getUuid()] = $execution;
102
        }
103
104
        $result = $this->taskExecutionRepository->findAll(1, 2);
105
106
        $this->assertCount(2, $result);
107
        foreach ($result as $item) {
108
            $this->assertArrayHasKey($item->getUuid(), $executions);
109
            unset($executions[$item->getUuid()]);
110
        }
111
112
        $result = $this->taskExecutionRepository->findAll(2, 2);
113
114
        $this->assertCount(1, $result);
115
        foreach ($result as $item) {
116
            $this->assertArrayHasKey($item->getUuid(), $executions);
117
            unset($executions[$item->getUuid()]);
118
        }
119
120
        $this->assertEmpty($executions);
121
    }
122
123
    public function testFindPending()
124
    {
125
        $execution = $this->testSave();
126
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
127
128
        $execution = $this->testSave(null, null, TaskStatus::RUNNING);
129
        $this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask()));
130
131
        $execution = $this->testSave(null, null, TaskStatus::COMPLETED);
132
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
133
134
        $execution = $this->testSave(null, null, TaskStatus::FAILED);
135
        $this->assertNull($this->taskExecutionRepository->findPending($execution->getTask()));
136
    }
137
138
    public function testFindByUuid()
139
    {
140
        $execution = $this->testSave();
141
142
        $result = $this->taskExecutionRepository->findByUuid($execution->getUuid());
143
        $this->assertEquals($execution->getUuid(), $result->getUuid());
144
    }
145
146 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...
147
    {
148
        $execution = $this->testSave();
149
150
        $result = $this->taskExecutionRepository->findByTask($execution->getTask());
151
152
        $this->assertCount(1, $result);
153
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
154
    }
155
156 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...
157
    {
158
        $execution = $this->testSave();
159
160
        $result = $this->taskExecutionRepository->findByTaskUuid($execution->getTask()->getUuid());
161
162
        $this->assertCount(1, $result);
163
        $this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid());
164
    }
165
166
    public function testFindScheduledPast()
167
    {
168
        $task = $this->createTask();
169
        $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...
170
171
        $execution = $this->testSave($task, new \DateTime('-1 hour'));
172
173
        $result = $this->taskExecutionRepository->findScheduled();
174
175
        $this->assertCount(1, $result);
176
        $this->assertEquals($execution->getUuid(), $result[0]->getUuid());
177
    }
178
179
    public function testFindScheduledFuture()
180
    {
181
        $task = $this->createTask();
182
        $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...
183
184
        $this->testSave($task, new \DateTime('+1 hour'));
185
186
        $this->assertEmpty($this->taskExecutionRepository->findScheduled());
187
    }
188
189
    private function createTask($handlerClass = TestHandler::class)
190
    {
191
        return new Task($handlerClass);
192
    }
193
194
    private function createTaskExecution(Task $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED)
195
    {
196
        $execution = new TaskExecution($task, $task->getHandlerClass(), $scheduleTime);
197
        $execution->setStatus($status);
198
199
        return $execution;
200
    }
201
202 View Code Duplication
    protected function purgeDatabase()
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...
203
    {
204
        $manager = self::$kernel->getContainer()->get('doctrine.orm.entity_manager');
205
        $connection = $manager->getConnection();
206
207
        if ($connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOMySql\Driver) {
208
            $connection->executeUpdate('SET foreign_key_checks = 0;');
209
        }
210
211
        $purger = new ORMPurger();
212
        $executor = new ORMExecutor($manager, $purger);
213
        $referenceRepository = new ProxyReferenceRepository($manager);
214
        $executor->setReferenceRepository($referenceRepository);
215
        $executor->purge();
216
217
        if ($connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOMySql\Driver) {
218
            $connection->executeUpdate('SET foreign_key_checks = 1;');
219
        }
220
    }
221
}
222