DataTypesModuleTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 174
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B provideDataTypesModuleAndResourceDefinition() 0 25 2
A testGetDataTypeFactory() 0 3 1
A provideInvalidResourceDefinition() 0 49 1
A testGetConfigVarName() 0 10 1
A testConstructorErrors() 0 6 1
A testGetDefinitionSummary() 0 12 1
A testGetDefinitionSummary_notEqualForDifferentDataTypes() 0 20 1
A makeDefinition() 0 6 1
A getContext() 0 5 1
1
<?php
2
3
namespace DataTypes\Tests\Modules;
4
5
use DataTypes\DataTypeFactory;
6
use DataTypes\Modules\DataTypesModule;
7
use Exception;
8
use ResourceLoaderContext;
9
10
/**
11
 * @covers DataTypes\Modules\DataTypesModule
12
 *
13
 * @group DataTypes
14
 *
15
 * @license GPL-2.0+
16
 * @author Daniel Werner < [email protected] >
17
 * @author Katie Filbert < [email protected] >
18
 */
19
class DataTypesModuleTest extends \PHPUnit_Framework_TestCase {
20
21
	/**
22
	 * @return array [instance, resource definition]
23
	 */
24
	public function provideDataTypesModuleAndResourceDefinition() {
25
		$dataTypeFactory = new DataTypeFactory( [ 'url' => 'string' ] );
26
27
		$validResourceDefinitions = [
28
			[
29
				'datatypesconfigvarname' => 'foo',
30
				'datatypefactory' => function() {
31
					return new DataTypeFactory( [] );
32
				}
33
			],
34
			[
35
				'datatypesconfigvarname' => 'bar123',
36
				'datatypefactory' => $dataTypeFactory
37
			],
38
		];
39
40
		$cases = [];
41
42
		foreach ( $validResourceDefinitions as $definition ) {
43
			$instance = new DataTypesModule( $definition );
44
			$cases[] = [ $instance, $definition ];
45
		}
46
47
		return $cases;
48
	}
49
50
	/**
51
	 * @dataProvider provideDataTypesModuleAndResourceDefinition
52
	 *
53
	 * @param DataTypesModule $module
54
	 */
55
	public function testGetDataTypeFactory( DataTypesModule $module ) {
56
		$this->assertInstanceOf( DataTypeFactory::class, $module->getDataTypeFactory() );
57
	}
58
59
	/**
60
	 * @return array [invalid resource definition, case description]
61
	 */
62
	public function provideInvalidResourceDefinition() {
63
		$dataTypeFactory = new DataTypeFactory( [] );
64
65
		$validDefinition = [
66
			'datatypesconfigvarname' => 'foo',
67
			'datatypefactory' => function() {
68
				return new DataTypeFactory( [] );
69
			}
70
		];
71
72
		return [
73
			[
74
				[
75
					'datatypesconfigvarname' => 'foo'
76
				],
77
				'missing "datatypefactory" field'
78
			],
79
			[
80
				[
81
					'datatypefactory' => $dataTypeFactory
82
				],
83
				'missing "datatypesconfigvarname" field'
84
			],
85
			[
86
				[],
87
				'all fields missing'
88
			],
89
			[
90
				array_merge(
91
					$validDefinition,
92
					[
93
						'datatypefactory' => 123
94
					]
95
				),
96
				'"datatypefactory" field has value of wrong type'
97
			],
98
			[
99
				array_merge(
100
					$validDefinition,
101
					[
102
						'datatypefactory' => function() {
103
							return null;
104
						}
105
					]
106
				),
107
				'"datatypefactory" callback does not return a DataTypeFactory instance'
108
			],
109
		];
110
	}
111
112
	/**
113
	 * @dataProvider provideDataTypesModuleAndResourceDefinition
114
	 *
115
	 * @param DataTypesModule $module
116
	 * @param array $definition
117
	 */
118
	public function testGetConfigVarName( DataTypesModule $module, array $definition ) {
119
		$configVarName = $module->getConfigVarName();
120
121
		$this->assertInternalType( 'string', $configVarName );
122
123
		$this->assertSame(
124
			$definition['datatypesconfigvarname'],
125
			$module->getConfigVarName()
126
		);
127
	}
128
129
	/**
130
	 * @dataProvider provideInvalidResourceDefinition
131
	 *
132
	 * @param array $definition
133
	 * @param string $caseDescription
134
	 */
135
	public function testConstructorErrors( array $definition, $caseDescription ) {
136
		$this->setName( 'Instantiation raises exception in case ' . $caseDescription );
137
		$this->setExpectedException( Exception::class );
138
139
		new DataTypesModule( $definition );
140
	}
141
142
	public function testGetDefinitionSummary() {
143
		$definition = $this->makeDefinition(
144
			[ 'foo' => 'string' ]
145
		);
146
147
		$module = new DataTypesModule( $definition );
148
		$summary = $module->getDefinitionSummary( $this->getContext() );
149
150
		$this->assertInternalType( 'array', $summary );
151
		$this->assertArrayHasKey( 0, $summary );
152
		$this->assertArrayHasKey( 'dataHash', $summary[0] );
153
	}
154
155
	public function testGetDefinitionSummary_notEqualForDifferentDataTypes() {
156
		$definition1 = $this->makeDefinition( [
157
			'foo' => 'string'
158
		] );
159
160
		$definition2 = $this->makeDefinition( [
161
			'foo' => 'string',
162
			'bar' => 'string'
163
		] );
164
165
		$module1 = new DataTypesModule( $definition1 );
166
		$module2 = new DataTypesModule( $definition2 );
167
168
		$context = $this->getContext();
169
170
		$summary1 = $module1->getDefinitionSummary( $context );
171
		$summary2 = $module2->getDefinitionSummary( $context );
172
173
		$this->assertNotEquals( $summary1[0]['dataHash'], $summary2[0]['dataHash'] );
174
	}
175
176
	private function makeDefinition( array $dataTypes ) {
177
		return [
178
			'datatypesconfigvarname' => 'foo123',
179
			'datatypefactory' => new DataTypeFactory( $dataTypes )
180
		];
181
	}
182
183
	/**
184
	 * @return ResourceLoaderContext
185
	 */
186
	private function getContext() {
187
		return $this->getMockBuilder( ResourceLoaderContext::class )
188
			->disableOriginalConstructor()
189
			->getMock();
190
	}
191
192
}
193