summaryrefslogtreecommitdiff
blob: 8995e06c8bb637f1170e2faaa514e0878f6b5d83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php

namespace SMW\Tests;

use SMW\FormatFactory;

/**
 * @covers \SMW\FormatFactory
 *
 * @group SMW
 * @group SMWExtension
 * @group SMWQueries
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class FormatFactoryTest extends \PHPUnit_Framework_TestCase {

	public function testSingleton() {
		$instance = FormatFactory::singleton();

		$this->assertInstanceOf( 'SMW\FormatFactory', $instance );
		$this->assertTrue( FormatFactory::singleton() === $instance );

		global $smwgResultFormats, $smwgResultAliases;

		foreach ( $smwgResultFormats as $formatName => $printerClass ) {
			$this->assertTrue( $instance->hasFormat( $formatName ) );
			$this->assertInstanceOf( $printerClass, $instance->getPrinter( $formatName ) );
		}

		foreach ( $smwgResultAliases as $formatName => $aliases ) {
			$printerClass = $smwgResultFormats[$formatName];

			foreach ( $aliases as $alias ) {
				$this->assertTrue( $instance->hasFormat( $alias ) );
				$this->assertInstanceOf( $printerClass, $instance->getPrinter( $formatName ) );
			}
		}
	}

	/**
	 * @return FormatFactory
	 */
	protected function getNewInstance() {
//		$reflector = new \ReflectionClass( 'SMW\FormatFactory' );
//		$constructor = $reflector->getConstructor();
//		$constructor->setAccessible( true );
//		return $constructor->invoke( $reflector );
		return new FormatFactory();
	}

	public function testRegisterFormat() {
		$factory = $this->getNewInstance();

		$factory->registerFormat( 'table', 'SMWTablePrinter' );
		$factory->registerFormat( 'list', 'SMWListResultPrinter' );

		$this->assertArrayEquals( array( 'table', 'list' ), $factory->getFormats() );

		$factory->registerFormat( 'table', 'SMWListResultPrinter' );

		$printer = $factory->getPrinter( 'table' );

		$this->assertInstanceOf( 'SMWListResultPrinter', $printer );
	}

	public function testRegisterAliases() {
		$factory = $this->getNewInstance();

		$this->assertEquals( 'foo', $factory->getCanonicalName( 'foo' ) );

		$factory->registerAliases( 'foo', array() );
		$factory->registerAliases( 'foo', array( 'bar' ) );
		$factory->registerAliases( 'foo', array( 'baz' ) );
		$factory->registerAliases( 'ohi', array( 'there', 'o_O' ) );

		$this->assertEquals( 'foo', $factory->getCanonicalName( 'foo' ) );

		$this->assertEquals( 'foo', $factory->getCanonicalName( 'bar' ) );
		$this->assertEquals( 'foo', $factory->getCanonicalName( 'baz' ) );

		$this->assertEquals( 'ohi', $factory->getCanonicalName( 'there' ) );
		$this->assertEquals( 'ohi', $factory->getCanonicalName( 'o_O' ) );

		$factory->registerAliases( 'foo', array( 'o_O' ) );

		$this->assertEquals( 'foo', $factory->getCanonicalName( 'o_O' ) );
	}

	public function testGetPrinter() {
		$factory = FormatFactory::singleton();

		foreach ( $factory->getFormats() as $format ) {
			$printer = $factory->getPrinter( $format );
			$this->assertInstanceOf( 'SMW\QueryResultPrinter', $printer );
		}

		// In case there are no formats PHPUnit would otherwise complain here.
		$this->assertTrue( true );
	}

	public function testGetFormats() {
		$factory = $this->getNewInstance();

		$this->assertInternalType( 'array', $factory->getFormats() );

		$factory->registerFormat( 'table', 'SMWTablePrinter' );
		$factory->registerFormat( 'list', 'SMWListResultPrinter' );

		$factory->registerAliases( 'foo', array( 'bar' ) );
		$factory->registerAliases( 'foo', array( 'baz' ) );
		$factory->registerAliases( 'ohi', array( 'there', 'o_O' ) );

		$formats = $factory->getFormats();
		$this->assertInternalType( 'array', $formats );

		$this->assertArrayEquals( array( 'table', 'list' ), $formats );
	}

	public function testHasFormat() {
		$factory = $this->getNewInstance();

		$this->assertFalse( $factory->hasFormat( 'ohi' ) );

		$factory->registerFormat( 'ohi', 'SMWTablePrinter' );
		$factory->registerAliases( 'ohi', array( 'there', 'o_O' ) );

		$this->assertTrue( $factory->hasFormat( 'ohi' ) );
		$this->assertTrue( $factory->hasFormat( 'there' ) );
		$this->assertTrue( $factory->hasFormat( 'o_O' ) );

		$factory = FormatFactory::singleton();

		foreach ( $factory->getFormats() as $format ) {
			$this->assertTrue( $factory->hasFormat( $format ) );
		}
	}

	/**
	 * @test FormatFactory::getPrinter
	 *
	 * @since 1.9
	 */
	public function testGetPrinterException() {
		$this->SetExpectedException( 'MWException' );

		$factory = $this->getNewInstance();
		$factory->getPrinter( 'lula' );

		$this->assertTrue( true );
	}

	/**
	 * @test FormatFactory::getCanonicalName
	 *
	 * @since 1.9
	 */
	public function testGetCanonicalNameException() {
		$this->SetExpectedException( 'MWException' );

		$factory = $this->getNewInstance();
		$factory->getCanonicalName( 9001 );

		$this->assertTrue( true );
	}

	/**
	 * @test FormatFactory::registerFormat
	 * @dataProvider getRegisterFormatExceptioDataProvider
	 *
	 * @since 1.9
	 */
	public function testRegisterFormatException( $formatName, $class ) {
		$this->SetExpectedException( 'MWException' );

		$factory = $this->getNewInstance();
		$factory->registerFormat( $formatName, $class );
		$this->assertTrue( true );
	}

	/**
	 * @test FormatFactory::registerAliases
	 * @dataProvider getRegisterAliasesExceptioDataProvider
	 *
	 * @since 1.9
	 */
	public function testRegisterAliasesException( $formatName, array $aliases ) {
		$this->SetExpectedException( 'MWException' );

		$factory = $this->getNewInstance();
		$factory->registerAliases( $formatName, $aliases );
		$this->assertTrue( true );
	}

	/**
	 * Register format exception data provider
	 *
	 * @return array
	 */
	public function getRegisterFormatExceptioDataProvider() {
		return array(
			array( 1001, 'Foo' ),
			array( 'Foo', 9001 ),
		);
	}

	/**
	 * Register aliases exception data provider
	 *
	 * @return array
	 */
	public function getRegisterAliasesExceptioDataProvider() {
		return array(
			array( 1001, array( 'Foo' => 'Bar' ) ),
			array( 'Foo', array( 'Foo' => 9001 ) ),
		);
	}

	protected function objectAssociativeSort( array &$array ) {
		uasort(
			$array,
			function ( $a, $b ) {
				return serialize( $a ) > serialize( $b ) ? 1 : -1;
			}
		);
	}

	protected function assertArrayEquals( array $expected, array $actual, $ordered = false, $named = false ) {
		if ( !$ordered ) {
			$this->objectAssociativeSort( $expected );
			$this->objectAssociativeSort( $actual );
		}

		if ( !$named ) {
			$expected = array_values( $expected );
			$actual = array_values( $actual );
		}

		call_user_func_array(
			array( $this, 'assertEquals' ),
			array_merge( array( $expected, $actual ), array_slice( func_get_args(), 4 ) )
		);
	}

}