|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the JVal package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace JVal; |
|
11
|
|
|
|
|
12
|
|
|
use JVal\Testing\BaseTestCase; |
|
13
|
|
|
|
|
14
|
|
|
class TypesTest extends BaseTestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @dataProvider instanceTypeProvider |
|
18
|
|
|
* |
|
19
|
|
|
* @param mixed $instance |
|
20
|
|
|
* @param string $expectedType |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testGetPrimitiveType($instance, $expectedType) |
|
23
|
|
|
{ |
|
24
|
|
|
$actualType = Types::getPrimitiveTypeOf($instance); |
|
25
|
|
|
$this->assertEquals($expectedType, $actualType); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @expectedException \JVal\Exception\UnsupportedTypeException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testGetPrimitiveTypeThrowsOnUnsupportedType() |
|
32
|
|
|
{ |
|
33
|
|
|
Types::getPrimitiveTypeOf(fopen(__FILE__, 'r')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testIsA() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->assertTrue(Types::isA([], Types::TYPE_ARRAY)); |
|
39
|
|
|
$this->assertTrue(Types::isA('foo', Types::TYPE_STRING)); |
|
40
|
|
|
$this->assertTrue(Types::isA(123, Types::TYPE_INTEGER)); |
|
41
|
|
|
$this->assertTrue(Types::isA(123, Types::TYPE_NUMBER)); |
|
42
|
|
|
$this->assertFalse(Types::isA(1.23, Types::TYPE_INTEGER)); |
|
43
|
|
|
$this->assertTrue(Types::isA(1.23, Types::TYPE_NUMBER)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function instanceTypeProvider() |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
[[1, 2, 3], Types::TYPE_ARRAY], |
|
50
|
|
|
[true, Types::TYPE_BOOLEAN], |
|
51
|
|
|
[123, Types::TYPE_INTEGER], |
|
52
|
|
|
[1.23, Types::TYPE_NUMBER], |
|
53
|
|
|
[null, Types::TYPE_NULL], |
|
54
|
|
|
[new \stdClass(), Types::TYPE_OBJECT], |
|
55
|
|
|
['123', Types::TYPE_STRING], |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|