Completed
Pull Request — master (#14)
by Pavel
28:57
created

CommitTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 103
Duplicated Lines 11.65 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 12
loc 103
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testSimpleCommit() 12 26 1
B testChainCommitWithRelation() 0 24 1
B testChainUpdateWithRelation() 0 25 1
A testRemove() 0 17 1
A getClientNames() 0 4 1

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 Bankiru\Api\Doctrine\Tests;
4
5
use Bankiru\Api\Doctrine\Test\Entity\TestEntity;
6
use Bankiru\Api\Doctrine\Test\Entity\TestReference;
7
use ScayTrase\Api\Rpc\RpcRequestInterface;
8
9
class CommitTest extends AbstractEntityManagerTest
10
{
11
    public function testSimpleCommit()
12
    {
13
        $entity = new TestReference();
14
15
        $this->getClient('test-reference-client')->push(
16
            $this->getResponseMock(true, 241),
17 View Code Duplication
            function (RpcRequestInterface $request) {
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...
18
                self::assertEquals('test-reference/create', $request->getMethod());
19
                self::assertEquals(
20
                    [
21
                        'reference-payload' => '',
22
                        'owner'             => null,
23
                    ],
24
                    $request->getParameters()
25
                );
26
27
                return true;
28
            }
29
        );
30
31
        $this->getManager()->persist($entity);
32
        $this->getManager()->flush();
33
34
        self::assertNotNull($entity->getId());
35
        self::assertEquals(241, $entity->getId());
36
    }
37
38
    public function testChainCommitWithRelation()
39
    {
40
        $entity = new TestReference();
41
        $parent = new TestEntity();
42
        $entity->setOwner($parent);
43
44
        $this->getClient()->push($this->getResponseMock(true, 42));
45
46
        $this->getClient('test-reference-client')->push($this->getResponseMock(true, 241));
47
48
        $this->getManager()->persist($entity);
49
        $this->getManager()->persist($parent);
50
        $this->getManager()->flush();
51
52
        self::assertNotNull($parent->getId());
53
        self::assertEquals(42, $parent->getId());
54
55
        self::assertNotNull($entity->getId());
56
        self::assertEquals(241, $entity->getId());
57
58
        self::assertEquals($entity->getOwner(), $parent);
59
60
        return $entity;
61
    }
62
63
    public function testChainUpdateWithRelation()
64
    {
65
        $entity = $this->testChainCommitWithRelation();
66
67
        $oldParent = $entity->getOwner();
68
        $newParent = new TestEntity();
69
        $this->getClient()->push($this->getResponseMock(true, 17));
70
        $this->getClient('test-reference-client')->push($this->getResponseMock(true, null));
71
72
        $entity->setOwner($newParent);
73
74
        $this->getManager()->persist($newParent);
75
        $this->getManager()->flush();
76
77
        self::assertNotNull($newParent->getId());
78
        self::assertEquals(17, $newParent->getId());
79
80
        self::assertNotNull($entity->getId());
81
        self::assertEquals(241, $entity->getId());
82
83
        self::assertEquals($entity->getOwner(), $newParent);
84
85
        self::assertNotSame($newParent, $oldParent);
86
        self::assertFalse($oldParent->getReferences()->contains($entity));
87
    }
88
89
    public function testRemove()
90
    {
91
        $entity = new TestReference();
92
93
        $this->getClient('test-reference-client')->push($this->getResponseMock(true, 241));
94
95
        $this->getManager()->persist($entity);
96
        $this->getManager()->flush();
97
98
        self::assertNotNull($entity->getId());
99
        self::assertEquals(241, $entity->getId());
100
101
        $this->getClient('test-reference-client')->push($this->getResponseMock(true, null));
102
103
        $this->getManager()->remove($entity);
104
        $this->getManager()->flush();
105
    }
106
107
    protected function getClientNames()
108
    {
109
        return array_merge(parent::getClientNames(), ['test-reference-client']);
110
    }
111
}
112