DocumentQueryRepositoryTests   C
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 157
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 19
dl 157
loc 157
rs 6.875

5 Methods

Rating   Name   Duplication   Size   Complexity  
B randomValue() 42 42 3
B uniqueValue() 42 42 1
A emptyRepository() 4 4 1
A comparator() 4 4 1
B testGet() 41 41 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
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Infrastructure\Repository\Doctrine\Tests\Units\ODM\MongoDB;
12
13
use Cubiche\Core\Comparable\Comparator;
14
use Cubiche\Core\Comparable\Direction;
15
use Cubiche\Core\Specification\Criteria;
16
use Cubiche\Domain\Geolocation\Coordinate;
17
use Cubiche\Domain\Repository\Tests\Fixtures\Address;
18
use Cubiche\Domain\Repository\Tests\Fixtures\AddressId;
19
use Cubiche\Domain\Repository\Tests\Fixtures\Phonenumber;
20
use Cubiche\Domain\Repository\Tests\Fixtures\Role;
21
use Cubiche\Domain\Repository\Tests\Fixtures\User;
22
use Cubiche\Domain\Repository\Tests\Fixtures\UserId;
23
use Cubiche\Domain\Repository\Tests\Units\QueryRepositoryTestCase;
24
use Cubiche\Domain\System\StringLiteral;
25
use Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\DocumentQueryRepository;
26
27
/**
28
 * DocumentQueryRepositoryTests class.
29
 *
30
 * @engine isolate
31
 *
32
 * @author Ivannis Suárez Jerez <[email protected]>
33
 */
34 View Code Duplication
class DocumentQueryRepositoryTests extends QueryRepositoryTestCase
0 ignored issues
show
Duplication introduced by
This class 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...
35
{
36
    use DocumentManagerTestCaseTrait;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function randomValue()
42
    {
43
        $user = new User(UserId::next(), 'User-'.\rand(1, 100), \rand(1, 100), $this->faker->email);
44
45
        $user->setFax(new Phonenumber($this->faker->phoneNumber));
46
        foreach (range(1, 3) as $key) {
47
            $user->addPhonenumber(new Phonenumber($this->faker->phoneNumber));
48
        }
49
50
        $user->setMainRole($this->faker->randomElement(Role::values()));
51
        foreach (range(1, 3) as $key) {
52
            $user->addRole($this->faker->randomElement(Role::values()));
53
        }
54
55
        $user->addAddress(
56
            new Address(
57
                AddressId::next(),
58
                'Home',
59
                $this->faker->streetAddress,
60
                $this->faker->postcode,
61
                $this->faker->city,
62
                Coordinate::fromLatLng($this->faker->latitude, $this->faker->longitude)
63
            )
64
        );
65
66
        $user->addAddress(
67
            new Address(
68
                AddressId::next(),
69
                'Work',
70
                $this->faker->streetAddress,
71
                $this->faker->postcode,
72
                $this->faker->city,
73
                Coordinate::fromLatLng($this->faker->latitude, $this->faker->longitude)
74
            )
75
        );
76
77
        $user->setLanguageLevel('english', $this->faker->randomDigit);
78
        $user->setLanguageLevel('spanish', $this->faker->randomDigit);
79
        $user->setLanguageLevel('french', $this->faker->randomDigit);
80
81
        return $user;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function uniqueValue()
88
    {
89
        $user = new User(UserId::next(), 'Methuselah', 1000, $this->faker->email);
90
91
        $user->setFax(new Phonenumber('+34-208-1234567'));
92
        $user->addPhonenumber(new Phonenumber('+34 685 165 267'));
93
        $user->addPhonenumber(new Phonenumber($this->faker->phoneNumber));
94
        $user->addPhonenumber(new Phonenumber($this->faker->phoneNumber));
95
96
        $user->setMainRole(Role::ROLE_ADMIN());
97
        $user->addRole(Role::ROLE_ADMIN());
98
        $user->addRole(Role::ROLE_ADMIN());
99
        $user->addRole(Role::ROLE_USER());
100
101
        $user->setLanguageLevel('english', 10);
102
        $user->setLanguageLevel('spanish', 7);
103
        $user->setLanguageLevel('french', 2.5);
104
105
        $user->addAddress(
106
            new Address(
107
                AddressId::next(),
108
                'Home',
109
                $this->faker->streetAddress,
110
                $this->faker->postcode,
111
                $this->faker->city,
112
                Coordinate::fromLatLng(41.390205, 2.154007)
113
            )
114
        );
115
116
        $user->addAddress(
117
            new Address(
118
                AddressId::next(),
119
                'Work',
120
                $this->faker->streetAddress,
121
                $this->faker->postcode,
122
                $this->faker->city,
123
                Coordinate::fromLatLng($this->faker->latitude, $this->faker->longitude)
124
            )
125
        );
126
127
        return $user;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    protected function emptyRepository()
134
    {
135
        return new DocumentQueryRepository($this->dm()->getRepository(User::class), $this->documentDataSourceFactory());
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    protected function comparator()
142
    {
143
        return Comparator::by(Criteria::property('age'), Direction::DESC());
144
    }
145
146
    /**
147
     * Test get.
148
     */
149
    public function testGet()
150
    {
151
        parent::testGet();
152
153
        $this
154
            ->given($repository = $this->randomRepository())
155
            ->and($unique = $this->uniqueValue())
156
            ->and($friend = new User(UserId::next(), 'Ivan', 32, $this->faker->email))
157
            ->and($friend1 = new User(UserId::next(), 'Karel', 32, $this->faker->email))
158
            ->and($repository->persist($friend))
159
            ->and($repository->persist($friend1))
160
            ->and($unique->addFriend($friend))
161
            ->and($unique->addFriend($friend1))
162
            ->and($repository->persist($unique))
163
            ->when(
164
                /** @var User $object */
165
                $object = $repository->get($unique->id())
166
            )
167
            ->then()
168
                ->object($object->fullName())
169
                    ->isEqualTo(StringLiteral::fromNative('Methuselah'))
170
                ->integer($object->languagesLevel()->get('english'))
171
                    ->isEqualTo(10)
172
                ->boolean($object->mainRole()->is(Role::ROLE_ADMIN))
173
                    ->isTrue()
174
                ->array($object->roles()->toArray())
175
                    ->contains(Role::ROLE_ADMIN)
176
                ->array($object->phonenumbers()->toArray())
177
                    ->contains('+34 685 165 267')
178
                ->string($object->fax()->number())
179
                    ->isEqualTo('+34-208-1234567')
180
                ->array($object->addresses()->toArray())
181
                    ->object[0]->isInstanceOf(Address::class)
182
                ->string($object->addresses()->toArray()[0]->name())
183
                    ->isEqualTo('Home')
184
                ->object($object->addresses()->toArray()[0]->coordinate())
185
                    ->isEqualTo(Coordinate::fromLatLng(41.390205, 2.154007))
186
                ->integer($object->friends()->count())
187
                    ->isEqualTo(2)
188
        ;
189
    }
190
}
191