Completed
Push — master ( d52f9e...94eed6 )
by Stephan
04:21 queued 02:45
created

testInvalidateOnUpdateWithDifferentSubobjectData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
cc 1
eloc 29
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 testInvalidateOnUpdateWithDifferentSubobjectData() {
63
64
		$subject = DIWikiPage::newFromTitle( Title::newFromText( __METHOD__ ) );
65
66
		$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...
67
		$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...
68
69
		$subobject->getSemanticData()->addPropertyObjectValue(
70
			new DIProperty( PropertyRegistrationHelper::SG_TERM ),
71
			new DIBlob( 'Foo' )
72
		);
73
74
		$subobject->getSemanticData()->addPropertyObjectValue(
75
			new DIProperty( PropertyRegistrationHelper::SG_DEFINITION ),
76
			new DIBlob( 'Bar' )
77
		);
78
79
		$store = $this->getMockBuilder( 'SMWStore' )
80
			->disableOriginalConstructor()
81
			->getMockForAbstractClass();
82
83
		$store->method( 'getPropertyValues' )
84
			->willReturn( [] );
85
86
		$semanticData = new SemanticData( $subject );
87
		$semanticData->addPropertyObjectValue(
88
			$subobject->getProperty(),
89
			$subobject->getContainer()
90
		);
91
92
		$glossaryCache = new GlossaryCache( new HashBagOStuff() );
93
94
		$itemId = $glossaryCache->getKeyForSubject(
95
			$subobject->getSemanticData()->getSubject()
96
		);
97
98
		$glossaryCache->getCache()->set( $itemId, 'preset.cacheitem' );
99
100
		$instance = new CacheInvalidator();
101
		$instance->setCache( $glossaryCache );
102
103
		$this->assertTrue( $instance->invalidateCacheOnStoreUpdate( $store, $semanticData ) );
104
105
		$this->assertFalse(
106
			$glossaryCache->getCache()->get( $itemId ),
107
			'Asserts that the preset item has been removed from cache'
108
		);
109
	}
110
111
	public function testInvalidateOnDeleteWithEmptyData() {
112
113
		$subject = DIWikiPage::newFromTitle( Title::newFromText( __METHOD__ ) );
114
115
		$store = $this->getMockBuilder( 'SMWStore' )
116
			->disableOriginalConstructor()
117
			->getMockForAbstractClass();
118
119
		$store->expects( $this->once() )
120
			->method( 'getProperties' )
121
			->with( $this->equalTo( $subject ) )
122
			->will( $this->returnValue( array() ) );
123
124
		$instance = new CacheInvalidator();
125
		$instance->setCache( new GlossaryCache( new HashBagOStuff() ) );
126
127
		$this->assertTrue( $instance->invalidateCacheOnPageDelete( $store, $subject ) );
128
	}
129
130
	public function testInvalidateOnDeleteWithSubobject() {
131
132
		$subobject  = new DIProperty( '_SOBJ' );
133
		$subject    = DIWikiPage::newFromTitle( Title::newFromText( __METHOD__ ) );
134
		$newSubject = DIWikiPage::newFromTitle( Title::newFromText( 'Subobject' ) );
135
136
		$store = $this->getMockBuilder( 'SMWStore' )
137
			->disableOriginalConstructor()
138
			->getMockForAbstractClass();
139
140
		$store->expects( $this->once() )
141
			->method( 'getProperties' )
142
			->with( $this->equalTo( $subject ) )
143
			->will( $this->returnValue( array( '_SOBJ' => $subobject ) ) );
144
145
		$store->expects( $this->once() )
146
			->method( 'getPropertyValues' )
147
			->with(
148
				$this->equalTo( $subject ),
149
				$this->equalTo( $subobject ) )
150
			->will( $this->returnValue( $newSubject ) );
151
152
		$glossaryCache = new GlossaryCache( new HashBagOStuff() );
153
154
		$itemId = $glossaryCache->getKeyForSubject( $subject );
155
156
		$glossaryCache->getCache()->set( $itemId, 'preset.cacheitem' );
157
158
		$instance = new CacheInvalidator();
159
		$instance->setCache( $glossaryCache );
160
161
		$this->assertTrue( $instance->invalidateCacheOnPageDelete( $store, $subject ) );
162
163
		$this->assertFalse(
164
			$glossaryCache->getCache()->get( $itemId ),
165
			'Asserts that the preset item has been removed from cache'
166
		);
167
	}
168
169
	public function testInvalidateOnMove() {
170
171
		$title = Title::newFromText( __METHOD__ );
172
173
		$instance = new CacheInvalidator();
174
		$instance->setCache( new GlossaryCache( new HashBagOStuff() ) );
175
176
		$this->assertTrue( $instance->invalidateCacheOnPageMove( $title ) );
177
	}
178
179
}
180