summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/SQLStore/QueryEngine/HierarchyTempTableBuilderTest.php
blob: b01b114eb502b04d957db6a98d77e7e65a5965ca (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
<?php

namespace SMW\Tests\SQLStore\QueryEngine;

use SMW\SQLStore\QueryEngine\HierarchyTempTableBuilder;
use SMW\Tests\PHPUnitCompat;

/**
 * @covers \SMW\SQLStore\QueryEngine\HierarchyTempTableBuilder
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 2.3
 *
 * @author mwjames
 */
class HierarchyTempTableBuilderTest extends \PHPUnit_Framework_TestCase {

	use PHPUnitCompat;

	private $connection;
	private $temporaryTableBuilder;

	protected function setUp() {

		$this->connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' )
			->disableOriginalConstructor()
			->getMock();

		$this->temporaryTableBuilder = $this->getMockBuilder( '\SMW\SQLStore\TableBuilder\TemporaryTableBuilder' )
			->disableOriginalConstructor()
			->getMock();
	}

	public function testCanConstruct() {

		$this->assertInstanceOf(
			'\SMW\SQLStore\QueryEngine\HierarchyTempTableBuilder',
			new HierarchyTempTableBuilder( $this->connection, $this->temporaryTableBuilder )
		);
	}

	public function testGetHierarchyTableDefinitionForType() {

		$this->connection->expects( $this->once() )
			->method( 'tableName' )
			->with(
				$this->stringContains( 'bar') )
			->will( $this->returnValue( '_bar' ) );

		$instance = new HierarchyTempTableBuilder(
			$this->connection,
			$this->temporaryTableBuilder
		);

		$instance->setPropertyHierarchyTableDefinition( 'bar', 3 );

		$this->assertEquals(
			[ '_bar', 3 ],
			$instance->getHierarchyTableDefinitionForType( 'property' )
		);
	}

	public function testTryToGetHierarchyTableDefinitionForUnregisteredTypeThrowsException() {

		$instance = new HierarchyTempTableBuilder(
			$this->connection,
			$this->temporaryTableBuilder
		);

		$this->setExpectedException( 'RuntimeException' );
		$instance->getHierarchyTableDefinitionForType( 'foo' );
	}

	public function testCreateHierarchyTempTable() {

		$this->connection->expects( $this->once() )
			->method( 'tableName' )
			->with(
				$this->stringContains( 'bar') )
			->will( $this->returnValue( '_bar' ) );

		$this->connection->expects( $this->atLeastOnce() )
			->method( 'query' );

		$instance = new HierarchyTempTableBuilder(
			$this->connection,
			$this->temporaryTableBuilder
		);

		$instance->setClassHierarchyTableDefinition( 'bar', 3 );
		$instance->createHierarchyTempTableFor( 'class', 'foobar', '(42)' );

		$expected = [
			'(42)' => 'foobar'
		];

		$this->assertEquals(
			$expected,
			$instance->getHierarchyCache()
		);

		$instance->emptyHierarchyCache();

		$this->assertEmpty(
			$instance->getHierarchyCache()
		);
	}

}