RequestToPhpHttpdTest::getHttpdRequestUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Onoi\HttpRequest\Tests\Integration;
4
5
use Onoi\HttpRequest\HttpRequestFactory;
6
use Onoi\Cache\CacheFactory;
7
use RuntimeException;
8
9
/**
10
 * @group onoi-http-request
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class RequestToPhpHttpdTest extends \PHPUnit_Framework_TestCase {
18
19
	private static $pid;
20
21
	/**
22
	 * @note Using the PHP in-build webserver to serve a slow/fast page response for
23
	 * validation of the async response
24
	 *
25
	 * Options defined in phpunit.xml.dist
26
	 * @see https://github.com/vgno/tech.vg.no-1812/blob/master/features/bootstrap/FeatureContext.php
27
	 */
28
	public static function setUpBeforeClass() {
29
30
		$command = sprintf( 'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!',
31
			WEB_SERVER_HOST,
32
			WEB_SERVER_PORT,
33
			WEB_SERVER_DOCROOT
34
		);
35
36
		$output = array();
37
		exec( $command, $output );
38
39
		self::$pid = (int) $output[0];
40
	}
41
42
	public static function tearDownAfterClass() {
43
	//	exec( 'kill ' . (int) self::$pid );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
	}
45
46
	public function testQueuedResponse() {
47
48
		$this->connectToHttpd();
49
		$expectedToCount = 5;
50
51
		$httpRequestFactory = new HttpRequestFactory();
52
		$multiCurlRequest = $httpRequestFactory->newMultiCurlRequest();
53
54
		for ( $i = 0; $i < $expectedToCount; $i++ ) {
55
			$multiCurlRequest->addHttpRequest(
56
				$httpRequestFactory->newCurlRequest( $this->getHttpdRequestUrl( $i ) )
57
			);
58
		}
59
60
		$this->assertCount(
61
			$expectedToCount,
62
			$multiCurlRequest->execute()
63
		);
64
	}
65
66
	public function testCachedMultiCurlQueuedResponse() {
67
68
		$this->connectToHttpd();
69
		$expectedToCount = 5;
70
71
		$cacheFactory = new CacheFactory();
72
73
		$httpRequestFactory = new HttpRequestFactory(
74
			$cacheFactory->newFixedInMemoryLruCache()
75
		);
76
77
		$multiCurlRequest = $httpRequestFactory->newMultiCurlRequest();
78
79
		for ( $i = 0; $i < $expectedToCount; $i++ ) {
80
			$multiCurlRequest->addHttpRequest(
81
				$httpRequestFactory->newCachedCurlRequest( $this->getHttpdRequestUrl( $i ) )
82
			);
83
		}
84
85
		$this->assertCount(
86
			$expectedToCount,
87
			$multiCurlRequest->execute()
88
		);
89
	}
90
91
	public function testMultiCurlForCallbackResponse() {
92
93
		$this->connectToHttpd();
94
		$expectedToCount = 5;
95
96
		$asyncCallbackResponseMock = $this->getMockBuilder( '\stdClass' )
97
			->setMethods( array( 'run' ) )
98
			->getMock();
99
100
		$asyncCallbackResponseMock->expects( $this->exactly( $expectedToCount ) )
101
			->method( 'run' );
102
103
		$httpRequestFactory = new HttpRequestFactory();
104
		$multiCurlRequest = $httpRequestFactory->newMultiCurlRequest();
105
106
		$multiCurlRequest->setOption( ONOI_HTTP_REQUEST_ON_COMPLETED_CALLBACK, function( $requestResponse ) use ( $asyncCallbackResponseMock ) {
107
			$asyncCallbackResponseMock->run( $requestResponse );
108
		} );
109
110
		for ( $i = 0; $i < $expectedToCount; $i++ ) {
111
			$multiCurlRequest->addHttpRequest(
112
				$httpRequestFactory->newCurlRequest( $this->getHttpdRequestUrl( $i ) )
113
			);
114
		}
115
116
		$multiCurlRequest->execute();
117
	}
118
119
	public function testSocketResponse() {
120
121
		$this->connectToHttpd();
122
		$expectedToCount = 1;
123
124
		$asyncCallbackResponseMock = $this->getMockBuilder( '\stdClass' )
125
			->setMethods( array( 'run' ) )
126
			->getMock();
127
128
		$asyncCallbackResponseMock->expects( $this->exactly( $expectedToCount ) )
129
			->method( 'run' );
130
131
		$httpRequestFactory = new HttpRequestFactory();
132
		$socketRequest = $httpRequestFactory->newSocketRequest();
133
134
		$socketRequest->setOption( ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT, 5 );
135
		$socketRequest->setOption( ONOI_HTTP_REQUEST_CONNECTION_FAILURE_REPEAT, 2 );
136
137
		$socketRequest->setOption( ONOI_HTTP_REQUEST_METHOD, 'GET' );
138
		$socketRequest->setOption( ONOI_HTTP_REQUEST_URL, WEB_SERVER_HOST . ':' .  WEB_SERVER_PORT . '/plain.php' );
139
140
		$socketRequest->ping();
141
142
		$socketRequest->setOption( ONOI_HTTP_REQUEST_ON_FAILED_CALLBACK, function( $requestResponse ) use ( $asyncCallbackResponseMock ) {
143
			$asyncCallbackResponseMock->run( $requestResponse );
144
		} );
145
146
		$socketRequest->execute();
147
	}
148
149
	private function getHttpdRequestUrl( $id ) {
150
151
		// slow/fast example used from https://gist.github.com/Xeoncross/2362936
152
153
		if ( $id % 2 ) {
154
			return WEB_SERVER_HOST . ':' .  WEB_SERVER_PORT . '/slow.php?id=' . $id;
155
		}
156
157
		return WEB_SERVER_HOST . ':' .  WEB_SERVER_PORT . '/fast.php?id=' . $id;
158
	}
159
160
	private function connectToHttpd() {
161
162
		$start = microtime(true);
163
164
		// Try to connect
165
		while ( microtime( true ) - $start <= 10 ) {
166
			if ( $this->canConnectToHttpd() ) {
167
				break;
168
			}
169
		}
170
	}
171
172
	private function canConnectToHttpd() {
173
174
		$sp = @fsockopen( WEB_SERVER_HOST, WEB_SERVER_PORT );
175
176
		if ( $sp === false ) {
177
			return false;
178
		}
179
180
		fclose( $sp );
181
182
		return true;
183
	}
184
185
}
186