MongoDBTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 16
Bugs 0 Features 6
Metric Value
wmc 5
c 16
b 0
f 6
lcom 1
cbo 15
dl 0
loc 84
rs 9.1666

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntityStoreFromConfiguration() 0 4 1
A testMongoDbStore() 0 60 2
A importCommand() 0 10 1
A setupMongoDb() 0 4 1
1
<?php
2
3
namespace Wikibase\EntityStore;
4
5
use Ask\Language\Description\AnyValue;
6
use Ask\Language\Description\SomeProperty;
7
use Ask\Language\Description\ValueDescription;
8
use Ask\Language\Option\QueryOptions;
9
use DataValues\StringValue;
10
use MongoConnectionException;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Output\NullOutput;
13
use Wikibase\DataModel\Entity\EntityIdValue;
14
use Wikibase\DataModel\Entity\ItemId;
15
use Wikibase\DataModel\Entity\PropertyId;
16
use Wikibase\DataModel\Term\Term;
17
use Wikibase\EntityStore\Config\EntityStoreFromConfigurationBuilder;
18
use Wikibase\EntityStore\Console\CliApplicationFactory;
19
20
/**
21
 * @licence GNU GPL v2+
22
 * @author Thomas Pellissier Tanon
23
 */
24
class MongoDBTest extends \PHPUnit_Framework_TestCase {
25
26
	public function testMongoDbStore() {
27
		try {
28
			$this->setupMongoDb();
29
		} catch( MongoConnectionException $e ) {
30
			$this->markTestSkipped( 'MongoDB is not running: ' . $e->getMessage() );
31
			return;
32
		}
33
34
		$store = $this->getEntityStoreFromConfiguration();
35
36
		$this->assertEquals(
37
			new ItemId( 'Q1' ),
38
			$store->getItemLookup()->getItemForId( new ItemId( 'Q1' ) )->getId()
39
		);
40
41
		$this->assertEquals(
42
			new PropertyId( 'P1' ),
43
			$store->getPropertyLookup()->getPropertyForId( new PropertyId( 'P1' ) )->getId()
44
		);
45
46
		$results = $store->getEntityDocumentLookup()->getEntityDocumentsForIds(
47
			[ new ItemId( 'Q1' ), new ItemId( 'Q1000' ) ]
48
		);
49
		$this->assertEquals( 1, count( $results ) );
50
		$this->assertEquals( new ItemId( 'Q1' ), $results[0]->getId() );
51
52
		$this->assertEquals(
53
			[ new ItemId( 'Q1' ) ],
54
			$store->getItemIdForTermLookup()->getItemIdsForTerm( new Term( 'en', 'universe' ) )
55
		);
56
57
		$this->assertEquals(
58
			[],
59
			$store->getItemIdForTermLookup()->getItemIdsForTerm( new Term( 'pl', 'Kosmos' ) )
60
		);
61
62
		$this->assertEquals(
63
			[ new PropertyId( 'P16' ) ],
64
			$store->getPropertyIdForTermLookup()->getPropertyIdsForTerm( new Term( 'en', 'highway system' ) )
65
		);
66
67
		$this->assertEquals(
68
			[ new PropertyId( 'P16' ) ],
69
			$store->getPropertyIdForQueryLookup()->getPropertyIdsForQuery(
70
				new AnyValue(),
71
				new QueryOptions( 1, 0 )
72
			)
73
		);
74
75
		$this->assertEquals(
76
			[ new ItemId( 'Q1' ) ],
77
			$store->getItemIdForQueryLookup()->getItemIdsForQuery(
78
				new SomeProperty(
79
					new EntityIdValue( new PropertyId( 'P18' ) ),
80
					new ValueDescription( new StringValue( 'Hubble ultra deep field.jpg' ) )
81
				),
82
				new QueryOptions( 10, 0 )
83
			)
84
		);
85
	}
86
87
	private function setupMongoDb() {
88
		$this->importCommand( 'import-json-dump', 'valid.json' );
89
		$this->importCommand( 'import-incremental-xml-dump', 'valid-incremental.xml' );
90
	}
91
92
	private function importCommand( $command, $file ) {
93
		$applicationFactory = new CliApplicationFactory();
94
		$importCommand = $applicationFactory->newApplication()->find( $command );
95
		$input = new ArrayInput( [
96
			'command' => $command,
97
			'file' => __DIR__ . '/../data/' . $file,
98
			'configuration' => __DIR__ . '/../data/valid-config-mongodb.json'
99
		] );
100
		$importCommand->run( $input, new NullOutput() );
101
	}
102
103
	private function getEntityStoreFromConfiguration() {
104
		$configBuilder = new EntityStoreFromConfigurationBuilder();
105
		return $configBuilder->buildEntityStore( __DIR__ . '/../data/valid-config-mongodb.json' );
106
	}
107
}
108