Completed
Push — master ( f8e2d1...0375a2 )
by Ivannis Suárez
07:49
created

d()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Specification\Tests\Units;
11
12
use Cubiche\Core\Specification\AndSpecification;
13
use Cubiche\Core\Specification\Criteria;
14
use Cubiche\Core\Specification\OrSpecification;
15
use Cubiche\Core\Specification\Specification;
16
use Cubiche\Core\Specification\SpecificationInterface;
17
use Cubiche\Core\Specification\SpecificationVisitorInterface;
18
use Cubiche\Core\Visitor\Tests\Units\VisiteeInterfaceTestCase;
19
20
/**
21
 * Specification Interface Test Case class.
22
 *
23
 * @author Ivannis Suárez Jerez <[email protected]>
24
 * @author Karel Osorio Ramírez <[email protected]>
25
 */
26
abstract class SpecificationInterfaceTestCase extends VisiteeInterfaceTestCase
27
{
28
    /**
29
     * Test create.
30
     */
31
    public function testCreate()
32
    {
33
        parent::testCreate();
34
35
        $this
36
            ->given($specification = $this->newDefaultTestedInstance())
37
            ->then()
38
                ->object($specification)
39
                    ->isInstanceOf(SpecificationInterface::class)
40
        ;
41
    }
42
43
    /**
44
     * Test andX.
45
     */
46 View Code Duplication
    public function testAndX()
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...
47
    {
48
        $this
49
            ->given(
50
                /** @var \Cubiche\Core\Specification\SpecificationInterface $specification */
51
                $specification = $this->newDefaultTestedInstance(),
52
                $other = $this->newRandomSpecification()
53
            )
54
            ->then()
55
            ->when($andSpecification = $specification->andX($other))
56
                ->object($andSpecification)
57
                    ->isInstanceOf(AndSpecification::class)
58
                ->object($andSpecification->left())
59
                    ->isIdenticalTo($specification)
60
                ->object($andSpecification->right())
61
                    ->isIdenticalTo($other)
62
        ;
63
    }
64
65
    /**
66
     * Test orX.
67
     */
68 View Code Duplication
    public function testOrX()
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...
69
    {
70
        $this
71
            ->given(
72
                /** @var \Cubiche\Core\Specification\SpecificationInterface $specification */
73
                $specification = $this->newDefaultTestedInstance(),
74
                $other = $this->newRandomSpecification()
75
            )
76
            ->then()
77
            ->when($orSpecification = $specification->orX($other))
78
                ->object($orSpecification)
79
                    ->isInstanceOf(OrSpecification::class)
80
                ->object($orSpecification->left())
81
                    ->isIdenticalTo($specification)
82
                ->object($orSpecification->right())
83
                    ->isIdenticalTo($other)
84
        ;
85
    }
86
87
    /**
88
     * Test magic and/or.
89
     */
90
    public function testMagicCall()
91
    {
92
        $this
93
            /* @var \Cubiche\Core\Specification\SpecificationInterface $specificationMock */
94
            ->given($specificationMock = $this->newDefaultMockTestedInstance())
95
            ->given($specification = $this->newRandomSpecification())
96
            ->when($specificationMock->and($specification))
97
                ->mock($specificationMock)
98
                    ->call('andX')
99
                        ->withArguments($specification)->once()
100
            ->when($specificationMock->or($specification))
101
                ->mock($specificationMock)
102
                    ->call('orX')
103
                        ->withArguments($specification)->once()
104
            ;
105
    }
106
107
    /**
108
     * Test not.
109
     */
110
    public function testNot()
111
    {
112
        $this
113
            /* @var \Cubiche\Core\Specification\SpecificationInterface $specification */
114
            ->given($specification = $this->newDefaultTestedInstance())
115
            ->then()
116
            ->when($notSpecification = $specification->not())
117
                ->object($notSpecification)
118
                    ->isInstanceOf(SpecificationInterface::class)
119
                ->object($notSpecification->not())
120
                    ->isEqualTo($specification)
121
        ;
122
    }
123
124
    /**
125
     * Test evaluate success.
126
     *
127
     * @dataProvider evaluateSuccessDataProvider
128
     */
129 View Code Duplication
    public function testEvaluateSuccess($value)
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...
130
    {
131
        $this
132
            /* @var \Cubiche\Core\Specification\SpecificationInterface $specification */
133
            ->given($specification = $this->newDefaultTestedInstance())
134
            ->then()
135
                ->boolean($specification->evaluate($value))
136
                    ->isTrue()
137
                ->boolean($specification->not()->evaluate($value))
138
                    ->isFalse()
139
        ;
140
    }
141
142
    /**
143
     * Test evaluate failure.
144
     *
145
     * @dataProvider evaluateFailureDataProvider
146
     */
147 View Code Duplication
    public function testEvaluateFailure($value)
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...
148
    {
149
        $this
150
            /* @var \Cubiche\Core\Specification\SpecificationInterface $specification */
151
            ->given($specification = $this->newDefaultTestedInstance())
152
            ->then()
153
                ->boolean($specification->evaluate($value))
154
                    ->isFalse()
155
                ->boolean($specification->not()->evaluate($value))
156
                    ->isTrue()
157
        ;
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    abstract protected function evaluateSuccessDataProvider();
164
165
    /**
166
     * @return array
167
     */
168
    abstract protected function evaluateFailureDataProvider();
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    protected function visitorInterface()
174
    {
175
        return SpecificationVisitorInterface::class;
176
    }
177
178
    /**
179
     * @return \Cubiche\Core\Specification\SpecificationInterface
180
     */
181
    protected function newRandomSpecification()
182
    {
183
        switch (\rand(0, 3)) {
184
            case 0:
185
                return Criteria::eq(5);
186
            case 1:
187
                return Criteria::gte(5);
188
            case 2:
189
                return Criteria::property('foo')->lte(10);
190
            case 3:
191
            default:
192
                return Criteria::false();
193
        }
194
    }
195
}
196