testCachedRequestToExternalUrl()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 2
nop 0
1
<?php
2
3
namespace Onoi\HttpRequest\Tests\Integration;
4
5
use Onoi\HttpRequest\CachedCurlRequest;
6
use Onoi\HttpRequest\CurlRequest;
7
use Onoi\Cache\FixedInMemoryLruCache;
8
9
/**
10
 * @group onoi-http-request
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class RequestToExternalUrlTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCachedRequestToExternalUrl() {
20
21
		$target = 'http://example.org/';
22
23
		$cache = new FixedInMemoryLruCache();
24
25
		$instance = new CachedCurlRequest(
26
			curl_init( $target ),
27
			$cache
28
		);
29
30
		if ( !$instance->ping() ) {
31
			$this->markTestSkipped( "Can't connect to " . $target );
32
		}
33
34
		$instance->setOption( CURLOPT_RETURNTRANSFER, true );
35
		$instance->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, 42 );
36
		$instance->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, 'foo' );
37
38
		$this->assertInternalType(
39
			'string',
40
			$instance->execute()
41
		);
42
43
		$this->assertFalse(
44
			$instance->isFromCache()
45
		);
46
47
		// Repeated request
48
		$instance->setOption( CURLOPT_RETURNTRANSFER, true );
49
		$instance->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, 42 );
50
		$instance->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, 'foo' );
51
52
		$instance->execute();
53
54
		$this->assertTrue(
55
			$instance->isFromCache()
56
		);
57
	}
58
59
}
60