Completed
Pull Request — master (#226)
by Stephan
02:29
created

TreeTest::provideQueryParamsAndResults()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
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() {
0 ignored issues
show
Coding Style introduced by
prepareGlobalState 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...
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[]|false $resultRow */
105
		$resultRow = $mockBuilder->newObject( 'ResultArray' );
106
107
		//$resultRow->add( $resultCell );
108
109
		$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 $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

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...
110
111
		$resultSet[] = $resultRow;
112
113
		/** @var array(SMWResultArray[]|false) $resultSet */
0 ignored issues
show
Documentation introduced by
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...
114
		$resultSet[] = false;
115
116
		$queryResult = $mockBuilder->newObject( 'QueryResult', [
117
			'getCount' => 1,
118
		] );
119
120
		$queryResult->expects( $this->any() )
121
			->method( 'getNext' )
122
			->will( call_user_func( [ $this, 'onConsecutiveCalls' ], $resultSet ) );
123
124
125
		$queryResult = $mockBuilder->newObject( 'QueryResult', [
126
			'getCount' => 1,
127
		] );
128
129
130
		$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] );
131
132
		$expected = '';
133
134
		return [ $queryResult, $params, $expected ];
135
	}
136
}