Completed
Push — master ( 1a5213...02ed40 )
by Stephan
20:02
created

testInvalidateOnUpdateWithNullSemanticData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SG\Tests\Cache;
4
5
use SG\PropertyRegistrationHelper;
6
use SG\Cache\CacheInvalidator;
7
use SG\Cache\GlossaryCache;
8
9
use SMW\Subobject;
10
use SMW\SemanticData;
11
use SMW\DIWikiPage;
12
use SMW\DIProperty;
13
use SMWDIBlob as DIBlob;
14
15
use HashBagOStuff;
16
use Title;
17
18
/**
19
 * @covers \SG\Cache\CacheInvalidator
20
 *
21
 * @ingroup Test
22
 *
23
 * @group SG
24
 * @group SGExtension
25
 * @group extension-semantic-glossary
26
 *
27
 * @license GNU GPL v2+
28
 * @since 1.0
29
 *
30
 * @author mwjames
31
 */
32
class CacheInvalidatorTest extends \PHPUnit_Framework_TestCase {
33
34
	public function testCanConstruct() {
35
		CacheInvalidator::clear();
36
37
		$this->assertInstanceOf(
38
			'\SG\Cache\CacheInvalidator',
39
			CacheInvalidator::getInstance()
40
		);
41
	}
42
43
	public function testInvalidateOnUpdateWithEmptyData() {
44
45
		$store = $this->getMockBuilder( '\SMW\Store' )
46
			->disableOriginalConstructor()
47
			->getMockForAbstractClass();
48
49
		$store->method( 'getPropertyValues' )
50
			->willReturn( [] );
51
52
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
53
			->disableOriginalConstructor()
54
			->getMockForAbstractClass();
55
56
		$instance = new CacheInvalidator();
57
		$instance->setCache( new GlossaryCache( new HashBagOStuff() ) );
58
59
		$this->assertTrue( $instance->invalidateCacheOnStoreUpdate( $store, $semanticData ) );
60
	}
61
62
	public function testInvalidateOnUpdateWithNullSemanticData() {
63
64
		$store = $this->getMockBuilder( '\SMW\Store' )
65
			->disableOriginalConstructor()
66
			->getMockForAbstractClass();
67
68
		$instance = new CacheInvalidator();
69
		$instance->setCache( new GlossaryCache( new HashBagOStuff() ) );
70
71
		$this->assertFalse(
72
			$instance->invalidateCacheOnStoreUpdate( $store, null )
73
		);
74
	}
75
76
	public function testInvalidateOnUpdateWithDifferentSubobjectData() {
77
78
		$subject = DIWikiPage::newFromTitle( Title::newFromText( __METHOD__ ) );
79
80
		$subobject = new Subobject( $subject->getTitle() );
0 ignored issues
show
Bug introduced by
It seems like $subject->getTitle() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
81
		$subobject->setSemanticData( '_999999' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\Subobject::setSemanticData() has been deprecated with message: since 2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
83
		$subobject->getSemanticData()->addPropertyObjectValue(
84
			new DIProperty( PropertyRegistrationHelper::SG_TERM ),
85
			new DIBlob( 'Foo' )
86
		);
87
88
		$subobject->getSemanticData()->addPropertyObjectValue(
89
			new DIProperty( PropertyRegistrationHelper::SG_DEFINITION ),
90
			new DIBlob( 'Bar' )
91
		);
92
93
		$store = $this->getMockBuilder( 'SMWStore' )
94
			->disableOriginalConstructor()
95
			->getMockForAbstractClass();
96
97
		$store->method( 'getPropertyValues' )
98
			->willReturn( [] );
99
100
		$semanticData = new SemanticData( $subject );
101
		$semanticData->addPropertyObjectValue(
102
			$subobject->getProperty(),
103
			$subobject->getContainer()
104
		);
105
106
		$glossaryCache = new GlossaryCache( new HashBagOStuff() );
107
108
		$itemId = $glossaryCache->getKeyForSubject(
109
			$subobject->getSemanticData()->getSubject()
110
		);
111
112
		$glossaryCache->getCache()->set( $itemId, 'preset.cacheitem' );
113
114
		$instance = new CacheInvalidator();
115
		$instance->setCache( $glossaryCache );
116
117
		$this->assertTrue( $instance->invalidateCacheOnStoreUpdate( $store, $semanticData ) );
118
119
		$this->assertFalse(
120
			$glossaryCache->getCache()->get( $itemId ),
121
			'Asserts that the preset item has been removed from cache'
122
		);
123
	}
124
125
	public function testInvalidateOnDeleteWithEmptyData() {
126
127
		$subject = DIWikiPage::newFromTitle( Title::newFromText( __METHOD__ ) );
128
129
		$store = $this->getMockBuilder( 'SMWStore' )
130
			->disableOriginalConstructor()
131
			->getMockForAbstractClass();
132
133
		$store->expects( $this->once() )
134
			->method( 'getProperties' )
135
			->with( $this->equalTo( $subject ) )
136
			->will( $this->returnValue( array() ) );
137
138
		$instance = new CacheInvalidator();
139
		$instance->setCache( new GlossaryCache( new HashBagOStuff() ) );
140
141
		$this->assertTrue( $instance->invalidateCacheOnPageDelete( $store, $subject ) );
142
	}
143
144
	public function testInvalidateOnDeleteWithSubobject() {
145
146
		$subobject  = new DIProperty( '_SOBJ' );
147
		$subject    = DIWikiPage::newFromTitle( Title::newFromText( __METHOD__ ) );
148
		$newSubject = DIWikiPage::newFromTitle( Title::newFromText( 'Subobject' ) );
149
150
		$store = $this->getMockBuilder( 'SMWStore' )
151
			->disableOriginalConstructor()
152
			->getMockForAbstractClass();
153
154
		$store->expects( $this->once() )
155
			->method( 'getProperties' )
156
			->with( $this->equalTo( $subject ) )
157
			->will( $this->returnValue( array( '_SOBJ' => $subobject ) ) );
158
159
		$store->expects( $this->once() )
160
			->method( 'getPropertyValues' )
161
			->with(
162
				$this->equalTo( $subject ),
163
				$this->equalTo( $subobject ) )
164
			->will( $this->returnValue( $newSubject ) );
165
166
		$glossaryCache = new GlossaryCache( new HashBagOStuff() );
167
168
		$itemId = $glossaryCache->getKeyForSubject( $subject );
169
170
		$glossaryCache->getCache()->set( $itemId, 'preset.cacheitem' );
171
172
		$instance = new CacheInvalidator();
173
		$instance->setCache( $glossaryCache );
174
175
		$this->assertTrue( $instance->invalidateCacheOnPageDelete( $store, $subject ) );
176
177
		$this->assertFalse(
178
			$glossaryCache->getCache()->get( $itemId ),
179
			'Asserts that the preset item has been removed from cache'
180
		);
181
	}
182
183
	public function testInvalidateOnMove() {
184
185
		$title = Title::newFromText( __METHOD__ );
186
187
		$instance = new CacheInvalidator();
188
		$instance->setCache( new GlossaryCache( new HashBagOStuff() ) );
189
190
		$this->assertTrue( $instance->invalidateCacheOnPageMove( $title ) );
191
	}
192
193
}
194