1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataTypes\Tests\Phpunit; |
4
|
|
|
|
5
|
|
|
use DataTypes\DataType; |
6
|
|
|
use DataTypes\DataTypeFactory; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use OutOfBoundsException; |
9
|
|
|
use PHPUnit_Framework_TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers DataTypes\DataTypeFactory |
13
|
|
|
* |
14
|
|
|
* @group DataTypes |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0+ |
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
18
|
|
|
* @author Daniel Kinzler |
19
|
|
|
* @author Thiemo Mättig |
20
|
|
|
*/ |
21
|
|
|
class DataTypeFactoryTest extends PHPUnit_Framework_TestCase { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @dataProvider valueTypesProvider |
25
|
|
|
*/ |
26
|
|
|
public function testConstructor( array $valueTypes ) { |
27
|
|
|
$instance = new DataTypeFactory( $valueTypes ); |
28
|
|
|
$this->assertInstanceOf( DataTypeFactory::class, $instance ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function valueTypesProvider() { |
32
|
|
|
return [ |
33
|
|
|
[ [] ], |
34
|
|
|
[ [ 'string' => 'string' ] ], |
35
|
|
|
[ [ 'customType' => 'customValueType' ] ], |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @dataProvider invalidConstructorArgumentProvider |
41
|
|
|
*/ |
42
|
|
|
public function testConstructorThrowsException( array $argument ) { |
43
|
|
|
$this->setExpectedException( InvalidArgumentException::class ); |
44
|
|
|
new DataTypeFactory( $argument ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function invalidConstructorArgumentProvider() { |
48
|
|
|
return [ |
49
|
|
|
[ [ 'string' => '' ] ], |
50
|
|
|
[ [ 'string' => 1 ] ], |
51
|
|
|
[ [ 'string' => new DataType( 'string', 'string' ) ] ], |
52
|
|
|
[ [ '' => 'string' ] ], |
53
|
|
|
[ [ 0 => 'string' ] ], |
54
|
|
|
[ [ 0 => new DataType( 'string', 'string' ) ] ], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGetTypeIds() { |
59
|
|
|
$instance = new DataTypeFactory( [ 'customType' => 'string' ] ); |
60
|
|
|
|
61
|
|
|
$expected = [ 'customType' ]; |
62
|
|
|
$this->assertSame( $expected, $instance->getTypeIds() ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testGetType() { |
66
|
|
|
$instance = new DataTypeFactory( [ 'customType' => 'string' ] ); |
67
|
|
|
|
68
|
|
|
$expected = new DataType( 'customType', 'string' ); |
69
|
|
|
$this->assertEquals( $expected, $instance->getType( 'customType' ) ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetUnknownType() { |
73
|
|
|
$instance = new DataTypeFactory( [] ); |
74
|
|
|
|
75
|
|
|
$this->setExpectedException( OutOfBoundsException::class ); |
76
|
|
|
$instance->getType( 'unknownTypeId' ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetTypes() { |
80
|
|
|
$instance = new DataTypeFactory( [ 'customType' => 'string' ] ); |
81
|
|
|
|
82
|
|
|
$expected = [ 'customType' => new DataType( 'customType', 'string' ) ]; |
83
|
|
|
$this->assertEquals( $expected, $instance->getTypes() ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function provideDataTypeBuilder() { |
87
|
|
|
return [ |
88
|
|
|
[ |
89
|
|
|
'data-type', |
90
|
|
|
[ 'data-type' => 'valuetype' ], |
91
|
|
|
'valuetype' |
92
|
|
|
], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @dataProvider provideDataTypeBuilder |
98
|
|
|
*/ |
99
|
|
|
public function testDataTypeBuilder( $id, $types, $expected ) { |
100
|
|
|
$factory = new DataTypeFactory( $types ); |
101
|
|
|
|
102
|
|
|
$type = $factory->getType( $id ); |
103
|
|
|
|
104
|
|
|
$this->assertEquals( $id, $type->getId() ); |
105
|
|
|
$this->assertEquals( $expected, $type->getDataValueType() ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|