RepositoryTestCase::testRemove()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 25
Ratio 100 %

Importance

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