summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/Elastic/QueryEngine/DescriptionInterpreters/ConceptDescriptionInterpreterTest.php
blob: cac182be26ab026145420afbe741ccbe60246169 (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
<?php

namespace SMW\Tests\Elastic\QueryEngine\DescriptionInterpreters;

use SMW\Elastic\QueryEngine\DescriptionInterpreters\ConceptDescriptionInterpreter;
use SMW\DIWikiPage;
use SMW\Query\DescriptionFactory;
use SMW\Tests\TestEnvironmentTrait;

/**
 * @covers \SMW\Elastic\QueryEngine\DescriptionInterpreters\ConceptDescriptionInterpreter
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class ConceptDescriptionInterpreterTest extends \PHPUnit_Framework_TestCase {

	private $conditionBuilder;
	private $descriptionFactory;
	private $queryParser;

	public function setUp() {

		$this->descriptionFactory = new DescriptionFactory();

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

		$parameters = $this->getMockBuilder( '\SMW\Elastic\QueryEngine\TermsLookup\Parameters' )
			->disableOriginalConstructor()
			->getMock();

		$this->termsLookup = $this->getMockBuilder( '\SMW\Elastic\QueryEngine\TermsLookup' )
			->disableOriginalConstructor()
			->getMock();

		$this->termsLookup->expects( $this->any() )
			->method( 'newParameters' )
			->will( $this->returnValue( $parameters ) );

		$this->conditionBuilder = $this->getMockBuilder( '\SMW\Elastic\QueryEngine\ConditionBuilder' )
			->disableOriginalConstructor()
			->setMethods( [ 'getTermsLookup', 'getStore', 'getID', 'interpretDescription' ] )
			->getMock();

		$this->conditionBuilder->expects( $this->any() )
			->method( 'getStore' )
			->will( $this->returnValue( $this->store ) );

		$this->conditionBuilder->expects( $this->any() )
			->method( 'getTermsLookup' )
			->will( $this->returnValue( $this->termsLookup ) );

		$this->queryParser = $this->getMockBuilder( '\SMW\Query\Parser' )
			->disableOriginalConstructor()
			->getMock();
	}

	public function testCanConstruct() {

		$this->assertInstanceOf(
			ConceptDescriptionInterpreter::class,
			new ConceptDescriptionInterpreter( $this->conditionBuilder, $this->queryParser )
		);
	}

	public function testInterpretDescription_EmptyConcept() {

		$instance = new ConceptDescriptionInterpreter(
			$this->conditionBuilder,
			$this->queryParser
		);

		$conceptDescription = $this->descriptionFactory->newConceptDescription(
			DIWikiPage::newFromText( 'Foo', SMW_NS_CONCEPT )
		);

		$this->assertEquals(
			[],
			$instance->interpretDescription( $conceptDescription )
		);
	}

	public function testInterpretDescription_AvailableConceptQuery() {

		$this->conditionBuilder->expects( $this->any() )
			->method( 'interpretDescription' )
			->will( $this->returnValue( $this->conditionBuilder->newCondition( [ 'Foo' ] ) ) );

		$description = $this->getMockBuilder( '\SMW\Query\Language\Description' )
			->disableOriginalConstructor()
			->getMock();

		$concept = $this->getMockBuilder( '\SMWDIConcept' )
			->disableOriginalConstructor()
			->getMock();

		$this->store->expects( $this->any() )
			->method( 'getPropertyValues' )
			->will( $this->returnValue( [ $concept ] ) );

		$this->queryParser->expects( $this->any() )
			->method( 'getQueryDescription' )
			->will( $this->returnValue( $description ) );

		$instance = new ConceptDescriptionInterpreter(
			$this->conditionBuilder,
			$this->queryParser
		);

		$conceptDescription = $this->descriptionFactory->newConceptDescription(
			DIWikiPage::newFromText( 'Foo', SMW_NS_CONCEPT )
		);

		$this->assertEquals(
			'{"bool":{"must":["Foo"]}}',
			$instance->interpretDescription( $conceptDescription )
		);
	}

}