summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/Query/QuerySourceFactoryTest.php
blob: 43e567ce40f4b60d578cf8833da994fa2f3bdac0 (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
<?php

namespace SMW\Tests\Query;

use SMW\Query\QuerySourceFactory;
use SMW\QueryEngine;
use SMW\Store;
use SMW\StoreAware;
use SMW\Tests\TestEnvironment;
use SMWQuery as Query;

/**
 * @covers SMW\Query\QuerySourceFactory
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class QuerySourceFactoryTest extends \PHPUnit_Framework_TestCase {

	private $testEnvironment;

	protected function setUp() {
		$this->testEnvironment = new TestEnvironment();

		$this->store = $this->getMockBuilder( '\SMW\Store' )
			->disableOriginalConstructor()
			->getMockForAbstractClass();

		$this->testEnvironment->registerObject( 'Store', $this->store );
	}

	protected function tearDown() {
		$this->testEnvironment->tearDown();
		parent::tearDown();
	}

	public function testCanConstruct() {

		$this->assertInstanceOf(
			QuerySourceFactory::class,
			new QuerySourceFactory( $this->store )
		);
	}

	public function testGetFromFakeSource() {

		$instance = new QuerySourceFactory(
			$this->store,
			[
				'foo' => FakeQueryEngine::class
			]
		);

		$this->assertInstanceOf(
			'\SMW\QueryEngine',
			$instance->get( 'foo' )
		);
	}

	public function testGetStandardStore() {

		$instance = new QuerySourceFactory(
			$this->store,
			[]
		);

		$this->assertInstanceOf(
			'\SMW\SQLStore\SQLStore',
			$instance->get( 'sql_store' )
		);

		$this->assertEquals(
			'SMWSQLStore',
			$instance->toString( 'sql_store' )
		);
	}

	public function testGetAsString() {

		$store = $this->getMockBuilder( '\SMW\SPARQLStore\SPARQLStore' )
			->disableOriginalConstructor()
			->getMock();

		$store->expects( $this->once() )
			->method( 'getInfo' )
			->will( $this->returnValue( [ 'SPARQLStore' ] ) );

		$instance = new QuerySourceFactory(
			$store,
			[]
		);

		$this->assertContains(
			'SPARQLStore',
			$instance->toString()
		);
	}

	public function testGetFromAnotherFakeSourceThatImplementsStoreAware() {

		$store = $this->getMockBuilder( '\SMW\Store' )
			->disableOriginalConstructor()
			->setMethods( [ 'getConnection' ] )
			->getMockForAbstractClass();

		$store->expects( $this->once() )
			->method( 'getConnection' )
			->with( $this->stringContains( 'foo' ) );

		$instance = new QuerySourceFactory(
			$store,
			[
				'bar' => AnotherFakeQueryEngine::class
			]
		);

		$this->assertInstanceOf(
			'\SMW\QueryEngine',
			$instance->get( 'bar' )
		);
	}
}

class FakeQueryEngine implements QueryEngine {

	public function getQueryResult( Query $query ) {
		return '';
	}

}

class AnotherFakeQueryEngine extends FakeQueryEngine implements StoreAware {

	public function setStore( Store $store ) {
		return $store->getConnection( 'foo' );
	}

}