These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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() { |
||
0 ignored issues
–
show
|
|||
38 | self::$initial_parser = $GLOBALS['wgParser']; |
||
39 | self::$initial_title = $GLOBALS['wgTitle']; |
||
40 | } |
||
41 | |||
42 | protected function tearDown() { |
||
0 ignored issues
–
show
tearDown uses the super-global variable $GLOBALS which is generally not recommended.
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: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
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 | * |
||
52 | * @return string[] |
||
53 | */ |
||
54 | public function getFormats() { |
||
55 | return [ 'tree' ]; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Returns the name of the class being tested. |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getClass() { |
||
64 | return '\SRF\Formats\Tree\TreeResultPrinter'; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | */ |
||
69 | public function testGetResult_NoParentProperty() { |
||
70 | |||
71 | $this->prepareGlobalState(); |
||
72 | |||
73 | $mockBuilder = new MockObjectBuilder(); |
||
74 | $mockBuilder->registerRepository( new CoreMockObjectRepository() ); |
||
75 | |||
76 | /** @var \PHPUnit_Framework_MockObject_MockObject $queryResult */ |
||
77 | $queryResult = $mockBuilder->newObject( 'QueryResult', [ 'getCount' => 1 ] ); |
||
78 | |||
79 | $queryResult->expects( $this->once() ) |
||
80 | ->method( 'addErrors' ) |
||
81 | ->will( $this->returnValue( null ) ); |
||
82 | |||
83 | $params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] ); |
||
84 | |||
85 | $testObject = new TreeResultPrinter( 'tree' ); |
||
86 | |||
87 | $this->assertEquals( |
||
88 | '', |
||
89 | $testObject->getResult( $queryResult, $params, SMW_OUTPUT_HTML ), |
||
90 | 'Result should be empty.' |
||
91 | ); |
||
92 | |||
93 | // Restore GLOBAL state to ensure that preceding tests do not use a |
||
94 | // mocked instance |
||
95 | $GLOBALS['wgParser'] = $this->parser; |
||
96 | $GLOBALS['wgTitle'] = $this->title; |
||
97 | } |
||
98 | |||
99 | protected function prepareGlobalState() { |
||
100 | |||
101 | // Store current state |
||
102 | $this->parser = $GLOBALS['wgParser']; |
||
103 | $this->title = $GLOBALS['wgTitle']; |
||
104 | |||
105 | $parserOutput = $this->getMockBuilder( '\ParserOutput' ) |
||
106 | ->disableOriginalConstructor() |
||
107 | ->getMock(); |
||
108 | |||
109 | $parserOutput->expects( $this->any() ) |
||
110 | ->method( 'getHeadItems' ) |
||
111 | ->will( $this->returnValue( [] ) ); |
||
112 | |||
113 | $parser = $this->getMockBuilder( '\Parser' ) |
||
114 | ->disableOriginalConstructor() |
||
115 | ->getMock(); |
||
116 | |||
117 | $parser->expects( $this->any() ) |
||
118 | ->method( 'parse' ) |
||
119 | ->will( $this->returnValue( $parserOutput ) ); |
||
120 | |||
121 | $title = $this->getMockBuilder( '\Title' ) |
||
122 | ->disableOriginalConstructor() |
||
123 | ->getMock(); |
||
124 | |||
125 | // Careful!! |
||
126 | $GLOBALS['wgParser'] = $parser; |
||
127 | $GLOBALS['wgTitle'] = $title; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return array |
||
132 | */ |
||
133 | protected function provideQueryParamsAndResults() { |
||
134 | $mockBuilder = new MockObjectBuilder(); |
||
135 | $mockBuilder->registerRepository( new CoreMockObjectRepository() ); |
||
136 | |||
137 | /** @var \SMWResultArray[]|false $resultRow */ |
||
138 | $resultRow = $mockBuilder->newObject( 'ResultArray' ); |
||
139 | |||
140 | //$resultRow->add( $resultCell ); |
||
141 | |||
142 | $resultSet[] = []; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$resultSet was never initialized. Although not strictly required by PHP, it is generally a good practice to add $resultSet = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.
Loading history...
|
|||
143 | |||
144 | $resultSet[] = $resultRow; |
||
145 | |||
146 | /** @var array(SMWResultArray[]|false) $resultSet */ |
||
0 ignored issues
–
show
The doc-type
array(SMWResultArray[]|false) could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.
Loading history...
|
|||
147 | $resultSet[] = false; |
||
148 | |||
149 | $queryResult = $mockBuilder->newObject( |
||
150 | 'QueryResult', |
||
151 | [ |
||
152 | 'getCount' => 1, |
||
153 | ] |
||
154 | ); |
||
155 | |||
156 | $queryResult->expects( $this->any() ) |
||
157 | ->method( 'getNext' ) |
||
158 | ->will( call_user_func( [ $this, 'onConsecutiveCalls' ], $resultSet ) ); |
||
159 | |||
160 | $queryResult = $mockBuilder->newObject( |
||
161 | 'QueryResult', |
||
162 | [ |
||
163 | 'getCount' => 1, |
||
164 | ] |
||
165 | ); |
||
166 | |||
167 | $params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] ); |
||
168 | |||
169 | $expected = ''; |
||
170 | |||
171 | return [ $queryResult, $params, $expected ]; |
||
172 | } |
||
173 | } |
||
174 |
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: