1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\HttpRequest\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\HttpRequest\SocketRequest; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\HttpRequest\SocketRequest |
9
|
|
|
* @group onoi-http-request |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.2 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class MockedHttpStreamSocketRequestTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function setUp() { |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
stream_wrapper_unregister( 'http' ); |
22
|
|
|
$return = stream_wrapper_register( |
23
|
|
|
'http', |
24
|
|
|
'Onoi\HttpRequest\Tests\MockHttpStreamWrapper' |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
if ( !$return ) { |
28
|
|
|
$this->markTestSkipped( 'Skip test due to a failed stream wrapper protocol registration' ); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getMockStream( $data, $code = 'HTTP/1.1 200 OK' ) { |
33
|
|
|
MockHttpStreamWrapper::$mockBodyData = $data; |
34
|
|
|
MockHttpStreamWrapper::$mockResponseCode = $code; |
35
|
|
|
|
36
|
|
|
$context = stream_context_create( |
37
|
|
|
array( |
38
|
|
|
'http' => array( |
39
|
|
|
'method' => 'GET' |
40
|
|
|
) |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
return fopen( 'http://example.com', 'r', false, $context ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function tearDown() { |
48
|
|
|
stream_wrapper_restore('http'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider locationProvider |
53
|
|
|
*/ |
54
|
|
|
public function testFollowLocation( $url, $urlComponent, $followLocation, $expectedUrl ) { |
55
|
|
|
|
56
|
|
|
$instance = $this->getMockBuilder( '\Onoi\HttpRequest\SocketRequest' ) |
57
|
|
|
->disableOriginalConstructor() |
58
|
|
|
->setMethods( array( 'getResourceFromSocketClient' ) ) |
59
|
|
|
->getMock(); |
60
|
|
|
|
61
|
|
|
$instance->expects( $this->once() ) |
62
|
|
|
->method( 'getResourceFromSocketClient' ) |
63
|
|
|
->with( |
64
|
|
|
$this->equalTo( $urlComponent ), |
65
|
|
|
$this->anything() ) |
66
|
|
|
->will( $this->returnValue( |
67
|
|
|
$this->getMockStream( "HTTP/1.1 301 Moved \nLocation: " . $followLocation ) ) ); |
68
|
|
|
|
69
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_URL, $url ); |
70
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_FOLLOWLOCATION, true ); |
71
|
|
|
|
72
|
|
|
$instance->ping(); |
73
|
|
|
|
74
|
|
|
$this->assertEquals( |
75
|
|
|
$expectedUrl, |
76
|
|
|
$instance->getOption( ONOI_HTTP_REQUEST_URL ) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testToReturnInvalidResource() { |
81
|
|
|
|
82
|
|
|
$instance = $this->getMockBuilder( '\Onoi\HttpRequest\SocketRequest' ) |
83
|
|
|
->disableOriginalConstructor() |
84
|
|
|
->setMethods( array( 'getResourceFromSocketClient' ) ) |
85
|
|
|
->getMock(); |
86
|
|
|
|
87
|
|
|
$instance->expects( $this->once() ) |
88
|
|
|
->method( 'getResourceFromSocketClient' ) |
89
|
|
|
->will( $this->returnValue( false ) ); |
90
|
|
|
|
91
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_URL, 'http://example.com/' ); |
92
|
|
|
|
93
|
|
|
$this->assertEquals( |
94
|
|
|
false, |
95
|
|
|
$instance->execute() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function locationProvider() { |
100
|
|
|
|
101
|
|
|
$urlComponent = array ( |
102
|
|
|
'scheme' => 'http', |
103
|
|
|
'host' => 'example.com', |
104
|
|
|
'port' => 80, |
105
|
|
|
'path' => '' |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$provider[] = array( |
|
|
|
|
109
|
|
|
'http://example.com', |
110
|
|
|
$urlComponent, |
111
|
|
|
'http://abc.com', |
112
|
|
|
'http://abc.com' |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$provider[] = array( |
116
|
|
|
'http://example.com', |
117
|
|
|
$urlComponent, |
118
|
|
|
'/foo', |
119
|
|
|
'http://example.com/foo' |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$urlComponent = array ( |
123
|
|
|
'scheme' => 'https', |
124
|
|
|
'host' => 'tls://example.com', |
125
|
|
|
'port' => 443, |
126
|
|
|
'path' => '' |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$provider[] = array( |
130
|
|
|
'https://example.com', |
131
|
|
|
$urlComponent, |
132
|
|
|
'/foo', |
133
|
|
|
'https://example.com/foo' |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$urlComponent = array ( |
137
|
|
|
'scheme' => 'https', |
138
|
|
|
'host' => 'tls://example.com', |
139
|
|
|
'port' => 4443, |
140
|
|
|
'path' => '' |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$provider[] = array( |
144
|
|
|
'https://example.com:4443', |
145
|
|
|
$urlComponent, |
146
|
|
|
'/foo', |
147
|
|
|
'https://example.com/foo' |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
return $provider; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
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:
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 thebar
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.