1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SRF\Test; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use SMW\Test\QueryPrinterRegistryTestCase; |
7
|
|
|
use SMW\Tests\Utils\Mock\CoreMockObjectRepository; |
8
|
|
|
use SMW\Tests\Utils\Mock\MockObjectBuilder; |
9
|
|
|
use SMWQueryProcessor; |
10
|
|
|
use SRF\Formats\Tree\TreeResultPrinter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class TreeTest |
14
|
|
|
* |
15
|
|
|
* @since 2.5 |
16
|
|
|
* |
17
|
|
|
* @ingroup SemanticResultFormats |
18
|
|
|
* @ingroup Test |
19
|
|
|
* |
20
|
|
|
* @group SRF |
21
|
|
|
* @group SMWExtension |
22
|
|
|
* @group ResultPrinters |
23
|
|
|
* |
24
|
|
|
* @author Stephan Gambke |
25
|
|
|
*/ |
26
|
|
|
class TreeTest extends QueryPrinterRegistryTestCase { |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the names of the formats being tested. |
30
|
|
|
* @return string[] |
31
|
|
|
*/ |
32
|
|
|
public function getFormats() { |
33
|
|
|
return [ 'tree' ]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns the name of the class being tested. |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getClass() { |
41
|
|
|
return '\SRF\Formats\Tree\TreeResultPrinter'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @covers \SRF\Formats\Tree\TreeResultPrinter::getResult() |
46
|
|
|
*/ |
47
|
|
|
public function testGetResult_NoParentProperty() { |
48
|
|
|
|
49
|
|
|
$this->prepareGlobalState(); |
50
|
|
|
|
51
|
|
|
$mockBuilder = new MockObjectBuilder(); |
52
|
|
|
$mockBuilder->registerRepository( new CoreMockObjectRepository() ); |
53
|
|
|
|
54
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject $queryResult */ |
55
|
|
|
$queryResult = $mockBuilder->newObject( 'QueryResult', [ 'getCount' => 1 ] ); |
56
|
|
|
|
57
|
|
|
$queryResult->expects( $this->once() ) |
58
|
|
|
->method( 'addErrors' ) |
59
|
|
|
->will( $this->returnValue( null ) ); |
60
|
|
|
|
61
|
|
|
$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] ); |
62
|
|
|
|
63
|
|
|
$testObject = new TreeResultPrinter( 'tree' ); |
64
|
|
|
|
65
|
|
|
$this->assertEquals( '', $testObject->getResult( $queryResult, $params, SMW_OUTPUT_HTML ), 'Result should be empty.' ); |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function prepareGlobalState() { |
|
|
|
|
71
|
|
|
|
72
|
|
|
$parserOutput = $this->getMockBuilder( '\ParserOutput' ) |
73
|
|
|
->disableOriginalConstructor() |
74
|
|
|
->getMock(); |
75
|
|
|
|
76
|
|
|
$parserOutput->expects( $this->any() ) |
77
|
|
|
->method( 'getHeadItems' ) |
78
|
|
|
->will( $this->returnValue( [] ) ); |
79
|
|
|
|
80
|
|
|
$parser = $this->getMockBuilder( '\Parser' ) |
81
|
|
|
->disableOriginalConstructor() |
82
|
|
|
->getMock(); |
83
|
|
|
|
84
|
|
|
$parser->expects( $this->any() ) |
85
|
|
|
->method( 'parse' ) |
86
|
|
|
->will( $this->returnValue( $parserOutput ) ); |
87
|
|
|
|
88
|
|
|
$title = $this->getMockBuilder( '\Title' ) |
89
|
|
|
->disableOriginalConstructor() |
90
|
|
|
->getMock(); |
91
|
|
|
|
92
|
|
|
$GLOBALS[ 'wgParser' ] = $parser; |
93
|
|
|
$GLOBALS[ 'wgTitle' ] = $title; |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
protected function provideQueryParamsAndResults() { |
101
|
|
|
$mockBuilder = new MockObjectBuilder(); |
102
|
|
|
$mockBuilder->registerRepository( new CoreMockObjectRepository() ); |
103
|
|
|
|
104
|
|
|
/** @var SMWResultArray $resultItem */ |
105
|
|
|
$resultCell = null; |
|
|
|
|
106
|
|
|
|
107
|
|
|
/** @var SMWResultArray[]|false $resultRow */ |
108
|
|
|
$resultRow = $mockBuilder->newObject( 'ResultArray' ); |
109
|
|
|
|
110
|
|
|
//$resultRow->add( $resultCell ); |
111
|
|
|
|
112
|
|
|
$resultSet[] = []; |
|
|
|
|
113
|
|
|
|
114
|
|
|
$resultSet[] = $resultRow; |
115
|
|
|
|
116
|
|
|
/** @var array(SMWResultArray[]|false) $resultSet */ |
|
|
|
|
117
|
|
|
$resultSet[] = false; |
118
|
|
|
|
119
|
|
|
$queryResult = $mockBuilder->newObject( 'QueryResult', [ |
120
|
|
|
'getCount' => 1, |
121
|
|
|
] ); |
122
|
|
|
|
123
|
|
|
$queryResult->expects( $this->any() ) |
124
|
|
|
->method( 'getNext' ) |
125
|
|
|
->will( call_user_func( [ $this, 'onConsecutiveCalls' ], $resultSet ) ); |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
$queryResult = $mockBuilder->newObject( 'QueryResult', [ |
129
|
|
|
'getCount' => 1, |
130
|
|
|
] ); |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] ); |
134
|
|
|
|
135
|
|
|
$expected = ''; |
136
|
|
|
|
137
|
|
|
return [ $queryResult, $params, $expected ]; |
138
|
|
|
} |
139
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: