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

TreeTest::prepareGlobalState()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
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
	/**
28
	 * Returns the names of the formats being tested.
29
	 * @return string[]
30
	 */
31
	public function getFormats() {
32
		return [ 'tree' ];
33
	}
34
35
	/**
36
	 * Returns the name of the class being tested.
37
	 * @return string
38
	 */
39
	public function getClass() {
40
		return '\SRF\Formats\Tree\TreeResultPrinter';
41
	}
42
43
	/**
44
	 * @covers \SRF\Formats\Tree\TreeResultPrinter::getResult()
45
	 */
46
	public function testGetResult_NoParentProperty() {
47
48
		$this->prepareGlobalState();
49
50
		$mockBuilder = new MockObjectBuilder();
51
		$mockBuilder->registerRepository( new CoreMockObjectRepository() );
52
53
		/** @var \PHPUnit_Framework_MockObject_MockObject $queryResult */
54
		$queryResult = $mockBuilder->newObject( 'QueryResult', [ 'getCount' => 1 ] );
55
56
		$queryResult->expects( $this->once() )
57
			->method( 'addErrors' )
58
			->will( $this->returnValue( null ) );
59
60
		$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] );
61
62
		$testObject = new TreeResultPrinter( 'tree' );
63
64
		$this->assertEquals( '', $testObject->getResult( $queryResult, $params, SMW_OUTPUT_HTML ), 'Result should be empty.' );
65
66
67
	}
68
69
	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...
70
71
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
72
			->disableOriginalConstructor()
73
			->getMock();
74
75
		$parserOutput->expects( $this->any() )
76
			->method( 'getHeadItems' )
77
			->will( $this->returnValue( [] ) );
78
79
		$parser = $this->getMockBuilder( '\Parser' )
80
			->disableOriginalConstructor()
81
			->getMock();
82
83
		$parser->expects( $this->any() )
84
			->method( 'parse' )
85
			->will( $this->returnValue( $parserOutput ) );
86
87
		$title = $this->getMockBuilder( '\Title' )
88
			->disableOriginalConstructor()
89
			->getMock();
90
91
		$GLOBALS[ 'wgParser' ] = $parser;
92
		$GLOBALS[ 'wgTitle' ] = $title;
93
94
	}
95
96
	/**
97
	 * @return array
98
	 */
99
	protected function provideQueryParamsAndResults() {
100
		$mockBuilder = new MockObjectBuilder();
101
		$mockBuilder->registerRepository( new CoreMockObjectRepository() );
102
103
		/** @var \SMWResultArray[]|false $resultRow */
104
		$resultRow = $mockBuilder->newObject( 'ResultArray' );
105
106
		//$resultRow->add( $resultCell );
107
108
		$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...
109
110
		$resultSet[] = $resultRow;
111
112
		/** @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...
113
		$resultSet[] = false;
114
115
		$queryResult = $mockBuilder->newObject( 'QueryResult', [
116
			'getCount' => 1,
117
		] );
118
119
		$queryResult->expects( $this->any() )
120
			->method( 'getNext' )
121
			->will( call_user_func( [ $this, 'onConsecutiveCalls' ], $resultSet ) );
122
123
124
		$queryResult = $mockBuilder->newObject( 'QueryResult', [
125
			'getCount' => 1,
126
		] );
127
128
129
		$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] );
130
131
		$expected = '';
132
133
		return [ $queryResult, $params, $expected ];
134
	}
135
}