Completed
Push — master ( aad109...a11cd2 )
by mw
02:20
created

phpunit/Unit/ByHttpRequest/QueryResultTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 );
66
		$instance->setJsonResponseParser( $jsonResponseParser );
67
68
		foreach ( $instance->getNext() as $result ) {
0 ignored issues
show
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' )
87
			->disableOriginalConstructor()
88
			->getMock();
89
90
		$jsonResponseParser = $this->getMockBuilder( '\SEQL\ByHttpRequest\JsonResponseParser' )
91
			->disableOriginalConstructor()
92
			->getMock();
93
94
		$jsonResponseParser->expects( $this->once() )
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
110
	public function testGetLink() {
111
112
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
113
			->disableOriginalConstructor()
114
			->getMock();
115
116
		$printRequest->expects( $this->once() )
117
			->method( 'getSerialisation' )
118
			->will( $this->returnValue( '?ABC' ) );
119
120
		$query = $this->getMockBuilder( '\SMWQuery' )
121
			->disableOriginalConstructor()
122
			->getMock();
123
124
		$query->expects( $this->any() )
125
			->method( 'getExtraPrintouts' )
126
			->will( $this->returnValue( array( $printRequest ) ) );
127
128
		$printRequests = array();
129
		$results = array();
130
131
		$instance = new QueryResult( $printRequests, $query, $results, $this->store, false );
132
		$instance->setRemoteTargetUrl( 'http://example.org:8080' );
133
134
		$this->assertInstanceOf(
135
			'\SMWInfolink',
136
			$instance->getLink()
137
		);
138
	}
139
140
}
141