Completed
Pull Request — master (#2)
by Wachter
05:49 queued 01:29
created

DoctrineStorageTest::testStoreTaskForKeyExists()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 8.9714
cc 2
eloc 15
nc 2
nop 4
1
<?php
2
3
namespace Unit\Storage;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Prophecy\Argument;
7
use Task\TaskBundle\Entity\Task as TaskEntity;
8
use Task\TaskBundle\Entity\TaskRepository;
9
use Task\TaskBundle\Storage\DoctrineStorage;
10
use Task\TaskInterface;
11
12
class DoctrineStorageTest extends \PHPUnit_Framework_TestCase
13
{
14
    public function storeDataProvider()
15
    {
16
        return [
17
            [new \DateTime(), true, '123-123-123'],
18
            [new \DateTime(), true, '123-123-123', 'test-key'],
19
            [new \DateTime(), true, '321-321-321'],
20
            [new \DateTime(), true, '321-321-321', 'test-key'],
21
            [new \DateTime('1 day ago'), true, '321-321-321'],
22
            [new \DateTime('1 day ago'), true, '321-321-321', 'test-key'],
23
            [new \DateTime(), false, '123-123-123'],
24
            [new \DateTime(), false, '123-123-123', 'test-key'],
25
        ];
26
    }
27
28
    /**
29
     * @dataProvider storeDataProvider
30
     */
31
    public function testStore($date, $completed, $uuid, $key = null)
32
    {
33
        $entityManager = $this->prophesize(EntityManagerInterface::class);
34
        $repository = $this->prophesize(TaskRepository::class);
35
36
        $storage = new DoctrineStorage($entityManager->reveal(), $repository->reveal());
37
38
        $task = $this->prophesize(TaskInterface::class);
39
        $task->getUuid()->willReturn($uuid);
40
        $task->getKey()->willReturn($key);
41
        $task->isCompleted()->willReturn($completed);
42
        $task->getExecutionDate()->willReturn($date);
43
44
        if ($key) {
45
            $repository->findOneBy(['key' => $key, 'completed' => false])->willReturn(null)->shouldBeCalledTimes(1);
46
        }
47
48
        $storage->store($task->reveal());
49
50
        $entityManager->persist(
51
            Argument::that(
52
                function (TaskEntity $entity) use ($date, $completed, $uuid) {
53
                    $this->assertEquals($uuid, $entity->getUuid());
54
                    $this->assertEquals($completed, $entity->isCompleted());
55
                    $this->assertEquals($date, $entity->getExecutionDate());
56
57
                    return true;
58
                }
59
            )
60
        )->shouldBeCalledTimes(1);
61
        $entityManager->flush()->shouldBeCalledTimes(1);
62
    }
63
64
    /**
65
     * @dataProvider storeDataProvider
66
     */
67
    public function testStoreTaskForKeyExists($date, $completed, $uuid, $key = null)
68
    {
69
        $entityManager = $this->prophesize(EntityManagerInterface::class);
70
        $repository = $this->prophesize(TaskRepository::class);
71
72
        $storage = new DoctrineStorage($entityManager->reveal(), $repository->reveal());
73
74
        $task = $this->prophesize(TaskInterface::class);
75
        $task->getUuid()->willReturn($uuid);
76
        $task->getKey()->willReturn($key);
77
        $task->isCompleted()->willReturn($completed);
78
        $task->getExecutionDate()->willReturn($date);
79
80
        if ($key) {
81
            $oldTask = $this->prophesize(TaskInterface::class);
82
83
            $repository->findOneBy(['key' => $key, 'completed' => false])->willReturn($oldTask)->shouldBeCalledTimes(1);
84
        }
85
86
        $storage->store($task->reveal());
87
88
        $entityManager->persist(Argument::any())->shouldNotBeCalled();
89
        $entityManager->flush()->shouldNotBeCalled();
90
    }
91
}
92