MultiCurlRequestTest::testPingConnectionState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 1
1
<?php
2
3
namespace Onoi\HttpRequest\Tests;
4
5
use Onoi\HttpRequest\MultiCurlRequest;
6
use Onoi\HttpRequest\CurlRequest;
7
8
/**
9
 * @covers \Onoi\HttpRequest\MultiCurlRequest
10
 * @group onoi-http-request
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class MultiCurlRequestTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$instance = new MultiCurlRequest( curl_multi_init() );
22
23
		$this->assertInstanceOf(
24
			'\Onoi\HttpRequest\HttpRequest',
25
			$instance
26
		);
27
28
		$this->assertInstanceOf(
29
			'\Onoi\HttpRequest\MultiCurlRequest',
30
			$instance
31
		);
32
	}
33
34
	public function testWrongResourceTypeThrowsException() {
35
		$this->setExpectedException( 'InvalidArgumentException' );
36
		new MultiCurlRequest( curl_init() );
37
	}
38
39
	public function testPingForEmptyHttpRequest() {
40
41
		$instance = new MultiCurlRequest( curl_multi_init() );
42
43
		$this->assertFalse(
44
			$instance->ping()
45
		);
46
	}
47
48
	/**
49
	 * @dataProvider pingConnectionStateProvider
50
	 */
51
	public function testPingConnectionState( $connectionState ) {
52
53
		$instance = new MultiCurlRequest( curl_multi_init() );
54
55
		$httpRequest = $this->getMockBuilder( '\Onoi\HttpRequest\CurlRequest' )
56
			->disableOriginalConstructor()
57
			->getMock();
58
59
		$httpRequest->expects( $this->once() )
60
			->method( 'ping' )
61
			->will( $this->returnValue( $connectionState ) );
62
63
		$instance->addHttpRequest(
64
			$httpRequest
65
		);
66
67
		$this->assertEquals(
68
			$connectionState,
69
			$instance->ping()
70
		);
71
	}
72
73
	public function testSetGetOption() {
74
75
		// https://github.com/facebook/hhvm/issues/5761
76
		if ( !defined( 'CURLMOPT_MAXCONNECTS' ) ) {
77
			$this->markTestSkipped( "Option is not supported for current PHP version" );
78
		}
79
80
		$instance = new MultiCurlRequest( curl_multi_init() );
81
82
		$instance->setOption(
83
			CURLMOPT_MAXCONNECTS,
84
			5
85
		);
86
87
		$this->assertSame(
88
			5,
89
			$instance->getOption( CURLMOPT_MAXCONNECTS )
90
		);
91
	}
92
93
	public function testExecuteForResponse() {
94
95
		$instance = new MultiCurlRequest( curl_multi_init() );
96
97
		$instance->addHttpRequest(
98
			new CurlRequest( curl_init() )
99
		);
100
101
		$this->assertInternalType(
102
			'array',
103
			$instance->execute()
104
		);
105
106
		$this->assertInternalType(
107
			'string',
108
			$instance->getLastError()
109
		);
110
111
		// http://php.net/manual/en/function.curl-multi-info-read.php
112
		$this->assertFalse(
113
			$instance->getLastTransferInfo()
114
		);
115
	}
116
117
	public function testDeprecatedSetCallback() {
118
119
		$instance = new MultiCurlRequest( curl_multi_init() );
120
121
		$instance->addHttpRequest(
122
			new CurlRequest( curl_init() )
123
		);
124
125
		$requestResponse = null;
126
127
		$instance->setCallback( function( $requestResponseCompleted ) use ( &$requestResponse ) {
0 ignored issues
show
Deprecated Code introduced by
The method Onoi\HttpRequest\MultiCurlRequest::setCallback() has been deprecated with message: since 1.1, use ONOI_HTTP_REQUEST_ON_COMPLETED_CALLBACK instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
128
			$requestResponse = $requestResponseCompleted;
129
		} );
130
131
		$instance->execute();
132
133
		$this->assertRequestResponse( $requestResponse );
134
	}
135
136
	public function testOnCompletedCallback() {
137
138
		$instance = new MultiCurlRequest( curl_multi_init() );
139
140
		$instance->addHttpRequest(
141
			new CurlRequest( curl_init() )
142
		);
143
144
		$requestResponse = null;
145
146
		$instance->setOption( ONOI_HTTP_REQUEST_ON_COMPLETED_CALLBACK, function( $requestResponseCompleted ) use ( &$requestResponse ) {
147
			$requestResponse = $requestResponseCompleted;
148
		} );
149
150
		$instance->execute();
151
152
		$this->assertSame(
153
			0,
154
			$instance->getLastErrorCode()
155
		);
156
157
		$this->assertRequestResponse( $requestResponse );
158
	}
159
160
	private function assertRequestResponse( $requestResponse ) {
161
162
		$this->assertInstanceOf(
163
			'\Onoi\HttpRequest\RequestResponse',
164
			$requestResponse
165
		);
166
167
		$expectedRequestResponseFields = array(
168
			'contents',
169
			'info'
170
		);
171
172
		foreach ( $expectedRequestResponseFields as $field ) {
173
			$this->assertTrue( $requestResponse->has( $field ) );
174
		}
175
	}
176
177
	public function pingConnectionStateProvider() {
178
179
		$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...
180
			true
181
		);
182
183
		$provider[] = array(
184
			false
185
		);
186
187
		return $provider;
188
	}
189
190
}
191