CollectionValidatorTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 193
rs 10
c 3
b 0
f 0

9 Methods

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