Completed
Push — master ( 264630...c90943 )
by mw
230:08 queued 195:34
created

tests/phpunit/Unit/DataTypeRegistryTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\DataTypeRegistry;
6
use SMWDataItem as DataItem;
7
8
/**
9
 * @covers \SMW\DataTypeRegistry
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.9
14
 *
15
 * @author mwjames
16
 */
17
class DataTypeRegistryTest extends \PHPUnit_Framework_TestCase {
18
19
	private $dataTypeRegistry;
20
21
	protected function setUp() {
22
		parent::setUp();
23
24
		$this->dataTypeRegistry = DataTypeRegistry::getInstance();
25
	}
26
27
	protected function tearDown() {
28
		$this->dataTypeRegistry->clear();
29
30
		parent::tearDown();
31
	}
32
33
	public function testGetInstance() {
34
35
		$this->assertInstanceOf(
36
			'\SMW\DataTypeRegistry',
37
			$this->dataTypeRegistry
38
		);
39
40
		$this->assertSame(
41
			$this->dataTypeRegistry,
42
			DataTypeRegistry::getInstance()
43
		);
44
45
		DataTypeRegistry::clear();
46
47
		$this->assertNotSame(
48
			$this->dataTypeRegistry,
49
			DataTypeRegistry::getInstance()
50
		);
51
	}
52
53
	public function testRegisterDatatype() {
54
55
		$this->assertNull(
56
			$this->dataTypeRegistry->getDataTypeClassById( '_foo' ),
57
			'Asserts that prior registration getDataTypeClassById() returns null'
58
		);
59
60
		$this->dataTypeRegistry
61
			->registerDataType( '_foo', '\SMW\Tests\FooValue', DataItem::TYPE_NOTYPE, 'FooValue' );
62
63
		$this->assertEquals(
64
			'\SMW\Tests\FooValue',
65
			$this->dataTypeRegistry->getDataTypeClassById( '_foo' ),
66
			'Asserts that getDataTypeClassById() returns the registered class'
67
		);
68
69
		$this->assertEquals(
70
			DataItem::TYPE_NOTYPE,
71
			$this->dataTypeRegistry->getDataItemId( '_foo' )
72
		);
73
74
		$this->assertEquals(
75
			'FooValue',
76
			$this->dataTypeRegistry->findTypeLabel( '_foo' )
77
		);
78
79
		$this->assertEmpty(
80
			$this->dataTypeRegistry->findTypeLabel( 'FooNoLabel' )
81
		);
82
83
		$this->assertEquals(
84
			DataItem::TYPE_NOTYPE,
85
			$this->dataTypeRegistry->getDataItemId( 'FooBar' )
86
		);
87
	}
88
89
	public function testRegisterDatatypeIdAndAlias() {
90
91
		$this->dataTypeRegistry
92
			->registerDataType( '_foo', '\SMW\Tests\FooValue', DataItem::TYPE_NOTYPE, 'FooValue' );
93
94
		$this->assertEmpty(
95
			$this->dataTypeRegistry->findTypeId( 'FooBar' )
96
		);
97
98
		$this->dataTypeRegistry->registerDataTypeAlias( '_foo', 'FooBar' );
99
100
		$this->assertTrue(
101
			$this->dataTypeRegistry->isKnownTypeId( '_foo' )
102
		);
103
104
		$this->assertEquals(
105
			'_foo',
106
			$this->dataTypeRegistry->findTypeId( 'FooBar' ),
107
			'Asserts that findTypeID returns the registered alias label'
108
		);
109
	}
110
111
	public function testGetDefaultDataItemTypeIdForValidDataItemType() {
112
		$this->assertInternalType(
113
			'string',
114
			$this->dataTypeRegistry->getDefaultDataItemTypeId( 1 )
115
		);
116
	}
117
118
	public function testGetDefaultDataItemTypeIdForInvalidDataItemType() {
119
		$this->assertNull(
120
			$this->dataTypeRegistry->getDefaultDataItemTypeId( 9999 )
121
		);
122
	}
123
124
	public function testFindCanonicalLabelById() {
125
		$this->assertSame(
126
			'Text',
127
			$this->dataTypeRegistry->findCanonicalLabelById( '_txt' )
128
		);
129
	}
130
131
	public function testTypeIdAndLabelAsLanguageIndependantInvocation() {
132
		$instance = new DataTypeRegistry(
133
			array( '_wpg' => 'Page' ),
134
			array( 'URI'  => '_uri' ),
135
			array()
136
		);
137
138
		$this->assertEquals(
139
			'_wpg',
140
			$instance->findTypeId( 'Page' ),
141
			'Asserts that findTypeID returns empty label'
142
		);
143
144
		$this->assertEquals(
145
			array( '_wpg' => 'Page' ),
146
			$instance->getKnownTypeLabels(),
147
			'Asserts that getKnownTypeLabels returns an array'
148
		);
149
	}
150
151
	public function testKnownAliasAsLanguageIndependantInvocation() {
152
153
		$instance = new DataTypeRegistry(
154
			array(),
155
			array( 'URI'  => '_uri' ),
156
			array()
157
		);
158
159
		$this->assertEquals(
160
			array( 'URI'  => '_uri' ),
161
			$instance->getKnownTypeAliases(),
162
			'Asserts that getKnownTypeAliases returns an array'
163
		);
164
	}
165
166
	public function testExtraneousCallbackFunction() {
167
168
		$instance = new DataTypeRegistry();
169
		$arg = 'foo';
170
171
		$instance->registerExtraneousFunction(
172
			'foo',
173
			function ( $arg ) {
174
				return 'bar' . $arg;
175
			}
176
		);
177
178
		$this->assertInternalType(
179
			'array',
180
			$instance->getExtraneousFunctions()
181
		);
182
	}
183
184
	public function testRegisterDataValueFormatter() {
185
186
		$dataValueFormatter = $this->getMockBuilder( '\SMW\DataValues\ValueFormatters\DataValueFormatter' )
187
			->disableOriginalConstructor()
188
			->getMock();
189
190
		$dataValueFormatter->expects( $this->never() )
191
			->method( 'isFormatterFor' );
192
193
		$instance = new DataTypeRegistry();
194
		$instance->registerDataValueFormatter( $dataValueFormatter );
195
	}
196
197
	public function testRegisterDVDescriptionDeserializer() {
198
199
		$descriptionDeserializer = $this->getMockBuilder( '\SMW\Deserializers\DVDescriptionDeserializer\DescriptionDeserializer' )
200
			->disableOriginalConstructor()
201
			->getMock();
202
203
		$descriptionDeserializer->expects( $this->never() )
204
			->method( 'isDeserializerFor' );
205
206
		$instance = new DataTypeRegistry();
207
		$instance->registerDVDescriptionDeserializer( $descriptionDeserializer );
208
	}
209
210
	public function testLookupByLabelIsCaseInsensitive() {
211
		$caseVariants = array(
212
			'page',
213
			'Page',
214
			'PAGE',
215
			'pAgE',
216
		);
217
218
		foreach ( $caseVariants as $caseVariant ) {
219
			$this->assertRegistryFindsIdForLabels( $caseVariant, $caseVariants );
220
			$this->assertRegistryFindsIdForAliases( $caseVariant, $caseVariants );
221
		}
222
	}
223
224
	public function testFindTypeIdByLanguage() {
225
226
		$instance = new DataTypeRegistry();
227
228
		$this->assertSame(
229
			'_num',
230
			$instance->findTypeIdByLanguage( 'Número', 'es' )
231
		);
232
233
		$this->assertSame(
234
			'_num',
235
			$instance->findTypeIdByLanguage( '数值型', 'zh-Hans' )
236
		);
237
	}
238
239
	protected function assertRegistryFindsIdForLabels( $inputLabel, array $equivalentLabels ) {
240
		$id = '_wpg';
241
242
		$registry = new DataTypeRegistry(
243
			array(),
244
			array( $inputLabel => $id ),
245
			array()
246
		);
247
248
		foreach ( $equivalentLabels as $caseVariant ) {
249
			$this->assertEquals( $id, $registry->findTypeId( $caseVariant ) );
250
		}
251
	}
252
253
	protected function assertRegistryFindsIdForAliases( $inputLabel, array $equivalentLabels ) {
254
		$id = '_wpg';
255
256
		$registry = new DataTypeRegistry(
257
			array( $id => $inputLabel ),
258
			array(),
259
			array()
260
		);
261
262
		foreach ( $equivalentLabels as $caseVariant ) {
263
			$this->assertEquals( $id, $registry->findTypeId( $caseVariant ) );
264
		}
265
	}
266
267
}
268
269
class FooValue {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
270
}
271