DataTypeFactoryTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 4 1
A valueTypesProvider() 0 7 1
A testConstructorThrowsException() 0 4 1
A invalidConstructorArgumentProvider() 0 10 1
A testGetTypeIds() 0 6 1
A testGetType() 0 6 1
A testGetUnknownType() 0 6 1
A testGetTypes() 0 6 1
A provideDataTypeBuilder() 0 9 1
A testDataTypeBuilder() 0 8 1
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