1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SRF\Test; |
4
|
|
|
|
5
|
|
|
use SMW\Test\QueryPrinterRegistryTestCase; |
6
|
|
|
use SMW\Tests\Utils\Mock\CoreMockObjectRepository; |
7
|
|
|
use SMW\Tests\Utils\Mock\MockObjectBuilder; |
8
|
|
|
use SMWQueryProcessor; |
9
|
|
|
use SRF\Formats\Tree\TreeResultPrinter; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TreeTest |
13
|
|
|
* |
14
|
|
|
* @since 2.5 |
15
|
|
|
* |
16
|
|
|
* @ingroup SemanticResultFormats |
17
|
|
|
* @ingroup Test |
18
|
|
|
* |
19
|
|
|
* @group SRF |
20
|
|
|
* @group SMWExtension |
21
|
|
|
* @group ResultPrinters |
22
|
|
|
* |
23
|
|
|
* @author Stephan Gambke |
24
|
|
|
*/ |
25
|
|
|
class TreeTest extends QueryPrinterRegistryTestCase { |
26
|
|
|
|
27
|
|
|
private $parser; |
28
|
|
|
private $title; |
29
|
|
|
|
30
|
|
|
private static $initial_parser; |
31
|
|
|
private static $initial_title; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Keep the global state and restore it on tearDown to avoid influencing |
35
|
|
|
* other tests in case this one fails in between. |
36
|
|
|
*/ |
37
|
|
|
public static function setUpBeforeClass() { |
|
|
|
|
38
|
|
|
self::$initial_parser = $GLOBALS['wgParser']; |
39
|
|
|
self::$initial_title = $GLOBALS['wgTitle']; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function tearDown() { |
|
|
|
|
43
|
|
|
$GLOBALS['wgParser'] = self::$initial_parser; |
44
|
|
|
$GLOBALS['wgTitle'] = self::$initial_title; |
45
|
|
|
|
46
|
|
|
parent::tearDown(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the names of the formats being tested. |
51
|
|
|
* @return string[] |
52
|
|
|
*/ |
53
|
|
|
public function getFormats() { |
54
|
|
|
return [ 'tree' ]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the name of the class being tested. |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getClass() { |
62
|
|
|
return '\SRF\Formats\Tree\TreeResultPrinter'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
*/ |
67
|
|
|
public function testGetResult_NoParentProperty() { |
|
|
|
|
68
|
|
|
|
69
|
|
|
$this->prepareGlobalState(); |
70
|
|
|
|
71
|
|
|
$mockBuilder = new MockObjectBuilder(); |
72
|
|
|
$mockBuilder->registerRepository( new CoreMockObjectRepository() ); |
73
|
|
|
|
74
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject $queryResult */ |
75
|
|
|
$queryResult = $mockBuilder->newObject( 'QueryResult', [ 'getCount' => 1 ] ); |
76
|
|
|
|
77
|
|
|
$queryResult->expects( $this->once() ) |
78
|
|
|
->method( 'addErrors' ) |
79
|
|
|
->will( $this->returnValue( null ) ); |
80
|
|
|
|
81
|
|
|
$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] ); |
82
|
|
|
|
83
|
|
|
$testObject = new TreeResultPrinter( 'tree' ); |
84
|
|
|
|
85
|
|
|
$this->assertEquals( '', $testObject->getResult( $queryResult, $params, SMW_OUTPUT_HTML ), 'Result should be empty.' ); |
86
|
|
|
|
87
|
|
|
// Restore GLOBAL state to ensure that preceding tests do not use a |
88
|
|
|
// mocked instance |
89
|
|
|
$GLOBALS[ 'wgParser' ] = $this->parser; |
90
|
|
|
$GLOBALS[ 'wgTitle' ] = $this->title; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function prepareGlobalState() { |
|
|
|
|
94
|
|
|
|
95
|
|
|
// Store current state |
96
|
|
|
$this->parser = $GLOBALS[ 'wgParser' ]; |
97
|
|
|
$this->title = $GLOBALS[ 'wgTitle' ]; |
98
|
|
|
|
99
|
|
|
$parserOutput = $this->getMockBuilder( '\ParserOutput' ) |
100
|
|
|
->disableOriginalConstructor() |
101
|
|
|
->getMock(); |
102
|
|
|
|
103
|
|
|
$parserOutput->expects( $this->any() ) |
104
|
|
|
->method( 'getHeadItems' ) |
105
|
|
|
->will( $this->returnValue( [] ) ); |
106
|
|
|
|
107
|
|
|
$parser = $this->getMockBuilder( '\Parser' ) |
108
|
|
|
->disableOriginalConstructor() |
109
|
|
|
->getMock(); |
110
|
|
|
|
111
|
|
|
$parser->expects( $this->any() ) |
112
|
|
|
->method( 'parse' ) |
113
|
|
|
->will( $this->returnValue( $parserOutput ) ); |
114
|
|
|
|
115
|
|
|
$title = $this->getMockBuilder( '\Title' ) |
116
|
|
|
->disableOriginalConstructor() |
117
|
|
|
->getMock(); |
118
|
|
|
|
119
|
|
|
// Careful!! |
120
|
|
|
$GLOBALS[ 'wgParser' ] = $parser; |
121
|
|
|
$GLOBALS[ 'wgTitle' ] = $title; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected function provideQueryParamsAndResults() { |
128
|
|
|
$mockBuilder = new MockObjectBuilder(); |
129
|
|
|
$mockBuilder->registerRepository( new CoreMockObjectRepository() ); |
130
|
|
|
|
131
|
|
|
/** @var \SMWResultArray[]|false $resultRow */ |
132
|
|
|
$resultRow = $mockBuilder->newObject( 'ResultArray' ); |
133
|
|
|
|
134
|
|
|
//$resultRow->add( $resultCell ); |
135
|
|
|
|
136
|
|
|
$resultSet[] = []; |
|
|
|
|
137
|
|
|
|
138
|
|
|
$resultSet[] = $resultRow; |
139
|
|
|
|
140
|
|
|
/** @var array(SMWResultArray[]|false) $resultSet */ |
|
|
|
|
141
|
|
|
$resultSet[] = false; |
142
|
|
|
|
143
|
|
|
$queryResult = $mockBuilder->newObject( 'QueryResult', [ |
144
|
|
|
'getCount' => 1, |
145
|
|
|
] ); |
146
|
|
|
|
147
|
|
|
$queryResult->expects( $this->any() ) |
148
|
|
|
->method( 'getNext' ) |
149
|
|
|
->will( call_user_func( [ $this, 'onConsecutiveCalls' ], $resultSet ) ); |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
$queryResult = $mockBuilder->newObject( 'QueryResult', [ |
153
|
|
|
'getCount' => 1, |
154
|
|
|
] ); |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] ); |
158
|
|
|
|
159
|
|
|
$expected = ''; |
160
|
|
|
|
161
|
|
|
return [ $queryResult, $params, $expected ]; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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: