Completed
Pull Request — master (#249)
by mw
02:01
created

TreeTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 121
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormats() 0 3 1
A getClass() 0 3 1
B testGetResult_NoParentProperty() 0 25 1
B prepareGlobalState() 0 30 1
B provideQueryParamsAndResults() 0 36 1
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
	/**
31
	 * Returns the names of the formats being tested.
32
	 * @return string[]
33
	 */
34
	public function getFormats() {
35
		return [ 'tree' ];
36
	}
37
38
	/**
39
	 * Returns the name of the class being tested.
40
	 * @return string
41
	 */
42
	public function getClass() {
43
		return '\SRF\Formats\Tree\TreeResultPrinter';
44
	}
45
46
	/**
47
	 * @covers \SRF\Formats\Tree\TreeResultPrinter::getResult()
48
	 */
49
	public function testGetResult_NoParentProperty() {
0 ignored issues
show
Coding Style introduced by
testGetResult_NoParentProperty 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...
50
51
		$this->prepareGlobalState();
52
53
		$mockBuilder = new MockObjectBuilder();
54
		$mockBuilder->registerRepository( new CoreMockObjectRepository() );
55
56
		/** @var \PHPUnit_Framework_MockObject_MockObject $queryResult */
57
		$queryResult = $mockBuilder->newObject( 'QueryResult', [ 'getCount' => 1 ] );
58
59
		$queryResult->expects( $this->once() )
60
			->method( 'addErrors' )
61
			->will( $this->returnValue( null ) );
62
63
		$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] );
64
65
		$testObject = new TreeResultPrinter( 'tree' );
66
67
		$this->assertEquals( '', $testObject->getResult( $queryResult, $params, SMW_OUTPUT_HTML ), 'Result should be empty.' );
68
69
		// Restore GLOBAL state to ensure that preceding tests do not use a
70
		// mocked instance
71
		$GLOBALS[ 'wgParser' ] = $this->parser;
72
		$GLOBALS[ 'wgTitle' ] = $this->title;
73
	}
74
75
	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...
76
77
		// Store current state
78
		$this->parser = $GLOBALS[ 'wgParser' ];
79
		$this->title = $GLOBALS[ 'wgTitle' ];
80
81
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
82
			->disableOriginalConstructor()
83
			->getMock();
84
85
		$parserOutput->expects( $this->any() )
86
			->method( 'getHeadItems' )
87
			->will( $this->returnValue( [] ) );
88
89
		$parser = $this->getMockBuilder( '\Parser' )
90
			->disableOriginalConstructor()
91
			->getMock();
92
93
		$parser->expects( $this->any() )
94
			->method( 'parse' )
95
			->will( $this->returnValue( $parserOutput ) );
96
97
		$title = $this->getMockBuilder( '\Title' )
98
			->disableOriginalConstructor()
99
			->getMock();
100
101
		// Careful!!
102
		$GLOBALS[ 'wgParser' ] = $parser;
103
		$GLOBALS[ 'wgTitle' ] = $title;
104
	}
105
106
	/**
107
	 * @return array
108
	 */
109
	protected function provideQueryParamsAndResults() {
110
		$mockBuilder = new MockObjectBuilder();
111
		$mockBuilder->registerRepository( new CoreMockObjectRepository() );
112
113
		/** @var \SMWResultArray[]|false $resultRow */
114
		$resultRow = $mockBuilder->newObject( 'ResultArray' );
115
116
		//$resultRow->add( $resultCell );
117
118
		$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...
119
120
		$resultSet[] = $resultRow;
121
122
		/** @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...
123
		$resultSet[] = false;
124
125
		$queryResult = $mockBuilder->newObject( 'QueryResult', [
126
			'getCount' => 1,
127
		] );
128
129
		$queryResult->expects( $this->any() )
130
			->method( 'getNext' )
131
			->will( call_user_func( [ $this, 'onConsecutiveCalls' ], $resultSet ) );
132
133
134
		$queryResult = $mockBuilder->newObject( 'QueryResult', [
135
			'getCount' => 1,
136
		] );
137
138
139
		$params = SMWQueryProcessor::getProcessedParams( [ 'format' => 'tree' ], [] );
140
141
		$expected = '';
142
143
		return [ $queryResult, $params, $expected ];
144
	}
145
}
146