Completed
Push — master ( ee70e4...91224a )
by mw
37:03
created

testPurgeCacheBySubject()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 1
b 0
f 0
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\CachedQueryResultPrefetcher;
6
use SMW\DIWikiPage;
7
use SMW\TransientStatsdCollector;
8
use Onoi\BlobStore\BlobStore;
9
10
/**
11
 * @covers \SMW\CachedQueryResultPrefetcher
12
 * @group semantic-mediawiki
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.5
16
 *
17
 * @author mwjames
18
 */
19
class CachedQueryResultPrefetcherTest extends \PHPUnit_Framework_TestCase {
20
21
	private $store;
22
	private $queryFactory;
23
	private $blobStore;
24
	private $transientStatsdCollector;
25
26
	protected function setUp() {
27
28
		$this->store = $this->getMockBuilder( '\SMW\Store' )
29
			->disableOriginalConstructor()
30
			->getMockForAbstractClass();
31
32
		$this->queryFactory = $this->getMockBuilder( '\SMW\QueryFactory' )
33
			->disableOriginalConstructor()
34
			->getMock();
35
36
		$this->blobStore = $this->getMockBuilder( BlobStore::class )
37
			->disableOriginalConstructor()
38
			->getMock();
39
40
		$this->transientStatsdCollector = $this->getMockBuilder( TransientStatsdCollector::class )
41
			->disableOriginalConstructor()
42
			->getMock();
43
	}
44
45
	public function testCanConstruct() {
46
47
		$this->assertInstanceOf(
48
			CachedQueryResultPrefetcher::class,
49
			new CachedQueryResultPrefetcher( $this->store, $this->queryFactory, $this->blobStore, $this->transientStatsdCollector )
50
		);
51
	}
52
53
	public function testGetQueryResultForEmptyQuery() {
54
55
		$query = $this->getMockBuilder( '\SMWQuery' )
56
			->disableOriginalConstructor()
57
			->getMock();
58
59
		$queryEngine = $this->getMockBuilder( '\SMW\QueryEngine' )
60
			->disableOriginalConstructor()
61
			->getMock();
62
63
		$queryEngine->expects( $this->once() )
64
			->method( 'getQueryResult' )
65
			->with($this->identicalTo( $query ) );
66
67
		$instance = new CachedQueryResultPrefetcher(
68
			$this->store,
69
			$this->queryFactory,
70
			$this->blobStore,
71
			$this->transientStatsdCollector
72
		);
73
74
		$instance->setQueryEngine( $queryEngine );
75
76
		$instance->getQueryResult( $query );
77
	}
78
79
	public function testPurgeCacheByQueryList() {
80
81
		$this->blobStore->expects( $this->atLeastOnce() )
82
			->method( 'exists' )
83
			->will( $this->returnValue( true ) );
84
85
		$this->blobStore->expects( $this->atLeastOnce() )
86
			->method( 'delete' );
87
88
		$instance = new CachedQueryResultPrefetcher(
89
			$this->store,
90
			$this->queryFactory,
91
			$this->blobStore,
92
			$this->transientStatsdCollector
93
		);
94
95
		$instance->resetCacheBy( array( 'Foo' ) );
96
	}
97
98
	public function testNoCache() {
99
100
		$this->blobStore->expects( $this->never() )
101
			->method( 'read' );
102
103
		$this->blobStore->expects( $this->atLeastOnce() )
104
			->method( 'canUse' )
105
			->will( $this->returnValue( true ) );
106
107
		$query = $this->getMockBuilder( '\SMWQuery' )
108
			->disableOriginalConstructor()
109
			->getMock();
110
111
		$query->expects( $this->atLeastOnce() )
112
			->method( 'getLimit' )
113
			->will( $this->returnValue( 100 ) );
114
115
		$query->expects( $this->atLeastOnce() )
116
			->method( 'getContextPage' )
117
			->will( $this->returnValue( DIWikiPage::newFromText( __METHOD__ ) ) );
118
119
		$query->expects( $this->atLeastOnce() )
120
			->method( 'getOptionBy' )
121
			->with( $this->equalTo( $query::NO_CACHE ) )
122
			->will( $this->returnValue( true ) );
123
124
		$queryEngine = $this->getMockBuilder( '\SMW\QueryEngine' )
125
			->disableOriginalConstructor()
126
			->getMock();
127
128
		$instance = new CachedQueryResultPrefetcher(
129
			$this->store,
130
			$this->queryFactory,
131
			$this->blobStore,
132
			$this->transientStatsdCollector
133
		);
134
135
		$instance->setQueryEngine( $queryEngine );
136
		$instance->getQueryResult( $query );
137
	}
138
139
	public function testMissingQueryEngineThrowsException() {
140
141
		$query = $this->getMockBuilder( '\SMWQuery' )
142
			->disableOriginalConstructor()
143
			->getMock();
144
145
		$instance = new CachedQueryResultPrefetcher(
146
			$this->store,
147
			$this->queryFactory,
148
			$this->blobStore,
149
			$this->transientStatsdCollector
150
		);
151
152
		$this->setExpectedException( 'RuntimeException' );
153
		$instance->getQueryResult( $query );
154
	}
155
156
	public function testPurgeCacheBySubject() {
157
158
		$subject = new DIWikiPage( 'Foo', NS_MAIN );
159
160
		$this->blobStore->expects( $this->atLeastOnce() )
161
			->method( 'exists' )
162
			->will( $this->returnValue( true ) );
163
164
		$this->blobStore->expects( $this->atLeastOnce() )
165
			->method( 'delete' )
166
			->with( $this->equalTo( '39e2606942246606c5daa2ddfec4ace8' ) );
167
168
		$this->transientStatsdCollector->expects( $this->once() )
169
			->method( 'recordStats' );
170
171
		$instance = new CachedQueryResultPrefetcher(
172
			$this->store,
173
			$this->queryFactory,
174
			$this->blobStore,
175
			$this->transientStatsdCollector
176
		);
177
178
		$instance->resetCacheBy( $subject );
179
	}
180
181
	public function testGetStats() {
182
183
		$this->transientStatsdCollector->expects( $this->once() )
184
			->method( 'getStats' )
185
			->will( $this->returnValue( array() ) );
186
187
		$instance = new CachedQueryResultPrefetcher(
188
			$this->store,
189
			$this->queryFactory,
190
			$this->blobStore,
191
			$this->transientStatsdCollector
192
		);
193
194
		$this->assertInternalType(
195
			'array',
196
			$instance->getStats()
197
		);
198
	}
199
200
}
201