Skip to content

Commit 53a1aa7

Browse files
committed
add geocoder logger test
1 parent 5008644 commit 53a1aa7

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

Tests/Logger/GeocoderLoggerTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\Bundle\GeocoderBundle\Tests\Logger;
12+
13+
use Bazinga\Bundle\GeocoderBundle\Logger\GeocoderLogger;
14+
use Geocoder\Result\Geocoded;
15+
16+
/**
17+
* @author Antoine Corcy <[email protected]>
18+
*/
19+
class GeocoderLoggerTest extends \PHPUnit_Framework_TestCase
20+
{
21+
protected $geocoderLogger;
22+
protected $result;
23+
protected $results;
24+
25+
public function setUp()
26+
{
27+
$logger = $this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface');
28+
$logger
29+
->expects($this->any())
30+
->method('info')
31+
->will($this->returnValue(null))
32+
;
33+
34+
$this->geocoderLogger = new GeocoderLogger($logger);
35+
36+
$this->result = new Geocoded;
37+
$this->result->fromArray(array(
38+
'latitude' => 1,
39+
'longitude' => 2,
40+
));
41+
42+
$this->results = new \SplObjectStorage;
43+
$this->results->attach($this->result);
44+
45+
$otherResult = new Geocoded;
46+
$otherResult->fromArray(array(
47+
'latitude' => 3,
48+
'longitude' => 4,
49+
));
50+
51+
$this->results->attach($otherResult);
52+
}
53+
54+
public function testLogSingleResult()
55+
{
56+
$this->geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->result);
57+
58+
$this->assertTrue(is_array($requests = $this->geocoderLogger->getRequests()));
59+
$this->assertCount(1, $requests);
60+
$this->assertTrue(is_array($request = $requests[0]));
61+
$this->assertSame($request['value'], 'copenhagen');
62+
$this->assertSame($request['duration'], 0.123);
63+
$this->assertSame($request['providerClass'], 'FooProvider');
64+
$this->assertSame($request['result'], '{"latitude":1,"longitude":2,"bounds":null,"streetNumber":null,"streetName":null,"zipcode":null,"city":null,"cityDistrict":null,"county":null,"countyCode":null,"region":null,"regionCode":null,"country":null,"countryCode":null,"timezone":null}');
65+
}
66+
67+
public function testLog2RequestsWithSingleResult()
68+
{
69+
$this->geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->result);
70+
$this->geocoderLogger->logRequest('paris', 0.456, 'BarProvider', $this->result);
71+
72+
$this->assertTrue(is_array($requests = $this->geocoderLogger->getRequests()));
73+
$this->assertCount(2, $requests);
74+
$this->assertTrue(is_array($request = $requests[0]));
75+
$this->assertSame($request['value'], 'copenhagen');
76+
$this->assertTrue(is_array($request = $requests[1]));
77+
$this->assertSame($request['value'], 'paris');
78+
}
79+
80+
public function testLogMultipleResults()
81+
{
82+
$this->geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->results);
83+
84+
$this->assertTrue(is_array($requests = $this->geocoderLogger->getRequests()));
85+
$this->assertCount(1, $requests);
86+
$this->assertTrue(is_array($request = $requests[0]));
87+
$this->assertSame($request['value'], 'copenhagen');
88+
$this->assertSame($request['duration'], 0.123);
89+
$this->assertSame($request['providerClass'], 'FooProvider');
90+
$this->assertSame($request['result'], '[{"latitude":1,"longitude":2,"bounds":null,"streetNumber":null,"streetName":null,"zipcode":null,"city":null,"cityDistrict":null,"county":null,"countyCode":null,"region":null,"regionCode":null,"country":null,"countryCode":null,"timezone":null},{"latitude":3,"longitude":4,"bounds":null,"streetNumber":null,"streetName":null,"zipcode":null,"city":null,"cityDistrict":null,"county":null,"countyCode":null,"region":null,"regionCode":null,"country":null,"countryCode":null,"timezone":null}]');
91+
$this->assertCount(2, json_decode($request['result']));
92+
}
93+
94+
public function testLog2RequetsWithMultipleResults()
95+
{
96+
$this->geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->results);
97+
$this->geocoderLogger->logRequest('paris', 0.456, 'BarProvider', $this->results);
98+
99+
$this->assertTrue(is_array($requests = $this->geocoderLogger->getRequests()));
100+
$this->assertCount(2, $requests);
101+
102+
$this->assertTrue(is_array($request = $requests[0]));
103+
$this->assertSame($request['value'], 'copenhagen');
104+
$this->assertCount(2, json_decode($request['result']));
105+
106+
$this->assertTrue(is_array($request = $requests[1]));
107+
$this->assertSame($request['value'], 'paris');
108+
$this->assertCount(2, json_decode($request['result']));
109+
}
110+
111+
public function testLogRequestsWithoutLogger()
112+
{
113+
$geocoderLogger = new GeocoderLogger;
114+
$geocoderLogger->logRequest('copenhagen', 0.123, 'FooProvider', $this->result);
115+
116+
$this->assertTrue(is_array($requests = $geocoderLogger->getRequests()));
117+
$this->assertCount(1, $requests);
118+
$this->assertTrue(is_array($request = $requests[0]));
119+
$this->assertSame($request['value'], 'copenhagen');
120+
$this->assertSame($request['duration'], 0.123);
121+
$this->assertSame($request['providerClass'], 'FooProvider');
122+
$this->assertSame($request['result'], '{"latitude":1,"longitude":2,"bounds":null,"streetNumber":null,"streetName":null,"zipcode":null,"city":null,"cityDistrict":null,"county":null,"countyCode":null,"region":null,"regionCode":null,"country":null,"countryCode":null,"timezone":null}');
123+
}
124+
}

0 commit comments

Comments
 (0)