Completed
Push — master ( 3184dc...186529 )
by mw
36:28
created

testApplyOrderConditionsWithInvalidSortKeyThrowsException()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\SQLStore\QueryEngine;
4
5
use SMW\SQLStore\QueryEngine\OrderConditionsComplementor;
6
use SMW\SQLStore\QueryEngine\QuerySegment;
7
8
/**
9
 * @covers \SMW\SQLStore\QueryEngine\OrderConditionsComplementor
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.5
14
 *
15
 * @author mwjames
16
 */
17
class OrderConditionsComplementorTest extends \PHPUnit_Framework_TestCase {
18
19
	private $querySegmentListBuilder;
20
21
	protected function setUp() {
22
23
		$this->querySegmentListBuilder = $this->getMockBuilder( '\SMW\SQLStore\QueryEngine\QuerySegmentListBuilder' )
24
			->disableOriginalConstructor()
25
			->getMock();
26
	}
27
28
	public function testCanConstruct() {
29
30
		$this->assertInstanceOf(
31
			'\SMW\SQLStore\QueryEngine\OrderConditionsComplementor',
32
			new OrderConditionsComplementor( $this->querySegmentListBuilder )
33
		);
34
	}
35
36
	public function testApplyOrderConditionsWithoutSortKey() {
37
38
		$this->querySegmentListBuilder->expects( $this->once() )
39
			->method( 'getQuerySegmentList' );
40
41
		$instance = new OrderConditionsComplementor(
42
			$this->querySegmentListBuilder
43
		);
44
45
		$instance->applyOrderConditions( 42 );
46
	}
47
48
	/**
49
	 * @dataProvider sortKeyProvider
50
	 */
51
	public function testApplyOrderConditionsWithSortKey( $sortKeys ) {
52
53
		$querySegment = new QuerySegment();
54
55
		$this->querySegmentListBuilder->expects( $this->once() )
56
			->method( 'getQuerySegmentList' );
57
58
		$this->querySegmentListBuilder->expects( $this->atLeastOnce() )
59
			->method( 'findQuerySegment' )
60
			->will( $this->returnValue( $querySegment ) );
61
62
		$instance = new OrderConditionsComplementor(
63
			$this->querySegmentListBuilder
64
		);
65
66
		$instance->setSortKeys( $sortKeys );
67
68
		$instance->applyOrderConditions( 42 );
69
		$querySegment->reset();
70
	}
71
72
	public function testApplyOrderConditionsWithInvalidSortKeyThrowsException() {
73
74
		$querySegment = new QuerySegment();
75
76
		$this->querySegmentListBuilder->expects( $this->never() )
77
			->method( 'getQuerySegmentList' );
78
79
		$this->querySegmentListBuilder->expects( $this->atLeastOnce() )
80
			->method( 'findQuerySegment' )
81
			->will( $this->returnValue( $querySegment ) );
82
83
		$instance = new OrderConditionsComplementor(
84
			$this->querySegmentListBuilder
85
		);
86
87
		$instance->setSortKeys( array(
88
				42 => 'ASC'
89
			)
90
		);
91
92
		$this->setExpectedException( 'RuntimeException' );
93
		$instance->applyOrderConditions( 42 );
94
95
		$querySegment->reset();
96
	}
97
98
	public function sortKeyProvider() {
99
100
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = 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...
101
			array(
102
				'' => 'ASC'
103
			)
104
		);
105
106
		$provider[] = array(
107
			array(
108
				'#' => 'DESC'
109
			)
110
		);
111
112
		$provider[] = array(
113
			array(
114
				'Foo' => 'ASC'
115
			)
116
		);
117
118
		return $provider;
119
	}
120
121
}
122