|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Linna Array. |
| 5 | + * |
| 6 | + * @author Sebastian Rapetti <[email protected]> |
| 7 | + * @copyright (c) 2017, Sebastian Rapetti |
| 8 | + * @license http://opensource.org/licenses/MIT MIT License |
| 9 | + */ |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +use Linna\TypedArray; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Typed Array Test |
| 17 | + */ |
| 18 | +class TypedArrayTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Provide allowed types. |
| 22 | + * |
| 23 | + * @return array |
| 24 | + */ |
| 25 | + function allowedTypeProvider() |
| 26 | + { |
| 27 | + return [ |
| 28 | + ['array'], |
| 29 | + ['bool'], |
| 30 | + ['callable'], |
| 31 | + ['float'], |
| 32 | + ['int'], |
| 33 | + ['object'], |
| 34 | + ['string'], |
| 35 | + ]; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Test new instance. |
| 40 | + * |
| 41 | + * @dataProvider allowedTypeProvider |
| 42 | + */ |
| 43 | + public function testCreateInstance($type) |
| 44 | + { |
| 45 | + $this->assertInstanceOf(TypedArray::class, (new TypedArray($type))); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test new instance with not allowed type. |
| 50 | + * |
| 51 | + * @expectedException InvalidArgumentException |
| 52 | + */ |
| 53 | + public function testCreateInstanceWithNotAllowedType() |
| 54 | + { |
| 55 | + $this->assertInstanceOf(TypedArray::class, (new TypedArray('notAllowedType'))); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * Provide arrays of right typed values. |
| 61 | + * |
| 62 | + * @return array |
| 63 | + */ |
| 64 | + function rightTypedArrayProvider() |
| 65 | + { |
| 66 | + return [ |
| 67 | + ['array', [[1],[2]]], //array of arrays |
| 68 | + ['bool', [true, false]], //array of bools |
| 69 | + ['callable', [function(){}, function(){}]], //aaray of callables |
| 70 | + ['float', [1.1, 2.2]], //array of floats |
| 71 | + ['int', [1, 2]], //array of integers |
| 72 | + ['object', [(object)['name' => 'foo'], (object)['name' => 'bar']]], //array of objects |
| 73 | + ['string', ['a', 'b']], //array of strings |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test new instance passing right typed array to constructor. |
| 79 | + * |
| 80 | + * @dataProvider rightTypedArrayProvider |
| 81 | + */ |
| 82 | + public function testCreateInstanceWithRightTypedArray($type, $array) |
| 83 | + { |
| 84 | + $this->assertInstanceOf(TypedArray::class, (new TypedArray($type, $array))); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Provide arrays of wrong typed values. |
| 89 | + * |
| 90 | + * @return array |
| 91 | + */ |
| 92 | + function wrongTypedArrayProvider() |
| 93 | + { |
| 94 | + return [ |
| 95 | + ['array', [true, false]], |
| 96 | + ['array', [function(){}, function(){}]], |
| 97 | + ['array', [1.1, 2.2]], |
| 98 | + ['array', [1, 2]], |
| 99 | + ['array', [(object)['name' => 'foo'], (object)['name' => 'bar']]], |
| 100 | + ['array', ['a', 'b']], |
| 101 | + |
| 102 | + ['bool', [[1],[2]]], |
| 103 | + ['bool', [function(){}, function(){}]], |
| 104 | + ['bool', [1.1, 2.2]], |
| 105 | + ['bool', [1, 2]], |
| 106 | + ['bool', [(object)['name' => 'foo'], (object)['name' => 'bar']]], |
| 107 | + ['bool', ['a', 'b']], |
| 108 | + |
| 109 | + ['callable', [[1],[2]]], |
| 110 | + ['callable', [true, false]], |
| 111 | + ['callable', [1.1, 2.2]], |
| 112 | + ['callable', [1, 2]], |
| 113 | + ['callable', [(object)['name' => 'foo'], (object)['name' => 'bar']]], |
| 114 | + ['callable', ['a', 'b']], |
| 115 | + |
| 116 | + ['float', [[1],[2]]], |
| 117 | + ['float', [true, false]], |
| 118 | + ['float', [function(){}, function(){}]], |
| 119 | + ['float', [1, 2]], |
| 120 | + ['float', [(object)['name' => 'foo'], (object)['name' => 'bar']]], |
| 121 | + ['float', ['a', 'b']], |
| 122 | + |
| 123 | + ['int', [[1],[2]]], |
| 124 | + ['int', [true, false]], |
| 125 | + ['int', [function(){}, function(){}]], |
| 126 | + ['int', [1.1, 2.2]], |
| 127 | + ['int', [(object)['name' => 'foo'], (object)['name' => 'bar']]], |
| 128 | + ['int', ['a', 'b']], |
| 129 | + |
| 130 | + ['object', [[1],[2]]], |
| 131 | + ['object', [true, false]], |
| 132 | + //skip this because closure pass as object |
| 133 | + //['object', [function(){}, function(){}]], |
| 134 | + ['object', [1.1, 2.2]], |
| 135 | + ['object', [1, 2]], |
| 136 | + ['object', ['a', 'b']], |
| 137 | + |
| 138 | + ['string', [[1],[2]]], |
| 139 | + ['string', [true, false]], |
| 140 | + ['string', [function(){}, function(){}]], |
| 141 | + ['string', [1.1, 2.2]], |
| 142 | + ['string', [1, 2]], |
| 143 | + ['string', [(object)['name' => 'foo'], (object)['name' => 'bar']]], |
| 144 | + ]; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Test new instance passing array with invalid element to constructor. |
| 149 | + * |
| 150 | + * @dataProvider wrongTypedArrayProvider |
| 151 | + * @expectedException InvalidArgumentException |
| 152 | + */ |
| 153 | + public function testCreateInstanceWithWrongTypedArray($type, $array) |
| 154 | + { |
| 155 | + $this->assertInstanceOf(TypedArray::class, (new TypedArray($type, $array))); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Provide values of right types. |
| 160 | + * |
| 161 | + * @return array |
| 162 | + */ |
| 163 | + function rightTypedValueProvider() |
| 164 | + { |
| 165 | + return [ |
| 166 | + ['array',[1]], |
| 167 | + ['bool', true], |
| 168 | + ['callable', function(){}], |
| 169 | + ['float', 1.1], |
| 170 | + ['int', 1], |
| 171 | + ['object',(object)['name' => 'foo']], |
| 172 | + ['string', 'a'], |
| 173 | + ]; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Test assign to array a right typed value. |
| 178 | + * |
| 179 | + * @dataProvider rightTypedValueProvider |
| 180 | + */ |
| 181 | + public function testAssignrRightTypedValueToArray($type, $value) |
| 182 | + { |
| 183 | + $array = new TypedArray($type); |
| 184 | + $array[] = $value; |
| 185 | + |
| 186 | + $this->assertEquals(1, $array->count()); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Provide values of wrong types. |
| 191 | + * |
| 192 | + * @return array |
| 193 | + */ |
| 194 | + function wrongTypedValueProvider() |
| 195 | + { |
| 196 | + return [ |
| 197 | + ['array', true], |
| 198 | + ['array', function(){}], |
| 199 | + ['array', 1.1], |
| 200 | + ['array', 1], |
| 201 | + ['array', (object)['name' => 'foo']], |
| 202 | + ['array', 'a'], |
| 203 | + |
| 204 | + ['bool', [1]], |
| 205 | + ['bool', function(){}], |
| 206 | + ['bool', 1.1], |
| 207 | + ['bool', 1], |
| 208 | + ['bool', (object)['name' => 'foo']], |
| 209 | + ['bool', 'a'], |
| 210 | + |
| 211 | + ['callable', [1]], |
| 212 | + ['callable', true], |
| 213 | + ['callable', 1.1], |
| 214 | + ['callable', 1], |
| 215 | + ['callable', (object)['name' => 'foo']], |
| 216 | + ['callable', 'a'], |
| 217 | + |
| 218 | + ['float', [1]], |
| 219 | + ['float', true], |
| 220 | + ['float', function(){}], |
| 221 | + ['float', 1], |
| 222 | + ['float', (object)['name' => 'foo']], |
| 223 | + ['float', 'a'], |
| 224 | + |
| 225 | + ['int', [1]], |
| 226 | + ['int', true], |
| 227 | + ['int', function(){}], |
| 228 | + ['int', 1.1], |
| 229 | + ['int', (object)['name' => 'foo']], |
| 230 | + ['int', 'a', 'b'], |
| 231 | + |
| 232 | + ['object', [1]], |
| 233 | + ['object', true], |
| 234 | + //skip this because closure pass as object |
| 235 | + //['object',[function(){}, function(){}]], |
| 236 | + ['object', 1.1], |
| 237 | + ['object', 1], |
| 238 | + ['object', 'a'], |
| 239 | + |
| 240 | + ['string', [1]], |
| 241 | + ['string', true], |
| 242 | + ['string', function(){}], |
| 243 | + ['string', 1.1], |
| 244 | + ['string', 1], |
| 245 | + ['string', (object)['name' => 'foo']], |
| 246 | + ]; |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Test assign to array a wrong typed value. |
| 251 | + * |
| 252 | + * @dataProvider wrongTypedValueProvider |
| 253 | + * @expectedException InvalidArgumentException |
| 254 | + */ |
| 255 | + public function testAssignWrongTypedValueToArray($type, $value) |
| 256 | + { |
| 257 | + $array = new TypedArray($type); |
| 258 | + $array[] = $value; |
| 259 | + } |
| 260 | +} |
0 commit comments