Completed
Push — master ( 5f9e66...1f3475 )
by mw
123:57 queued 89:01
created

testFindAndMatch_CCAT()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\Query\Result;
4
5
use SMW\Query\Result\ResultFieldMatchFinder;
6
use SMW\DataItemFactory;
7
use SMW\DataValueFactory;
8
use SMW\Query\PrintRequest;
9
10
/**
11
 * @covers SMW\Query\Result\ResultFieldMatchFinder
12
 * @group semantic-mediawiki
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.5
16
 *
17
 * @author mwjames
18
 */
19
class ResultFieldMatchFinderTest extends \PHPUnit_Framework_TestCase {
20
21
	private $dataItemFactory;
22
	private $dataValueFactory;
23
24
	protected function setUp() {
25
		parent::setUp();
26
		$this->dataItemFactory = new DataItemFactory();
27
		$this->dataValueFactory = DataValueFactory::getInstance();
28
	}
29
30
	public function testCanConstruct() {
31
32
		$store = $this->getMockBuilder( '\SMW\Store' )
33
			->disableOriginalConstructor()
34
			->getMockForAbstractClass();
35
36
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
37
			->disableOriginalConstructor()
38
			->getMock();
39
40
		$this->assertInstanceOf(
41
			'SMW\Query\Result\ResultFieldMatchFinder',
42
			new ResultFieldMatchFinder( $store, $printRequest )
43
		);
44
	}
45
46
	public function testGetRequestOptions() {
47
48
		$store = $this->getMockBuilder( '\SMW\Store' )
49
			->disableOriginalConstructor()
50
			->getMockForAbstractClass();
51
52
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
53
			->disableOriginalConstructor()
54
			->getMock();
55
56
		$printRequest->expects( $this->any() )
57
			->method( 'getParameter' )
58
			->will( $this->returnValue( 42 ) );
59
60
		$instance = new ResultFieldMatchFinder(
61
			$store,
62
			$printRequest
63
		);
64
65
		$this->assertInstanceOf(
66
			'SMW\RequestOptions',
67
			$instance->getRequestOptions()
68
		);
69
	}
70
71
	public function testFindAndMatch_THIS() {
72
73
		$dataItem = $this->dataItemFactory->newDIWikiPage( 'Foo' );
74
75
		$store = $this->getMockBuilder( '\SMW\Store' )
76
			->disableOriginalConstructor()
77
			->getMockForAbstractClass();
78
79
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
80
			->disableOriginalConstructor()
81
			->getMock();
82
83
		$printRequest->expects( $this->any() )
84
			->method( 'isMode' )
85
			->with($this->equalTo( PrintRequest::PRINT_THIS ) )
86
			->will( $this->returnValue( true ) );
87
88
		$instance = new ResultFieldMatchFinder(
89
			$store,
90
			$printRequest
91
		);
92
93
		$this->assertEquals(
94
			array( $dataItem ),
95
			$instance->findAndMatch( $dataItem )
96
		);
97
	}
98
99
	public function testFindAndMatch_CATS() {
100
101
		$dataItem = $this->dataItemFactory->newDIWikiPage( 'Foo' );
102
		$expected = $this->dataItemFactory->newDIWikiPage( __METHOD__ );
103
104
		$store = $this->getMockBuilder( '\SMW\Store' )
105
			->disableOriginalConstructor()
106
			->getMockForAbstractClass();
107
108
		$store->expects( $this->once() )
109
			->method( 'getPropertyValues' )
110
			->with(
111
				$this->equalTo( $dataItem ),
112
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_INST' ) ) )
113
			->will( $this->returnValue( array( $expected ) ) );
114
115
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
116
			->disableOriginalConstructor()
117
			->getMock();
118
119
		$printRequest->expects( $this->at( 1 ) )
120
			->method( 'isMode' )
121
			->with($this->equalTo( PrintRequest::PRINT_CATS ) )
122
			->will( $this->returnValue( true ) );
123
124
		$instance = new ResultFieldMatchFinder(
125
			$store,
126
			$printRequest
127
		);
128
129
		$this->assertEquals(
130
			array( $expected ),
131
			$instance->findAndMatch( $dataItem )
132
		);
133
	}
134
135
	public function testFindAndMatch_CCAT() {
136
137
		$dataItem = $this->dataItemFactory->newDIWikiPage( 'Bar' );
138
		$expected = $this->dataItemFactory->newDIWikiPage( __METHOD__ );
139
140
		$store = $this->getMockBuilder( '\SMW\Store' )
141
			->disableOriginalConstructor()
142
			->getMockForAbstractClass();
143
144
		$store->expects( $this->once() )
145
			->method( 'getPropertyValues' )
146
			->with(
147
				$this->equalTo( $dataItem ),
148
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_INST' ) ) )
149
			->will( $this->returnValue( array( $expected ) ) );
150
151
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
152
			->disableOriginalConstructor()
153
			->getMock();
154
155
		$printRequest->expects( $this->at( 2 ) )
156
			->method( 'isMode' )
157
			->with($this->equalTo( PrintRequest::PRINT_CCAT ) )
158
			->will( $this->returnValue( true ) );
159
160
		$printRequest->expects( $this->once() )
161
			->method( 'getData' )
162
			->will( $this->returnValue( $expected ) );
163
164
		$instance = new ResultFieldMatchFinder(
165
			$store,
166
			$printRequest
167
		);
168
169
		$this->assertEquals(
170
			array( $this->dataItemFactory->newDIBoolean( true ) ),
171
			$instance->findAndMatch( $dataItem )
172
		);
173
	}
174
175
	public function testFindAndMatch_PROP() {
176
177
		$dataItem = $this->dataItemFactory->newDIWikiPage( 'Bar' );
178
		$expected = $this->dataItemFactory->newDIWikiPage( __METHOD__ );
179
180
		$store = $this->getMockBuilder( '\SMW\Store' )
181
			->disableOriginalConstructor()
182
			->getMockForAbstractClass();
183
184
		$store->expects( $this->once() )
185
			->method( 'getPropertyValues' )
186
			->with(
187
				$this->equalTo( $dataItem ),
188
				$this->equalTo( $this->dataItemFactory->newDIProperty( 'Prop' ) ) )
189
			->will( $this->returnValue( array( $expected ) ) );
190
191
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
192
			->disableOriginalConstructor()
193
			->getMock();
194
195
		$printRequest->expects( $this->at( 3 ) )
196
			->method( 'isMode' )
197
			->with($this->equalTo( PrintRequest::PRINT_PROP ) )
198
			->will( $this->returnValue( true ) );
199
200
		$printRequest->expects( $this->any() )
201
			->method( 'getParameter' )
202
			->will( $this->returnValue( false ) );
203
204
		$printRequest->expects( $this->once() )
205
			->method( 'getData' )
206
			->will( $this->returnValue( $this->dataValueFactory->newPropertyValueByLabel( 'Prop' ) ) );
207
208
		$instance = new ResultFieldMatchFinder(
209
			$store,
210
			$printRequest
211
		);
212
213
		$this->assertEquals(
214
			array( $expected ),
215
			$instance->findAndMatch( $dataItem )
216
		);
217
	}
218
219
}
220