Completed
Push — master ( fc9c20...b71285 )
by
unknown
01:52
created

testGetDefinitionSummary_notEqualForDifferentDataTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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