summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/includes/QueryPrinterFactoryTest.php
blob: ea3d0609cf8a2648ea7b573cb22a09fdced0ecab (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
<?php

namespace SMW\Tests;

use SMW\QueryPrinterFactory;
use SMW\QueryResultPrinter;
use SMW\TableResultPrinter;
use SMWListResultPrinter;

/**
 * @covers \SMW\QueryPrinterFactory
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class QueryPrinterFactoryTest extends \PHPUnit_Framework_TestCase {

	use PHPUnitCompat;

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

		$this->assertInstanceOf( QueryPrinterFactory::class, $instance );
		$this->assertTrue( QueryPrinterFactory::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 ) );
			}
		}
	}

	public function testRegisterFormat() {
		$factory = new QueryPrinterFactory();

		$factory->registerFormat( 'table', TableResultPrinter::class );
		$factory->registerFormat( 'list', SMWListResultPrinter::class );

		$this->assertContains( 'table', $factory->getFormats() );
		$this->assertContains( 'list', $factory->getFormats() );
		$this->assertCount( 2, $factory->getFormats() );

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

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

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

	public function testRegisterAliases() {
		$factory = new QueryPrinterFactory();

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

		$factory->registerAliases( 'foo', [] );
		$factory->registerAliases( 'foo', [ 'bar' ] );
		$factory->registerAliases( 'foo', [ 'baz' ] );
		$factory->registerAliases( 'ohi', [ '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', [ 'o_O' ] );

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

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

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

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

	public function testGetFormats() {
		$factory = new QueryPrinterFactory();

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

		$factory->registerFormat( 'table', TableResultPrinter::class );
		$factory->registerFormat( 'list', SMWListResultPrinter::class );

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

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

		$this->assertContains( 'table', $factory->getFormats() );
		$this->assertContains( 'list', $factory->getFormats() );
		$this->assertCount( 2, $factory->getFormats() );
	}

	public function testHasFormat() {
		$factory = new QueryPrinterFactory();

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

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

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

		$factory = QueryPrinterFactory::singleton();

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

	public function testGetPrinterThrowsException() {

		$factory = new QueryPrinterFactory();

		$this->setExpectedException( '\SMW\Query\Exception\ResultFormatNotFoundException' );
		$factory->getPrinter( 'lula' );
	}

	public function testGetCanonicalNameThrowsException() {

		$factory = new QueryPrinterFactory();

		$this->setExpectedException( 'InvalidArgumentException' );
		$factory->getCanonicalName( 9001 );
	}

	/**
	 * @dataProvider registerFormatExceptioProvider
	 */
	public function testRegisterFormatThrowsException( $formatName, $class ) {

		$factory = new QueryPrinterFactory();

		$this->setExpectedException( 'InvalidArgumentException' );
		$factory->registerFormat( $formatName, $class );
	}

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

	/**
	 * @dataProvider registerAliasesExceptionProvider
	 */
	public function testRegisterAliasesThrowsException( $formatName, array $aliases ) {

		$factory = new QueryPrinterFactory();

		$this->setExpectedException( 'InvalidArgumentException' );
		$factory->registerAliases( $formatName, $aliases );
	}

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

}