-
Notifications
You must be signed in to change notification settings - Fork 4
/
SelfInitializingFakeTest.php
38 lines (35 loc) · 1.15 KB
/
SelfInitializingFakeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
class SelfInitializingFakeTest extends PHPUnit_Framework_TestCase
{
public function testReturnsAlwaysTheSameResultForEachQuery()
{
$fake = new GoogleMapsDirectionsSelfInitializingFake();
$httpStart = $this->currentTime();
$fake->getDirections('Milan', 'Rome');
$httpEnd = $this->currentTime();
$fake->getDirections('Milan', 'Rome');
$cachedEnd = $this->currentTime();
$httpTime = $httpEnd - $httpStart;
$cachedTime = $cachedEnd - $httpEnd;
$this->assertGreaterThan(10, $httpTime / $cachedTime);
}
private function currentTime()
{
return microtime(true);
}
}
class GoogleMapsDirectionsSelfInitializingFake
{
private $cache = array();
public function getDirections($from, $to)
{
$url = "http://maps.googleapis.com/maps/api/directions/xml?origin={$from}&destination={$to}&sensor=false";
if (isset($this->cache[$url])) {
$response = $this->cache[$url];
} else {
$response = file_get_contents($url);
$this->cache[$url] = $response;
}
return new SimpleXMLElement($response);
}
}