summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/SPARQLStore/QueryEngine/XmlResponseParserTest.php
blob: 30d930f1c4dff28c8e8eeb2e32d1c1ad2a9035bf (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
<?php

namespace SMW\Tests\SPARQLStore\QueryEngine;

use SMW\SPARQLStore\QueryEngine\XmlResponseParser;
use SMW\Tests\Utils\Fixtures\Results\FakeRawResultProvider;
use SMWExpLiteral as ExpLiteral;
use SMWExpResource as ExpResource;
use SMW\Tests\PHPUnitCompat;

/**
 * @covers \SMW\SPARQLStore\QueryEngine\XmlResponseParser
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 2.0
 *
 * @author mwjames
 */
class XmlResponseParserTest extends \PHPUnit_Framework_TestCase {

	use PHPUnitCompat;

	public function testCanConstruct() {
		$this->assertInstanceOf(
			'\SMW\SPARQLStore\QueryEngine\XmlResponseParser',
			new XmlResponseParser()
		);
	}

	/**
	 * @dataProvider rawXmlResultDocumentProvider
	 */
	public function testXmlParse( $rawXmlResult, $expectedResultRowItemInstance ) {

		$instance = new XmlResponseParser();
		$resultFormat = $instance->parse( $rawXmlResult );

		$this->assertInstanceOf(
			'\SMW\SPARQLStore\QueryEngine\RepositoryResult',
			$resultFormat
		);

		$this->assertResultFormat(
			$expectedResultRowItemInstance,
			$resultFormat
		);
	}

	public function testInvalidXmlThrowsException() {

		$rawResultProvider = new FakeRawResultProvider();

		$instance = new XmlResponseParser();

		$this->setExpectedException( '\SMW\SPARQLStore\Exception\XmlParserException' );
		$instance->parse( $rawResultProvider->getInvalidSparqlResultXml() );
	}

	protected function assertResultFormat( $expectedResultRowItemInstance, $results ) {

		if ( !is_array( $expectedResultRowItemInstance ) ) {
			$expectedResultRowItemInstance =  [ $expectedResultRowItemInstance ];
		}

		foreach ( $results as $key => $row ) {
			$this->assertResultRow( $expectedResultRowItemInstance[$key], $row );
		}
	}

	protected function assertResultRow( $expectedItemInstance, $row ) {

		foreach ( $row as $key => $item ) {

			if ( $item === null ) {
				continue;
			}

			$this->assertEquals( $expectedItemInstance, $item );
		}
	}

	public function rawXmlResultDocumentProvider() {

		$rawResultProvider = new FakeRawResultProvider();

		#0
		$provider[] = [
			$rawResultProvider->getUriResourceSparqlResultXml(),
			new ExpResource( 'http://example.org/id/Foo' )
		];

		#1
		$provider[] = [
			$rawResultProvider->getEmptySparqlResultXml(),
			null
		];

		#2 @bug 62218
		$provider[] = [
			$rawResultProvider->getNonTypeLiteralResultXml(),
			new ExpLiteral( 'Has foo' )
		];

		#3
		$provider[] = [
			$rawResultProvider->getBooleanSparqlResultXml(),
			new ExpLiteral( 'true', 'http://www.w3.org/2001/XMLSchema#boolean' )
		];

		#4
		$provider[] = [
			$rawResultProvider->getStringTypeLiteralSparqlResultXml(),
			new ExpLiteral( 'Foo', 'http://www.w3.org/2001/XMLSchema#string' )
		];

		#5
		$provider[] = [
			$rawResultProvider->getIntegerTypeLiteralSparqlResultXml(),
			new ExpLiteral( '1', 'http://www.w3.org/2001/XMLSchema#integer' )
		];

		#6
		$provider[] = [
			$rawResultProvider->getMixedRowsSparqlResultXml(),
			[
				new ExpResource( 'http://example.org/id/Foo' ),
				new ExpResource( 'http://example.org/id/Bar' ),
				new ExpLiteral( 'Quux', 'http://www.w3.org/2001/XMLSchema#string' )
			]
		];

		#7 #450
		$provider[] = [
			false,
			null
		];

		#8 #450
		$provider[] = [
			'false',
			null
		];

		#9 #626
		$provider[] = [
			'true',
			new ExpLiteral( 'true', 'http://www.w3.org/2001/XMLSchema#boolean' )
		];

		#10
		$provider[] = [
			'',
			new ExpLiteral( 'false', 'http://www.w3.org/2001/XMLSchema#boolean' )
		];

		#11
		$provider[] = [
			$rawResultProvider->getMixedRowsSparqlResultUtf8Xml(),
			[
				new ExpResource( 'http://example.org/id/F安o' ),
				new ExpResource( 'http://example.org/id/B定ar' ),
				new ExpLiteral( 'Quux安定', 'http://www.w3.org/2001/XMLSchema#string' )
			]
		];

		return $provider;
	}

}