CollectionAttributeTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 207
rs 10
c 4
b 1
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidator() 0 6 1
A itShouldCheckIfIsArray() 0 8 1
A itShouldCheckIfEach() 0 21 1
A itShouldCheckIfHasKeyFormat() 0 18 1
A itShouldCheckIfEndsWith() 0 22 1
A itShouldCheckIfContains() 0 14 1
A itShouldCheckIfHasKey() 0 18 1
A itShouldCheckIfLength() 0 18 1
A itShouldCheckIfIsNotEmpty() 0 18 1
A itShouldCheckIfStartsWith() 0 22 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 9/20/14
5
 * Time: 6:59 PM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Tests\NilPortugues\Validator\Attribute\Collection;
12
13
use NilPortugues\Validator\Validator;
14
15
class CollectionAttributeTest extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @return \NilPortugues\Validator\Attribute\Collection\CollectionAttribute
19
     */
20
    protected function getValidator()
21
    {
22
        $validator = Validator::create();
23
24
        return $validator->isArray('propertyName');
25
    }
26
27
    /**
28
     * @test
29
     */
30
    public function itShouldCheckIfIsArray()
31
    {
32
        $this->assertTrue($this->getValidator()->validate(['hello', 'world']));
33
        $this->assertTrue($this->getValidator()->validate(new \ArrayObject()));
34
        $this->assertTrue($this->getValidator()->validate(new \SplFixedArray()));
35
36
        $this->assertFalse($this->getValidator()->validate('a'));
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function itShouldCheckIfEach()
43
    {
44
        $array       = ['hello', 'world'];
45
        $arrayObject = new \ArrayObject($array);
46
        $fixedArray  = (new \SplFixedArray())->fromArray($array);
47
48
        $validator     = Validator::create();
49
        $valueIsString = $validator->isString('value')->isAlpha();
50
        $keyIsInteger  = $validator->isInteger('key')->isPositiveOrZero();
51
52
        $this->assertTrue($this->getValidator()->each($valueIsString)->validate($array));
53
        $this->assertFalse($this->getValidator()->each($valueIsString)->validate(['yes', 'n@']));
54
55
        $this->assertTrue($this->getValidator()->each($valueIsString, $keyIsInteger)->validate($array));
56
57
        $this->assertTrue($this->getValidator()->each($valueIsString)->validate($arrayObject));
58
        $this->assertTrue($this->getValidator()->each($valueIsString, $keyIsInteger)->validate($arrayObject));
59
60
        $this->assertTrue($this->getValidator()->each($valueIsString)->validate($fixedArray));
61
        $this->assertTrue($this->getValidator()->each($valueIsString, $keyIsInteger)->validate($fixedArray));
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function itShouldCheckIfHasKeyFormat()
68
    {
69
        $array       = ['one' => 'hello', 'two' => 'world'];
70
        $arrayObject = new \ArrayObject($array);
71
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
72
73
        $validator    = Validator::create();
74
        $keyIsString  = $validator->isString('key')->isAlpha();
75
        $keyIsInteger = $validator->isInteger('key')->isPositiveOrZero();
76
77
78
79
        $this->assertTrue($this->getValidator()->hasKeyFormat($keyIsString)->validate($array));
80
81
        $this->assertTrue($this->getValidator()->hasKeyFormat($keyIsString)->validate($arrayObject));
82
83
        $this->assertTrue($this->getValidator()->hasKeyFormat($keyIsInteger)->validate($fixedArray));
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function itShouldCheckIfEndsWith()
90
    {
91
        $array       = ['one' => 'hello', 'two' => 1];
92
        $arrayObject = new \ArrayObject($array);
93
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
94
95
        $this->assertTrue($this->getValidator()->endsWith('1', false)->validate($array));
96
        $this->assertTrue($this->getValidator()->endsWith('1', false)->validate($arrayObject));
97
        $this->assertTrue($this->getValidator()->endsWith('1', false)->validate($fixedArray));
98
99
        $array       = [1, 2, 3];
100
        $arrayObject = new \ArrayObject($array);
101
        $fixedArray  = (new \SplFixedArray())->fromArray($array);
102
103
        $this->assertTrue($this->getValidator()->endsWith(3, true)->validate($array));
104
        $this->assertTrue($this->getValidator()->endsWith(3, true)->validate($arrayObject));
105
        $this->assertTrue($this->getValidator()->endsWith(3, true)->validate($fixedArray));
106
107
        $this->assertFalse($this->getValidator()->endsWith('3', true)->validate($array));
108
        $this->assertFalse($this->getValidator()->endsWith('3', true)->validate($arrayObject));
109
        $this->assertFalse($this->getValidator()->endsWith('3', true)->validate($fixedArray));
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function itShouldCheckIfContains()
116
    {
117
        $array       = ['one' => 'hello', 'two' => 1];
118
        $arrayObject = new \ArrayObject($array);
119
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
120
121
        $this->assertTrue($this->getValidator()->contains('hello', false)->validate($array));
122
        $this->assertTrue($this->getValidator()->contains('hello', false)->validate($arrayObject));
123
        $this->assertTrue($this->getValidator()->contains('hello', false)->validate($fixedArray));
124
125
        $this->assertTrue($this->getValidator()->contains(1, true)->validate($array));
126
        $this->assertTrue($this->getValidator()->contains(1, true)->validate($arrayObject));
127
        $this->assertTrue($this->getValidator()->contains(1, true)->validate($fixedArray));
128
    }
129
130
    /**
131
     * @test
132
     */
133
    public function itShouldCheckIfHasKey()
134
    {
135
        $array       = ['one' => 'hello', 'two' => 1];
136
        $arrayObject = new \ArrayObject($array);
137
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
138
139
        $this->assertTrue($this->getValidator()->hasKey('one')->validate($array));
140
        $this->assertTrue($this->getValidator()->hasKey('one')->validate($arrayObject));
141
        $this->assertTrue($this->getValidator()->hasKey(0)->validate($fixedArray));
142
143
        $array       = [];
144
        $arrayObject = new \ArrayObject($array);
145
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
146
147
        $this->assertFalse($this->getValidator()->hasKey(0)->validate($array));
148
        $this->assertFalse($this->getValidator()->hasKey(0)->validate($arrayObject));
149
        $this->assertFalse($this->getValidator()->hasKey(0)->validate($fixedArray));
150
    }
151
152
    /**
153
     * @test
154
     */
155
    public function itShouldCheckIfLength()
156
    {
157
        $array       = ['one' => 'hello', 'two' => 1];
158
        $arrayObject = new \ArrayObject($array);
159
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
160
161
        $this->assertTrue($this->getValidator()->hasLength(2)->validate($array));
162
        $this->assertTrue($this->getValidator()->hasLength(2)->validate($arrayObject));
163
        $this->assertTrue($this->getValidator()->hasLength(2)->validate($fixedArray));
164
165
        $array       = [];
166
        $arrayObject = new \ArrayObject($array);
167
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
168
169
        $this->assertTrue($this->getValidator()->hasLength(0)->validate($array));
170
        $this->assertTrue($this->getValidator()->hasLength(0)->validate($arrayObject));
171
        $this->assertTrue($this->getValidator()->hasLength(0)->validate($fixedArray));
172
    }
173
174
    /**
175
     * @test
176
     */
177
    public function itShouldCheckIfIsNotEmpty()
178
    {
179
        $array       = ['one' => 'hello', 'two' => 1];
180
        $arrayObject = new \ArrayObject($array);
181
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
182
183
        $this->assertTrue($this->getValidator()->isNotEmpty()->validate($array));
184
        $this->assertTrue($this->getValidator()->isNotEmpty()->validate($arrayObject));
185
        $this->assertTrue($this->getValidator()->isNotEmpty()->validate($fixedArray));
186
187
        $array       = [];
188
        $arrayObject = new \ArrayObject($array);
189
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
190
191
        $this->assertFalse($this->getValidator()->isNotEmpty()->validate($array));
192
        $this->assertFalse($this->getValidator()->isNotEmpty()->validate($arrayObject));
193
        $this->assertFalse($this->getValidator()->isNotEmpty()->validate($fixedArray));
194
    }
195
196
    /**
197
     * @test
198
     */
199
    public function itShouldCheckIfStartsWith()
200
    {
201
        $array       = ['one' => 'hello', 'two' => 1];
202
        $arrayObject = new \ArrayObject($array);
203
        $fixedArray  = (new \SplFixedArray())->fromArray(\array_values($array));
204
205
        $this->assertTrue($this->getValidator()->startsWith('hello', false)->validate($array));
206
        $this->assertTrue($this->getValidator()->startsWith('hello', false)->validate($arrayObject));
207
        $this->assertTrue($this->getValidator()->startsWith('hello', false)->validate($fixedArray));
208
209
        $array       = [1, 2, 3];
210
        $arrayObject = new \ArrayObject($array);
211
        $fixedArray  = (new \SplFixedArray())->fromArray($array);
212
213
        $this->assertTrue($this->getValidator()->startsWith(1, true)->validate($array));
214
        $this->assertTrue($this->getValidator()->startsWith(1, true)->validate($arrayObject));
215
        $this->assertTrue($this->getValidator()->startsWith(1, true)->validate($fixedArray));
216
217
        $this->assertFalse($this->getValidator()->startsWith('1', true)->validate($array));
218
        $this->assertFalse($this->getValidator()->startsWith('1', true)->validate($arrayObject));
219
        $this->assertFalse($this->getValidator()->startsWith('1', true)->validate($fixedArray));
220
    }
221
}
222