testGetPropertysForTermWithCacheHit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\EntityStore\Cache;
4
5
use OutOfBoundsException;
6
use Wikibase\DataModel\Entity\PropertyId;
7
use Wikibase\DataModel\Term\Term;
8
9
/**
10
 * @covers Wikibase\EntityStore\Cache\CachedPropertyIdForTermLookup
11
 *
12
 * @licence GPLv2+
13
 * @author Thomas Pellissier Tanon
14
 */
15
class CachedPropertyIdForTermLookupTest extends \PHPUnit_Framework_TestCase {
16
17
	public function testGetPropertysForTermWithCacheHit() {
18
		$propertyIds = [ new PropertyId( 'P1' ) ];
19
20
		$propertyForTermLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\PropertyIdForTermLookup' )
21
			->disableOriginalConstructor()
22
			->getMock();
23
24
		$entityIdForTermCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityIdForTermCache' )
25
			->disableOriginalConstructor()
26
			->getMock();
27
		$entityIdForTermCacheMock->expects( $this->once() )
28
			->method( 'fetch' )
29
			->with( $this->equalTo( new Term( 'en', 'foo' ) ), $this->equalTo( 'property' ) )
30
			->willReturn( $propertyIds );
31
32
		$propertyIdForTermLookup = new CachedPropertyIdForTermLookup( $propertyForTermLookupMock, $entityIdForTermCacheMock );
33
		$this->assertEquals(
34
			$propertyIds,
35
			$propertyIdForTermLookup->getPropertyIdsForTerm( new Term( 'en', 'foo' ) )
36
		);
37
	}
38
39
	public function testGetPropertysForTermWithCacheMiss() {
40
		$propertyIds = [ new PropertyId( 'P1' ) ];
41
42
		$propertyIdForTermLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\PropertyIdForTermLookup' )
43
			->disableOriginalConstructor()
44
			->getMock();
45
		$propertyIdForTermLookupMock->expects( $this->once() )
46
			->method( 'getPropertyIdsForTerm' )
47
			->with( $this->equalTo( new Term( 'en', 'foo' ) ) )
48
			->willReturn( $propertyIds );
49
50
		$entityIdForTermCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityIdForTermCache' )
51
			->disableOriginalConstructor()
52
			->getMock();
53
		$entityIdForTermCacheMock->expects( $this->once() )
54
			->method( 'fetch' )
55
			->with( $this->equalTo( new Term( 'en', 'foo' ) ), $this->equalTo( 'property' ) )
56
			->willThrowException( new OutOfBoundsException() );
57
58
		$propertyIdForTermLookup = new CachedPropertyIdForTermLookup( $propertyIdForTermLookupMock, $entityIdForTermCacheMock );
59
		$this->assertEquals(
60
			$propertyIds,
61
			$propertyIdForTermLookup->getPropertyIdsForTerm( new Term( 'en', 'foo' ) )
62
		);
63
	}
64
}
65