Completed
Push — master ( c3e068...3f9a02 )
by
unknown
02:14
created

QueryResultTest::testGetLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SEQL\ByHttpRequest\Tests;
4
5
use SEQL\ByHttpRequest\QueryResult;
6
use SMW\DIWikiPage;
7
8
/**
9
 * @covers \SEQL\ByHttpRequest\QueryResult
10
 * @group semantic-external-query-lookup
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class QueryResultTest extends \PHPUnit_Framework_TestCase {
18
19
	private $store;
20
21
	protected function setUp() {
22
23
		$this->store = $this->getMockBuilder( '\SMW\Store' )
24
			->disableOriginalConstructor()
25
			->getMockForAbstractClass();
26
	}
27
28
	public function testCanConstruct() {
29
30
		$query = $this->getMockBuilder( '\SMWQuery' )
31
			->disableOriginalConstructor()
32
			->getMock();
33
34
		$printRequests = array();
35
		$results = array();
36
37
		$this->assertInstanceOf(
38
			'\SEQL\ByHttpRequest\QueryResult',
39
			new QueryResult( $printRequests, $query, $results, $this->store, false )
40
		);
41
	}
42
43
	public function testGetNext() {
44
45
		$jsonResponseParser = $this->getMockBuilder( '\SEQL\ByHttpRequest\JsonResponseParser' )
46
			->disableOriginalConstructor()
47
			->getMock();
48
49
		$query = $this->getMockBuilder( '\SMWQuery' )
50
			->disableOriginalConstructor()
51
			->getMock();
52
53
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
54
			->disableOriginalConstructor()
55
			->getMock();
56
57
		$printRequests = array(
58
			$printRequest
59
		);
60
61
		$results = array(
62
			new DIWikiPage( 'Foo', NS_MAIN )
63
		);
64
65
		$instance = new QueryResult( $printRequests, $query, $results, $this->store, false );
0 ignored issues
show
Documentation introduced by mwjames
$results is of type array<integer,object<SMW...ect<SMW\\DIWikiPage>"}>, but the function expects a array<integer,object<SMWDIWikiPage>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
		$instance->setJsonResponseParser( $jsonResponseParser );
67
68
		foreach ( $instance->getNext() as $result ) {
0 ignored issues
show
Bug introduced by mwjames
The expression $instance->getNext() of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
69
			$this->assertInstanceOf(
70
				'\SEQL\ByHttpRequest\CannedResultArray',
71
				$result
72
			);
73
		}
74
	}
75
76
	public function testToArray() {
77
78
		$expected = array(
79
			'Foo'
80
		);
81
82
		$query = $this->getMockBuilder( '\SMWQuery' )
83
			->disableOriginalConstructor()
84
			->getMock();
85
86
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
0 ignored issues
show
Unused Code introduced by mwjames
$printRequest is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
			->disableOriginalConstructor()
88
			->getMock();
89
90
		$jsonResponseParser = $this->getMockBuilder( '\SEQL\ByHttpRequest\JsonResponseParser' )
91
			->disableOriginalConstructor()
92
			->getMock();
93
94
		$jsonResponseParser->expects( $this->exactly( 2 ) )
95
			->method( 'getRawResponseResult' )
96
			->will( $this->returnValue( $expected ) );
97
98
		$printRequests = array();
99
		$results = array();
100
101
		$instance = new QueryResult( $printRequests, $query, $results, $this->store, false );
102
		$instance->setJsonResponseParser( $jsonResponseParser );
103
104
		$this->assertEquals(
105
			$expected,
106
			$instance->toArray()
107
		);
108
109
		$this->assertEquals(
110
			$expected,
111
			$instance->serializeToArray()
112
		);
113
	}
114
115
	public function testGetLink() {
116
117
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
118
			->disableOriginalConstructor()
119
			->getMock();
120
121
		$printRequest->expects( $this->once() )
122
			->method( 'getSerialisation' )
123
			->will( $this->returnValue( '?ABC' ) );
124
125
		$query = $this->getMockBuilder( '\SMWQuery' )
126
			->disableOriginalConstructor()
127
			->getMock();
128
129
		$query->expects( $this->any() )
130
			->method( 'getExtraPrintouts' )
131
			->will( $this->returnValue( array( $printRequest ) ) );
132
133
		$printRequests = array();
134
		$results = array();
135
136
		$instance = new QueryResult( $printRequests, $query, $results, $this->store, false );
137
		$instance->setRemoteTargetUrl( 'http://example.org:8080' );
138
139
		$this->assertInstanceOf(
140
			'\SMWInfolink',
141
			$instance->getLink()
142
		);
143
	}
144
145
}
146