Completed
Pull Request — master (#2)
by Wachter
05:57
created

DoctrineStorageTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 15.05 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 2 Features 3
Metric Value
wmc 5
c 4
b 2
f 3
lcom 1
cbo 5
dl 14
loc 93
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A storeDataProvider() 0 13 1
B testStore() 7 32 2
B testStoreTaskForKeyExists() 7 37 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
                function (TaskEntity $entity) use ($date, $completed, $uuid) {
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...
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
            $entityManager->persist(Argument::any())->shouldNotBeCalled();
86
            $entityManager->flush()->shouldNotBeCalled();
87
        } else {
88
            $entityManager->persist(
89
                Argument::that(
90 View Code Duplication
                    function (TaskEntity $entity) use ($date, $completed, $uuid) {
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...
91
                        $this->assertEquals($uuid, $entity->getUuid());
92
                        $this->assertEquals($completed, $entity->isCompleted());
93
                        $this->assertEquals($date, $entity->getExecutionDate());
94
95
                        return true;
96
                    }
97
                )
98
            )->shouldBeCalledTimes(1);
99
            $entityManager->flush()->shouldBeCalledTimes(1);
100
        }
101
102
        $storage->store($task->reveal());
103
    }
104
}
105