Completed
Push — master ( d93f11...86f8d1 )
by Jeroen De
03:37
created

testGetEntity_exception()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
c 2
b 0
f 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\Lookup;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Services\Lookup\EntityLookup;
8
use Wikibase\DataModel\Services\Lookup\RestrictedEntityLookup;
9
10
/**
11
 * @covers Wikibase\DataModel\Services\Lookup\RestrictedEntityLookup
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Marius Hoch
15
 */
16
class RestrictedEntityLookupTest extends \PHPUnit_Framework_TestCase {
17
18
	/**
19
	 * @return \PHPUnit_Framework_MockObject_MockObject|EntityLookup
20
	 */
21
	private function getEntityLookup() {
22
		$entityLookup = $this->getMock( 'Wikibase\DataModel\Services\Lookup\EntityLookup' );
23
24
		$entityLookup->expects( $this->any() )
25
			->method( 'hasEntity' )
26
			->will( $this->returnValue( true ) );
27
28
		$entityLookup->expects( $this->any() )
29
			->method( 'getEntity' )
30
			->will( $this->returnCallback( function( EntityId $id ) {
31
				return $id->getSerialization();
32
			} ) );
33
34
		return $entityLookup;
35
	}
36
37
	public function testConstructor() {
38
		$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 1 );
39
		$this->assertInstanceOf(
40
			'Wikibase\DataModel\Services\Lookup\RestrictedEntityLookup',
41
			$lookup
42
		);
43
	}
44
45
	/**
46
	 * @expectedException \InvalidArgumentException
47
	 */
48
	public function testConstructor_exception() {
49
		new RestrictedEntityLookup( $this->getEntityLookup(), 0 );
50
	}
51
52
	public function testHasEntity() {
53
		$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 );
54
55
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q22' ) ) );
56
	}
57
58
	public function testGetEntityAccessCount() {
59
		$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 );
60
61
		for ( $i = 1; $i < 6; $i++ ) {
62
			$lookup->getEntity( new ItemId( 'Q' . $i ) );
63
		}
64
		// Q3 has already been loaded, thus doesn't count
65
		$lookup->getEntity( new ItemId( 'Q3' ) );
66
67
		$this->assertSame( 5, $lookup->getEntityAccessCount() );
68
	}
69
70
	public function testResetEntityAccessCount() {
71
		$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 );
72
		$lookup->getEntity( new ItemId( 'Q1' ) );
73
74
		$lookup->resetEntityAccessCount();
75
76
		$lookup->getEntity( new ItemId( 'Q2' ) );
77
		$this->assertSame( 1, $lookup->getEntityAccessCount() );
78
	}
79
80
	public function testGetEntity() {
81
		$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 );
82
83
		for ( $i = 1; $i < 6; $i++ ) {
84
			$this->assertSame(
85
				'Q' . $i,
86
				$lookup->getEntity( new ItemId( 'Q' . $i ) )
87
			);
88
		}
89
	}
90
91
	public function testGetEntity_exception() {
92
		$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 3 );
93
94
		$lookup->getEntity( new ItemId( 'Q1' ) );
95
		$lookup->getEntity( new ItemId( 'Q2' ) );
96
		$lookup->getEntity( new ItemId( 'Q3' ) );
97
98
		$this->setExpectedException( '\Wikibase\DataModel\Services\Lookup\EntityAccessLimitException' );
99
		$lookup->getEntity( new ItemId( 'Q4' ) );
100
	}
101
102
	public function testHasEntityBeenAccessed() {
103
		$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 );
104
		$lookup->getEntity( new ItemId( 'Q2' ) );
105
106
		$this->assertTrue( $lookup->entityHasBeenAccessed( new ItemId( 'Q2' ) ) );
107
		$this->assertFalse( $lookup->entityHasBeenAccessed( new ItemId( 'Q42' ) ) );
108
	}
109
110
}
111