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