QueryRepositoryTestCase::testPersist()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 31

Duplication

Lines 39
Ratio 100 %

Importance

Changes 0
Metric Value
dl 39
loc 39
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 31
nc 1
nop 0
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\Domain\Repository\Tests\Units;
13
14
use Cubiche\Core\Collections\Tests\Units\CollectionTestCase;
15
use Cubiche\Core\Comparable\Comparator;
16
use Cubiche\Core\Specification\Criteria;
17
use Cubiche\Domain\Repository\QueryRepositoryInterface;
18
use Cubiche\Domain\Repository\Tests\Fixtures\User;
19
use Cubiche\Domain\Repository\Tests\Fixtures\UserId;
20
use mageekguy\atoum\adapter as Adapter;
21
use mageekguy\atoum\annotations\extractor as Extractor;
22
use mageekguy\atoum\asserter\generator as Generator;
23
use mageekguy\atoum\test\assertion\manager as Manager;
24
use mageekguy\atoum\tools\variable\analyzer as Analyzer;
25
26
/**
27
 * QueryRepositoryTestCase Class.
28
 *
29
 * @author Ivannis Suárez Jerez <[email protected]>
30
 * @author Karel Osorio Ramírez <[email protected]>
31
 */
32
abstract class QueryRepositoryTestCase extends CollectionTestCase
33
{
34
    /**
35
     * @param Adapter   $adapter
36
     * @param Extractor $annotationExtractor
37
     * @param Generator $asserterGenerator
38
     * @param Manager   $assertionManager
39
     * @param \Closure  $reflectionClassFactory
40
     * @param \Closure  $phpExtensionFactory
41
     * @param Analyzer  $analyzer
42
     */
43 View Code Duplication
    public function __construct(
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...
44
        Adapter $adapter = null,
45
        Extractor $annotationExtractor = null,
46
        Generator $asserterGenerator = null,
47
        Manager $assertionManager = null,
48
        \Closure $reflectionClassFactory = null,
49
        \Closure $phpExtensionFactory = null,
50
        Analyzer $analyzer = null
51
    ) {
52
        parent::__construct(
53
            $adapter,
54
            $annotationExtractor,
55
            $asserterGenerator,
56
            $assertionManager,
57
            $reflectionClassFactory,
58
            $phpExtensionFactory,
59
            $analyzer
60
        );
61
62
        $this->getAssertionManager()
63
            ->setHandler(
64
                'randomRepository',
65
                function ($size = null) {
66
                    return $this->randomRepository($size);
67
                }
68
            )
69
            ->setHandler(
70
                'emptyRepository',
71
                function () {
72
                    return $this->emptyRepository();
73
                }
74
            )
75
        ;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected function randomCollection($size = null)
82
    {
83
        return $this->randomRepository($size);
84
    }
85
86
    /**
87
     * @param int $size
88
     *
89
     * @return QueryRepositoryInterface
90
     */
91
    protected function randomRepository($size = null)
92
    {
93
        $repository = $this->emptyRepository();
94
        foreach ($this->randomValues($size) as $randomValue) {
95
            $repository->persist($randomValue);
96
        }
97
98
        return $repository;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    protected function emptyCollection()
105
    {
106
        return $this->emptyRepository();
107
    }
108
109
    /**
110
     * @return QueryRepositoryInterface
111
     */
112
    abstract protected function emptyRepository();
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    protected function randomValue()
118
    {
119
        return new User(UserId::next(), 'User-'.\rand(1, 100), \rand(1, 100), $this->faker->email);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    protected function uniqueValue()
126
    {
127
        return new User(UserId::next(), 'Methuselah', 1000, $this->faker->email);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    protected function comparator()
134
    {
135
        return Comparator::by(Criteria::property('age'));
136
    }
137
138
    /**
139
     * Test get.
140
     */
141 View Code Duplication
    public function testGet()
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...
142
    {
143
        $this
144
            ->given($repository = $this->randomRepository())
145
            ->and($unique = $this->uniqueValue())
146
            ->when($repository->persist($unique))
147
            ->then()
148
                ->object($repository->get($unique->id()))
149
                    ->isEqualTo($unique);
150
    }
151
152
    /**
153
     * Test persist.
154
     */
155 View Code Duplication
    public function testPersist()
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...
156
    {
157
        $this
158
            ->given($repository = $this->randomRepository())
159
            ->and($unique = $this->uniqueValue())
160
            ->then()
161
                ->variable($repository->get($unique->id()))
162
                    ->isNull()
163
            ->and()
164
            ->when($repository->persist($unique))
165
            ->then()
166
                ->object($repository->get($unique->id()))
167
                    ->isEqualTo($unique)
168
        ;
169
170
        $this
171
            ->given($repository = $this->randomRepository())
172
            ->given($id = UserId::next())
173
            ->then()
174
                ->exception(function () use ($repository, $id) {
175
                    $repository->persist($id);
176
                })->isInstanceOf(\InvalidArgumentException::class)
177
        ;
178
179
        $this
180
            ->given($repository = $this->emptyRepository())
181
            ->and($value = $this->randomValue())
182
            ->and($age = $value->age())
183
            ->when(function () use ($repository, $value, $age) {
184
                $repository->persist($value);
185
                $value->setAge($age + 1);
186
                $repository->persist($value);
187
            })
188
            ->then()
189
                ->object($other = $repository->findOne(Criteria::property('id')->eq($value->id())))
190
                    ->integer($other->age())
191
                        ->isEqualTo($age + 1)
192
        ;
193
    }
194
195
    /**
196
     * Test persistAll.
197
     */
198 View Code Duplication
    public function testPersistAll()
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...
199
    {
200
        $this
201
            ->given($repository = $this->emptyRepository())
202
            ->and($unique = $this->uniqueValue())
203
            ->and($random = $this->randomValue())
204
            ->then()
205
                ->variable($repository->get($unique->id()))
206
                    ->isNull()
207
                ->variable($repository->get($random->id()))
208
                    ->isNull()
209
            ->and()
210
            ->when($repository->persistAll([$unique, $random]))
211
            ->then()
212
                ->object($repository->get($unique->id()))
213
                    ->isEqualTo($unique)
214
                ->object($repository->get($random->id()))
215
                    ->isEqualTo($random)
216
        ;
217
218
        $this
219
            ->given($repository = $this->randomRepository())
220
            ->given($id = UserId::next())
221
            ->then()
222
                ->exception(function () use ($repository, $id) {
223
                    $repository->persistAll([$id]);
224
                })->isInstanceOf(\InvalidArgumentException::class)
225
        ;
226
227
        $this
228
            ->given($repository = $this->emptyRepository())
229
            ->and($value = $this->randomValue())
230
            ->and($age = $value->age())
231
            ->when(function () use ($repository, $value, $age) {
232
                $repository->persistAll([$value]);
233
                $value->setAge($age + 1);
234
                $repository->persistAll([$value]);
235
            })
236
            ->then()
237
                ->object($other = $repository->get($value->id()))
238
                    ->integer($other->age())
239
                        ->isEqualTo($age + 1)
240
        ;
241
    }
242
243
    /**
244
     * Test remove.
245
     */
246 View Code Duplication
    public function testRemove()
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...
247
    {
248
        $this
249
            ->given($repository = $this->randomRepository())
250
            ->and($unique = $this->uniqueValue())
251
            ->when($repository->persist($unique))
252
            ->then()
253
                ->object($repository->get($unique->id()))
254
                    ->isEqualTo($unique)
255
            ->and()
256
            ->when($repository->remove($unique))
257
            ->then()
258
                ->variable($repository->get($unique->id()))
259
                    ->isNull()
260
        ;
261
262
        $this
263
            ->given($repository = $this->randomRepository())
264
            ->given($id = UserId::next())
265
            ->then()
266
                ->exception(function () use ($repository, $id) {
267
                    $repository->remove($id);
268
                })->isInstanceOf(\InvalidArgumentException::class)
269
        ;
270
    }
271
272
    /**
273
     * Test findOne.
274
     */
275
    public function testFindOne()
276
    {
277
        $this
278
            ->given($repository = $this->emptyRepository())
279
            ->and($value = $this->randomValue())
280
            ->when($repository->persist($value))
281
            ->then()
282
                ->object($repository->findOne(Criteria::property('id')->eq($value->id())))
283
                    ->isEqualTo($value)
284
        ;
285
    }
286
287
    /**
288
     * Test getIterator.
289
     */
290
    public function testGetIterator()
291
    {
292
        $this
293
            ->given($collection = $this->randomRepository())
294
            ->then()
295
                ->object($collection->getIterator())
296
                    ->isInstanceOf(\Traversable::class)
297
        ;
298
    }
299
}
300