Completed
Push — master ( f152b5...f23cc1 )
by mw
20:04
created

TreeTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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
	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
Coding Style introduced by
setUpBeforeClass 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...
38
		self::$initial_parser = $GLOBALS['wgParser'];
39
		self::$initial_title = $GLOBALS['wgTitle'];
40
	}
41
42
	protected function tearDown() {
0 ignored issues
show
Coding Style introduced by
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
	 * @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() {
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...
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() {
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...
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[] = [];
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...
137
138
		$resultSet[] = $resultRow;
139
140
		/** @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...
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