Passed
Push — cache ( 02020d...419cd9 )
by
unknown
06:18 queued 02:04
created

DataTypesModuleTest::getContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
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
 * @licence GNU GPL v2+
12
 * @author Daniel Werner < [email protected] >
13
 * @author Katie Filbert < [email protected] >
14
 */
15
class DataTypesModuleTest extends \PHPUnit_Framework_TestCase {
16
17
	/**
18
	 * @return array [instance, resource definition]
19
	 */
20
	public function provideDataTypesModuleAndResourceDefinition() {
21
		$dataTypeFactory = new DataTypeFactory();
0 ignored issues
show
Bug introduced by
The call to DataTypeFactory::__construct() misses a required argument $valueTypes.

This check looks for function calls that miss required arguments.

Loading history...
22
23
		$validResourceDefinitions = array(
24
			array(
25
				'datatypesconfigvarname' => 'foo',
26
				'datatypefactory' => function() {
27
					return new DataTypeFactory();
0 ignored issues
show
Bug introduced by
The call to DataTypeFactory::__construct() misses a required argument $valueTypes.

This check looks for function calls that miss required arguments.

Loading history...
28
				}
29
			),
30
			array(
31
				'datatypesconfigvarname' => 'bar123',
32
				'datatypefactory' => $dataTypeFactory
33
			)
34
		);
35
36
		$cases = array();
37
38
		foreach( $validResourceDefinitions as $definition ) {
39
			$instance = new DataTypesModule( $definition );
40
			$cases[] = array( $instance, $definition );
41
		}
42
43
		return $cases;
44
	}
45
46
	/**
47
	 * @dataProvider provideDataTypesModuleAndResourceDefinition
48
	 *
49
	 * @param DataTypesModule $module
50
	 */
51
	public function testGetDataTypeFactory( DataTypesModule $module ) {
52
		$this->assertInstanceOf(
53
			'DataTypes\DataTypeFactory',
54
			$module->getDataTypeFactory()
55
		);
56
	}
57
58
	/**
59
	 * @return array [invalid resource definition, case description]
60
	 */
61
	public function provideInvalidResourceDefinition() {
62
		$dataTypeFactory = new DataTypeFactory();
0 ignored issues
show
Bug introduced by
The call to DataTypeFactory::__construct() misses a required argument $valueTypes.

This check looks for function calls that miss required arguments.

Loading history...
63
64
		$validDefinition = array(
65
			'datatypesconfigvarname' => 'foo',
66
			'datatypefactory' => function() {
67
				return new DataTypeFactory();
0 ignored issues
show
Bug introduced by
The call to DataTypeFactory::__construct() misses a required argument $valueTypes.

This check looks for function calls that miss required arguments.

Loading history...
68
			}
69
		);
70
71
		return array(
72
			array(
73
				array(
74
					'datatypesconfigvarname' => 'foo'
75
				),
76
				'missing "datatypefactory" field'
77
			),
78
			array(
79
				array(
80
					'datatypefactory' => $dataTypeFactory
81
				),
82
				'missing "datatypesconfigvarname" field'
83
			),
84
			array(
85
				array(),
86
				'all fields missing'
87
			),
88
			array(
89
				array_merge(
90
					$validDefinition,
91
					array(
92
						'datatypefactory' => 123
93
					)
94
				),
95
				'"datatypefactory" field has value of wrong type'
96
			),
97
			array(
98
				array_merge(
99
					$validDefinition,
100
					array(
101
						'datatypefactory' => function() {
102
							return null;
103
						}
104
					)
105
				),
106
				'"datatypefactory" callback does not return a DataTypeFactory instance'
107
			)
108
		);
109
	}
110
111
	/**
112
	 * @dataProvider provideDataTypesModuleAndResourceDefinition
113
	 *
114
	 * @param DataTypesModule $module
115
	 * @param array $definition
116
	 */
117
	public function testGetConfigVarName( DataTypesModule $module, array $definition ) {
118
		$configVarName = $module->getConfigVarName();
119
120
		$this->assertInternalType( 'string', $configVarName );
121
122
		$this->assertSame(
123
			$definition['datatypesconfigvarname'],
124
			$module->getConfigVarName()
125
		);
126
	}
127
128
	/**
129
	 * @dataProvider provideInvalidResourceDefinition
130
	 *
131
	 * @param array $definition
132
	 * @param string $caseDescription
133
	 */
134
	public function testConstructorErrors( array $definition, $caseDescription ) {
135
		$this->setName( 'Instantiation raises exception in case ' . $caseDescription );
136
		$this->setExpectedException( 'Exception' );
137
138
		new DataTypesModule( $definition );
139
	}
140
141
	public function testGetDefinitionSummary() {
142
		$definition = $this->makeDefinition(
143
			array( 'foo' => 'string' )
144
		);
145
146
		$module = new DataTypesModule( $definition );
147
		$summary = $module->getDefinitionSummary( $this->getContext() );
148
149
		$this->assertInternalType( 'array', $summary );
150
		$this->assertArrayHasKey( 0, $summary );
151
		$this->assertArrayHasKey( 'dataHash', $summary[0] );
152
	}
153
154
	public function testGetDefinitionSummary_notEqualForDifferentDataTypes() {
155
		$definition1 = $this->makeDefinition( array(
156
			'foo' => 'string'
157
		) );
158
159
		$definition2 = $this->makeDefinition( array(
160
			'foo' => 'string',
161
			'bar' => 'string'
162
		) );
163
164
		$module1 = new DataTypesModule( $definition1 );
165
		$module2 = new DataTypesModule( $definition2 );
166
167
		$context = $this->getContext();
168
169
		$summary1 = $module1->getDefinitionSummary( $context );
170
		$summary2 = $module2->getDefinitionSummary( $context );
171
172
		$this->assertNotEquals( $summary1[0]['dataHash'], $summary2[0]['dataHash'] );
173
	}
174
175
176
	private function makeDefinition( array $dataTypes ) {
177
		return array(
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' )
188
			->disableOriginalConstructor()
189
			->getMock();
190
	}
191
192
}
193