Skip to content

Commit 28fcb8d

Browse files
committed
TypedObjectArrayTest added
1 parent da96691 commit 28fcb8d

File tree

3 files changed

+106
-7
lines changed

3 files changed

+106
-7
lines changed

src/TypedObjectArray.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class TypedObjectArray extends ArrayObject
3535
*/
3636
public function __construct(string $type, array $array = [])
3737
{
38+
if (!class_exists($type)){
39+
throw new InvalidArgumentException('Type passed to '.__CLASS__.' must be an existing class');
40+
}
41+
3842
//check elements of passed array
3943
//I will find another method
4044
foreach ($array as $element) {

tests/TypedArrayTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ public function testCreateInstanceWithNotAllowedType()
6464
function rightTypedArrayProvider()
6565
{
6666
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
67+
['array', [[1],[2]]],
68+
['bool', [true, false]],
69+
['callable', [function(){}, function(){}]],
70+
['float', [1.1, 2.2]],
71+
['int', [1, 2]],
72+
['object', [(object)['name' => 'foo'], (object)['name' => 'bar']]],
73+
['string', ['a', 'b']],
7474
];
7575
}
7676

tests/TypedObjectArrayTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\TypedObjectArray;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Typed Object Array Test
17+
*/
18+
class TypedObjectArrayTest extends TestCase
19+
{
20+
/**
21+
* Test new instance.
22+
*/
23+
public function testCreateInstance()
24+
{
25+
$this->assertInstanceOf(TypedObjectArray::class, (new TypedObjectArray(ArrayObject::class)));
26+
}
27+
28+
/**
29+
* Test new instance with not existent class.
30+
*
31+
* @expectedException InvalidArgumentException
32+
*/
33+
public function testCreateInstanceWithNotExistentClass()
34+
{
35+
$this->assertInstanceOf(TypedObjectArray::class, (new TypedObjectArray(ArrayBadObject::class)));
36+
}
37+
38+
/**
39+
* Test new instance passing right typed array to constructor.
40+
*/
41+
public function testCreateInstanceWithRightTypedArray()
42+
{
43+
$this->assertInstanceOf(
44+
TypedObjectArray::class,
45+
(new TypedObjectArray(
46+
ArrayObject::class, [
47+
new ArrayObject([1,2,3]),
48+
new ArrayObject([1.1,2.2,3.3]),
49+
new ArrayObject(['a','b','c'])
50+
])
51+
)
52+
);
53+
}
54+
55+
/**
56+
* Test new instance passing array with invalid element to constructor.
57+
*
58+
* @expectedException InvalidArgumentException
59+
*/
60+
public function testCreateInstanceWithWrongTypedArray()
61+
{
62+
$this->assertInstanceOf(
63+
TypedObjectArray::class,
64+
(new TypedObjectArray(
65+
ArrayObject::class, [
66+
new ArrayObject([1,2,3]),
67+
new ArrayObject([1.1,2.2,3.3]),
68+
new SplStack()
69+
])
70+
)
71+
);
72+
}
73+
74+
/**
75+
* Test assign to array a right typed value.
76+
*/
77+
public function testAssignrRightTypedValueToArray()
78+
{
79+
$array = new TypedObjectArray(ArrayObject::class);
80+
$array[] = new ArrayObject([1,2,3]);
81+
82+
$this->assertEquals(1, $array->count());
83+
}
84+
85+
/**
86+
* Test assign to array a wrong typed value.
87+
*
88+
* @expectedException InvalidArgumentException
89+
*/
90+
public function testAssignWrongTypedValueToArray()
91+
{
92+
$array = new TypedObjectArray(ArrayObject::class);
93+
$array[] = new SplStack();
94+
}
95+
}

0 commit comments

Comments
 (0)