QueryEngineTest::testInsertItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests;
4
5
use Ask\Language\Description\SomeProperty;
6
use Ask\Language\Description\ValueDescription;
7
use Ask\Language\Option\QueryOptions;
8
use DataValues\NumberValue;
9
use Doctrine\DBAL\DriverManager;
10
use PHPUnit\Framework\TestCase;
11
use Tests\Queryr\Replicator\Fixtures\StubPropertyDataValueTypeLookup;
12
use Wikibase\DataModel\Entity\BasicEntityIdParser;
13
use Wikibase\DataModel\Entity\EntityIdValue;
14
use Wikibase\DataModel\Entity\Item;
15
use Wikibase\DataModel\Entity\ItemId;
16
use Wikibase\DataModel\Entity\PropertyId;
17
use Wikibase\DataModel\Snak\PropertyValueSnak;
18
use Wikibase\DataModel\Statement\Statement;
19
use Wikibase\QueryEngine\DescriptionMatchFinder;
20
use Wikibase\QueryEngine\QueryStoreWriter;
21
use Wikibase\QueryEngine\SQLStore\DataValueHandlersBuilder;
22
use Wikibase\QueryEngine\SQLStore\SQLStore;
23
use Wikibase\QueryEngine\SQLStore\StoreConfig;
24
use Wikibase\QueryEngine\SQLStore\StoreSchema;
25
26
/**
27
 * @licence GNU GPL v2+
28
 * @author Jeroen De Dauw < [email protected] >
29
 */
30
class QueryEngineTest extends TestCase {
31
32
	/**
33
	 * @var QueryStoreWriter
34
	 */
35
	private $writer;
36
37
	/**
38
	 * @var DescriptionMatchFinder
39
	 */
40
	private $queryEngine;
41
42
	public function setUp() {
43
		if ( !defined( 'WIKIBASE_QUERYENGINE_VERSION' ) ) {
44
			$this->markTestSkipped( 'QueryEngine not installed' );
45
			return;
46
		}
47
48
		$sqlStore = new SQLStore( $this->newStoreSchema(), $this->newStoreConfig() );
49
50
		$connection = $this->newConnection();
51
52
		$sqlStore->newInstaller( $connection->getSchemaManager() )->install();
53
54
		$this->writer = $sqlStore->newWriter( $connection );
55
56
		$this->queryEngine = $sqlStore->newDescriptionMatchFinder(
57
			$connection,
58
			new StubPropertyDataValueTypeLookup(),
59
			new BasicEntityIdParser()
60
		);
61
	}
62
63
	private function newConnection() {
64
		return DriverManager::getConnection( array(
65
			'driver' => 'pdo_sqlite',
66
			'memory' => true,
67
		) );
68
	}
69
70
	private function newStoreSchema() {
71
		$handlersBuilder = new DataValueHandlersBuilder();
72
73
		return new StoreSchema(
74
			'qe_',
75
			$handlersBuilder->withSimpleMainSnakHandlers()->getHandlers()
76
		);
77
	}
78
79
	private function newStoreConfig() {
80
		$config = new StoreConfig(
81
			'QueryR Replicator QueryEngine'
82
		);
83
84
		return $config;
85
	}
86
87
	public function testInsertItem() {
88
		$item = new Item();
89
		$item->setId( new ItemId( 'Q100' ) );
90
91
		$statement = new Statement( new PropertyValueSnak( 42, new NumberValue( 1337 ) ) );
92
		$statement->setGuid( 'foo claim' );
93
		$item->getStatements()->addStatement( $statement );
94
95
		$this->writer->insertEntity( $item );
96
97
		$propertyDescription = new SomeProperty(
98
			new EntityIdValue( new PropertyId( 'P42' ) ),
99
			new ValueDescription( new NumberValue( 1337 ) )
100
		);
101
102
		$ids = $this->queryEngine->getMatchingEntities(
103
			$propertyDescription,
104
			new QueryOptions( 2, 0 )
105
		);
106
107
		$this->assertEquals( array( 'Q100' ), $ids );
108
	}
109
110
}
111