PersisterTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 196
Duplicated Lines 24.49 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 4
dl 48
loc 196
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testPersister() 0 14 1
A getProvider() 0 22 1
A setUp() 0 15 1
B setUpOneToOneRelation() 24 24 1
B setUpOneToManyRelation() 0 27 1
B setUpManyToOneRelation() 24 24 1
B setUpManyToManyRelation() 0 32 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
namespace Ajir\RabbitMqSqlBundle\Tests\Persister;
3
4
use Ajir\RabbitMqSqlBundle\Factory\EntityFactory;
5
use Ajir\RabbitMqSqlBundle\Factory\RelationFactory;
6
use Ajir\RabbitMqSqlBundle\Persister\Persister;
7
use Ajir\RabbitMqSqlBundle\Provider\ProviderInterface;
8
use PHPUnit_Framework_TestCase;
9
10
/**
11
 * Class DataMapperTest
12
 *
13
 * @author Florian Ajir <[email protected]>
14
 */
15
class PersisterTest extends PHPUnit_Framework_TestCase
16
{
17
18
    /**
19
     * The simple mapping fixture
20
     *
21
     * @var array
22
     */
23
    protected $data;
24
25
    /**
26
     *
27
     */
28
    public function testPersister()
29
    {
30
        $provider = $this->getProvider();
31
        $entityFactory = new EntityFactory('Ajir\RabbitMqSqlBundle\Model\Entity');
32
        $relationFactory = new RelationFactory(
33
            'Ajir\RabbitMqSqlBundle\Model\OneToOneRelation',
34
            'Ajir\RabbitMqSqlBundle\Model\OneToManyRelation',
35
            'Ajir\RabbitMqSqlBundle\Model\ManyToOneRelation',
36
            'Ajir\RabbitMqSqlBundle\Model\ManyToManyRelation'
37
        );
38
39
        $persister = new Persister($provider, $entityFactory, $relationFactory);
0 ignored issues
show
Bug introduced by
It seems like $provider defined by $this->getProvider() on line 30 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Ajir\RabbitMqSqlBundle\P...ersister::__construct() does only seem to accept object<Ajir\RabbitMqSqlB...ider\ProviderInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40
        $persister->persist($this->data);
41
    }
42
43
    /**
44
     * @return \PHPUnit_Framework_MockObject_MockObject|ProviderInterface
45
     */
46
    private function getProvider()
47
    {
48
        $provider = $this->getMock('Ajir\RabbitMqSqlBundle\Provider\ProviderInterface');
49
        $provider
50
            ->expects($this->any())
51
            ->method('getColumnValueWhere')
52
            ->will($this->returnValue('1'));
53
        $provider
54
            ->expects($this->any())
55
            ->method('insert')
56
            ->will($this->returnValue('1'));
57
        $provider
58
            ->expects($this->any())
59
            ->method('insertOrUpdateIfExists')
60
            ->will($this->returnValue('1'));
61
        $provider
62
            ->expects($this->atLeastOnce())
63
            ->method('delete')
64
            ->will($this->returnValue('1'));
65
66
        return $provider;
67
    }
68
69
    /**
70
     * Sets up the fixture
71
     */
72
    protected function setUp()
73
    {
74
        $this->data = array(
75
            'user' => array(
76
                'id' => '1',
77
                'sku' => 'sku_user',
78
                'name' => 'toto',
79
                '_table' => 'user'
80
            )
81
        );
82
        $this->setUpOneToOneRelation();
83
        $this->setUpOneToManyRelation();
84
        $this->setUpManyToOneRelation();
85
        $this->setUpManyToManyRelation();
86
    }
87
88
    /**
89
     * Set up oneToOne relation fixtures
90
     */
91 View Code Duplication
    private function setUpOneToOneRelation()
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...
92
    {
93
        $this->data['user']['_related']['oneToOne'] = array(
94
            'Customer' => array(
95
                '_relation' => array(
96
                    'targetEntity' => 'Customer',
97
                    'joinColumn' => array(
98
                        'name' => 'customer_id',
99
                        'referencedColumnName' => 'id',
100
                    ),
101
                    'table' => 'customer',
102
                ),
103
                '_data' => array(
104
                    'Customer' => array(
105
                        '_table' => 'customer',
106
                        '_identifier' => 'sku',
107
                        'sku' => '1',
108
                        'email' => '[email protected]',
109
                        'last_purchased' => '2015-06-26T22:22:00+0200',
110
                    ),
111
                ),
112
            )
113
        );
114
    }
115
116
    /**
117
     * Set up oneToMany relation fixtures
118
     */
119
    private function setUpOneToManyRelation()
120
    {
121
        $this->data['user']['_related']['oneToMany'] = array(
122
            'User' => array(
123
                '_relation' => array(
124
                    'targetEntity' => 'User',
125
                    'joinColumn' => array(
126
                        'name' => 'parent_id',
127
                        'referencedColumnName' => 'id'
128
                    ),
129
                    'table' => 'user'
130
                ),
131
                '_data' => array(
132
                    0 => array(
133
                        'User' => array(
134
                            '_table' => 'user',
135
                            '_identifier' => 'sku',
136
                            'id' => '2',
137
                            'sku' => 'azertyuiopqsdfgh',
138
                            'name' => 'riri',
139
                            'created_at' => '2015-06-01T22:22:00+0200'
140
                        )
141
                    )
142
                )
143
            )
144
        );
145
    }
146
147
    /**
148
     * Set up manyToOne relation fixtures
149
     */
150 View Code Duplication
    private function setUpManyToOneRelation()
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...
151
    {
152
        $this->data['user']['_related']['manyToOne'] = array(
153
            'Address' => array(
154
                '_relation' => array(
155
                    'targetEntity' => 'Address',
156
                    'joinColumn' => array(
157
                        'name' => 'address_id',
158
                        'referencedColumnName' => 'id',
159
                    ),
160
                    'table' => 'address',
161
                ),
162
                '_data' => array(
163
                    'Address' => array(
164
                        '_table' => 'address',
165
                        '_identifier' => 'sku',
166
                        'sku' => '1',
167
                        'postal_code' => '34000',
168
                        'city' => 'Montpellier',
169
                    ),
170
                ),
171
            )
172
        );
173
    }
174
175
    /**
176
     * Set up manyToMany relation fixtures
177
     */
178
    private function setUpManyToManyRelation()
179
    {
180
        $this->data['user']['_related']['manyToMany'] = array(
181
            'Group' => array(
182
                '_relation' => array(
183
                    'targetEntity' => 'Group',
184
                    'joinTable' => array(
185
                        'name' => 'users_groups',
186
                        'joinColumn' => array(
187
                            'name' => 'user_id',
188
                            'referencedColumnName' => 'id',
189
                        ),
190
                        'inverseJoinColumn' => array(
191
                            'name' => 'group_id',
192
                            'referencedColumnName' => 'id',
193
                        ),
194
                    ),
195
                    'table' => 'groups',
196
                ),
197
                '_data' => array(
198
                    0 => array(
199
                        'Group' => array(
200
                            '_table' => 'groups',
201
                            '_identifier' => 'sku',
202
                            'sku' => '0123456789azerty',
203
                            'created_at' => '2015-06-01T22:22:00+0200',
204
                        ),
205
                    ),
206
                )
207
            )
208
        );
209
    }
210
}
211