DataSourceTestCase   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 248
Duplicated Lines 36.29 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 90
loc 248
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 50 1
randomDataSource() 0 6 ?
emptyDataSource() 0 1 ?
uniqueValue() 0 1 ?
A testCreate() 0 9 1
A testCount() 0 9 1
A testLength() 13 13 1
A testOffset() 13 13 1
A testSearchCriteria() 16 16 1
A testSortCriteria() 16 16 1
A testIsSorted() 16 16 1
A testIsFiltered() 16 16 1
A testIsSliced() 0 17 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
namespace Cubiche\Core\Collections\Tests\Units\DataSource;
11
12
use Cubiche\Core\Comparable\Comparator;
13
use Cubiche\Core\Comparable\ComparatorInterface;
14
use Cubiche\Core\Specification\Criteria;
15
use Cubiche\Core\Specification\SpecificationInterface;
16
use Cubiche\Core\Collections\DataSource\DataSourceInterface;
17
use Cubiche\Core\Collections\Tests\Units\TestCase;
18
use mageekguy\atoum\adapter as Adapter;
19
use mageekguy\atoum\annotations\extractor as Extractor;
20
use mageekguy\atoum\asserter\generator as Generator;
21
use mageekguy\atoum\test\assertion\manager as Manager;
22
use mageekguy\atoum\tools\variable\analyzer as Analyzer;
23
24
/**
25
 * DataSourceTestCase class.
26
 *
27
 * @author Ivannis Suárez Jerez <[email protected]>
28
 */
29
abstract class DataSourceTestCase extends TestCase
30
{
31
    /**
32
     * @param Adapter   $adapter
33
     * @param Extractor $annotationExtractor
34
     * @param Generator $asserterGenerator
35
     * @param Manager   $assertionManager
36
     * @param \Closure  $reflectionClassFactory
37
     * @param \Closure  $phpExtensionFactory
38
     * @param Analyzer  $analyzer
39
     */
40
    public function __construct(
41
        Adapter $adapter = null,
42
        Extractor $annotationExtractor = null,
43
        Generator $asserterGenerator = null,
44
        Manager $assertionManager = null,
45
        \Closure $reflectionClassFactory = null,
46
        \Closure $phpExtensionFactory = null,
47
        Analyzer $analyzer = null
48
    ) {
49
        parent::__construct(
50
            $adapter,
51
            $annotationExtractor,
52
            $asserterGenerator,
53
            $assertionManager,
54
            $reflectionClassFactory,
55
            $phpExtensionFactory,
56
            $analyzer
57
        );
58
59
        $this->getAssertionManager()
60
            ->setHandler(
61
                'randomDataSource',
62
                function (
63
                    SpecificationInterface $searchCriteria = null,
64
                    ComparatorInterface $sortCriteria = null,
65
                    $offset = null,
66
                    $length = null
67
                ) {
68
                    return $this->randomDataSource(
69
                        $searchCriteria,
70
                        $sortCriteria,
71
                        $offset,
72
                        $length
73
                    );
74
                }
75
            )
76
            ->setHandler(
77
                'emptyDataSource',
78
                function () {
79
                    return $this->emptyDataSource();
80
                }
81
            )
82
            ->setHandler(
83
                'uniqueValue',
84
                function () {
85
                    return $this->uniqueValue();
86
                }
87
            )
88
        ;
89
    }
90
91
    /**
92
     * @param SpecificationInterface|null $searchCriteria
93
     * @param ComparatorInterface|null    $sortCriteria
94
     * @param null                        $offset
95
     * @param null                        $length
96
     *
97
     * @return mixed
98
     */
99
    abstract protected function randomDataSource(
100
        SpecificationInterface $searchCriteria = null,
101
        ComparatorInterface $sortCriteria = null,
102
        $offset = null,
103
        $length = null
104
    );
105
106
    /**
107
     * @return DataSourceInterface
108
     */
109
    abstract protected function emptyDataSource();
110
111
    /**
112
     * @return mixed
113
     */
114
    abstract protected function uniqueValue();
115
116
    /*
117
     * Test create.
118
     */
119
    public function testCreate()
120
    {
121
        $this
122
            ->given($datasource = $this->randomDataSource())
123
            ->then
124
                ->datasource($datasource)
125
                    ->isInstanceOf(DataSourceInterface::class)
126
        ;
127
    }
128
129
    /*
130
     * Test count.
131
     */
132
    public function testCount()
133
    {
134
        $this
135
            ->given($datasource = $this->randomDataSource())
136
            ->then
137
                ->integer($datasource->count())
138
                    ->isGreaterThan(0)
139
        ;
140
    }
141
142
    /*
143
     * Test length.
144
     */
145 View Code Duplication
    public function testLength()
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...
146
    {
147
        $this
148
            ->given($datasource = $this->randomDataSource())
149
            ->then
150
                ->variable($datasource->length())
151
                    ->isNull()
152
            ->given($datasource = $this->randomDataSource(null, null, null, 10))
153
            ->then
154
                ->variable($datasource->length())
155
                    ->isEqualTo(10)
156
        ;
157
    }
158
159
    /*
160
     * Test offset.
161
     */
162 View Code Duplication
    public function testOffset()
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...
163
    {
164
        $this
165
            ->given($datasource = $this->randomDataSource())
166
            ->then
167
                ->variable($datasource->offset())
168
                    ->isNull()
169
            ->given($datasource = $this->randomDataSource(null, null, 8))
170
            ->then
171
                ->variable($datasource->offset())
172
                    ->isEqualTo(8)
173
        ;
174
    }
175
176
    /*
177
     * Test searchCriteria.
178
     */
179 View Code Duplication
    public function testSearchCriteria()
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...
180
    {
181
        $this
182
            ->given($datasource = $this->randomDataSource())
183
            ->then
184
                ->variable($datasource->searchCriteria())
185
                    ->isNull()
186
            ->given(
187
                $searchCriteria = Criteria::false(),
188
                $datasource = $this->randomDataSource($searchCriteria)
189
            )
190
            ->then
191
                ->variable($datasource->searchCriteria())
192
                    ->isEqualTo($searchCriteria)
193
        ;
194
    }
195
196
    /*
197
     * Test sortCriteria.
198
     */
199 View Code Duplication
    public function testSortCriteria()
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...
200
    {
201
        $this
202
            ->given($datasource = $this->randomDataSource())
203
            ->then
204
                ->variable($datasource->sortCriteria())
205
                    ->isNull()
206
            ->given(
207
                $sortCriteria = new Comparator(),
208
                $datasource = $this->randomDataSource(null, $sortCriteria)
209
            )
210
            ->then
211
                ->variable($datasource->sortCriteria())
212
                    ->isEqualTo($sortCriteria)
213
        ;
214
    }
215
216
    /*
217
     * Test isSorted.
218
     */
219 View Code Duplication
    public function testIsSorted()
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...
220
    {
221
        $this
222
            ->given($datasource = $this->randomDataSource())
223
            ->then
224
                ->boolean($datasource->isSorted())
225
                    ->isFalse()
226
            ->given(
227
                $sortCriteria = new Comparator(),
228
                $datasource = $this->randomDataSource(null, $sortCriteria)
229
            )
230
            ->then
231
                ->boolean($datasource->isSorted())
232
                    ->isTrue()
233
        ;
234
    }
235
236
    /*
237
     * Test isFiltered.
238
     */
239 View Code Duplication
    public function testIsFiltered()
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...
240
    {
241
        $this
242
            ->given($datasource = $this->randomDataSource())
243
            ->then
244
                ->boolean($datasource->isFiltered())
245
                    ->isFalse()
246
            ->given(
247
                $searchCriteria = Criteria::false(),
248
                $datasource = $this->randomDataSource($searchCriteria)
249
            )
250
            ->then
251
                ->boolean($datasource->isFiltered())
252
                    ->isTrue()
253
        ;
254
    }
255
256
    /*
257
     * Test isSliced.
258
     */
259
    public function testIsSliced()
260
    {
261
        $this
262
            ->given($datasource = $this->randomDataSource())
263
            ->then
264
                ->boolean($datasource->isSliced())
265
                    ->isFalse()
266
            ->given($datasource = $this->randomDataSource(null, null, 8))
267
            ->then
268
                ->boolean($datasource->isSliced())
269
                    ->isTrue()
270
            ->given($datasource = $this->randomDataSource(null, null, null, 10))
271
            ->then
272
                ->boolean($datasource->isSliced())
273
                    ->isTrue()
274
        ;
275
    }
276
}
277