Passed
Pull Request — master (#136)
by None
03:40
created

ReferenceBacklinksLookupTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 169
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SCI\Tests;
4
5
use SCI\ReferenceBacklinksLookup;
6
use SCI\PropertyRegistry;
7
use SMW\DIWikiPage;
8
use SMW\DIProperty;
9
use SMWDIBlob as DIBlob;
10
use SMW\Tests\PHPUnitCompat;
11
12
/**
13
 * @covers \SCI\ReferenceBacklinksLookup
14
 * @group semantic-cite
15
 *
16
 * @license GNU GPL v2+
17
 * @since   1.0
18
 *
19
 * @author mwjames
20
 */
21
class ReferenceBacklinksLookupTest extends \PHPUnit_Framework_TestCase {
22
23
	use PHPUnitCompat;
24
25
	public function testCanConstruct() {
26
27
		$store = $this->getMockBuilder( '\SMW\Store' )
28
			->disableOriginalConstructor()
29
			->getMockForAbstractClass();
30
31
		$this->assertInstanceOf(
32
			'\SCI\ReferenceBacklinksLookup',
33
			new ReferenceBacklinksLookup( $store )
34
		);
35
	}
36
37
	public function testtryToFindCitationKeyFor() {
38
39
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
40
			->disableOriginalConstructor()
41
			->getMock();
42
43
		$semanticData->expects( $this->once() )
44
			->method( 'getPropertyValues' )
45
			->with( $this->equalTo( new DIProperty( PropertyRegistry::SCI_CITE_KEY ) ) )
46
			->will( $this->returnValue( [ 'Foo', 'Bar' ] ) );
47
48
		$store = $this->getMockBuilder( '\SMW\Store' )
49
			->disableOriginalConstructor()
50
			->getMockForAbstractClass();
51
52
		$store->expects( $this->once() )
53
			->method( 'getSemanticData' )
54
			->will( $this->returnValue( $semanticData ) );
55
56
		$instance = new ReferenceBacklinksLookup( $store );
57
		$instance->setStore( $store );
58
59
		$this->assertEquals(
60
			'Bar',
61
			$instance->tryToFindCitationKeyFor( DIWikiPage::newFromText( __METHOD__ ) )
62
		);
63
	}
64
65
	public function testTryToAddReferenceBacklinksForNoKeys() {
66
67
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
68
			->disableOriginalConstructor()
69
			->getMock();
70
71
		$semanticData->expects( $this->once() )
72
			->method( 'getSubject' )
73
			->will( $this->returnValue( DIWikiPage::newFromText( __METHOD__ ) ) );
74
75
		$semanticData->expects( $this->once() )
76
			->method( 'getPropertyValues' )
77
			->with( $this->equalTo( new DIProperty( PropertyRegistry::SCI_CITE_KEY ) ) )
78
			->will( $this->returnValue( [] ) );
79
80
		$semanticData->expects( $this->never() )
81
			->method( 'addPropertyObjectValue' );
82
83
		$store = $this->getMockBuilder( '\SMW\Store' )
84
			->disableOriginalConstructor()
85
			->getMockForAbstractClass();
86
87
		$store->expects( $this->once() )
88
			->method( 'getSemanticData' )
89
			->will( $this->returnValue( $semanticData ) );
90
91
		$requestOptions = new \stdClass;
92
		$requestOptions->limit = 5;
93
		$requestOptions->offset = 0;
94
95
		$instance = new ReferenceBacklinksLookup( $store );
96
		$instance->setRequestOptions( $requestOptions );
97
98
		$instance->addReferenceBacklinksTo(
99
			$semanticData
100
		);
101
	}
102
103
	public function testAddReferenceBacklinks() {
104
105
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
106
			->disableOriginalConstructor()
107
			->getMock();
108
109
		$semanticData->expects( $this->once() )
110
			->method( 'getSubject' )
111
			->will( $this->returnValue( DIWikiPage::newFromText( __METHOD__ ) ) );
112
113
		$semanticData->expects( $this->once() )
114
			->method( 'getPropertyValues' )
115
			->will( $this->returnValue( [ DIWikiPage::newFromText( 'Bar' ) ] ) );
116
117
		$semanticData->expects( $this->atLeastOnce() )
118
			->method( 'addPropertyObjectValue' )
119
			->with(
120
					$this->equalTo( new DIProperty( PropertyRegistry::SCI_CITE_REFERENCE ) ),
121
					$this->anything() );
122
123
		$queryResult = $this->getMockBuilder( '\SMWQueryResult' )
124
			->disableOriginalConstructor()
125
			->getMock();
126
127
		$queryResult->expects( $this->once() )
128
			->method( 'getResults' )
129
			->will( $this->returnValue( [ DIWikiPage::newFromText( 'Foo' ) ] ) );
130
131
		$store = $this->getMockBuilder( '\SMW\Store' )
132
			->disableOriginalConstructor()
133
			->getMockForAbstractClass();
134
135
		$store->expects( $this->once() )
136
			->method( 'getQueryResult' )
137
			->will( $this->returnValue( $queryResult ) );
138
139
		$store->expects( $this->once() )
140
			->method( 'getSemanticData' )
141
			->will( $this->returnValue( $semanticData ) );
142
143
		$instance = new ReferenceBacklinksLookup( $store );
144
145
		$instance->addReferenceBacklinksTo(
146
			$semanticData
147
		);
148
	}
149
150
	public function testGetSpecialPropertySearchFurtherLink() {
151
152
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
153
			->disableOriginalConstructor()
154
			->getMock();
155
156
		$semanticData->expects( $this->once() )
157
			->method( 'getPropertyValues' )
158
			->will( $this->returnValue( [ new DIBlob( 'Bar' ) ] ) );
159
160
		$store = $this->getMockBuilder( '\SMW\Store' )
161
			->disableOriginalConstructor()
162
			->getMockForAbstractClass();
163
164
		$store->expects( $this->once() )
165
			->method( 'getSemanticData' )
166
			->will( $this->returnValue( $semanticData ) );
167
168
		$property = new DIProperty( PropertyRegistry::SCI_CITE_REFERENCE );
169
		$subject = DIWikiPage::newFromText( __METHOD__ );
170
171
		$instance = new ReferenceBacklinksLookup( $store );
172
173
		$result = $instance->getSpecialPropertySearchFurtherLink(
174
			$property,
175
			$subject,
176
			$html
177
		);
178
179
		$this->assertFalse(
180
			$result
181
		);
182
183
		$this->assertContains(
184
			'SearchByProperty',
185
			$html
186
		);
187
	}
188
189
}
190