Skip to content

Commit 5fce095

Browse files
committed
Fix timed geocoder with exception flow
1 parent 5211e23 commit 5fce095

File tree

2 files changed

+82
-32
lines changed

2 files changed

+82
-32
lines changed

src/Geocoder/TimedGeocoder.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ public function __construct(Geocoder $delegate, Stopwatch $stopwatch)
3535
public function geocode($value)
3636
{
3737
$this->stopwatch->start('geocode', 'geocoder');
38-
$result = $this->delegate->geocode($value);
38+
39+
try {
40+
$result = $this->delegate->geocode($value);
41+
} catch (\Exception $e) {
42+
$this->stopwatch->stop('geocode');
43+
44+
throw $e;
45+
}
46+
3947
$this->stopwatch->stop('geocode');
4048

4149
return $result;
@@ -47,7 +55,15 @@ public function geocode($value)
4755
public function reverse($latitude, $longitude)
4856
{
4957
$this->stopwatch->start('reverse', 'geocoder');
50-
$result = $this->delegate->reverse($latitude, $longitude);
58+
59+
try {
60+
$result = $this->delegate->reverse($latitude, $longitude);
61+
} catch (\Exception $e) {
62+
$this->stopwatch->stop('reverse');
63+
64+
throw $e;
65+
}
66+
5167
$this->stopwatch->stop('reverse');
5268

5369
return $result;

tests/Geocoder/Tests/TimedGeocoderTest.php

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,68 @@
77

88
class TimedGeocoderTest extends TestCase
99
{
10-
protected function setUp()
11-
{
12-
$this->stopwatch = new Stopwatch();
13-
$this->delegate = $this->getMock('Geocoder\Geocoder');
14-
$this->geocoder = new TimedGeocoder($this->delegate, $this->stopwatch);
15-
}
16-
17-
public function testGeocode()
18-
{
19-
$this->delegate->expects($this->once())
20-
->method('geocode')
21-
->with($this->equalTo('foo'))
22-
->will($this->returnValue([]));
23-
24-
$this->geocoder->geocode('foo');
25-
26-
$this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
27-
}
28-
29-
public function testReverse()
30-
{
31-
$this->delegate->expects($this->once())
32-
->method('reverse')
33-
->with($this->equalTo(0, 0))
34-
->will($this->returnValue([]));
35-
36-
$this->geocoder->reverse(0, 0);
37-
38-
$this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
39-
}
10+
protected function setUp()
11+
{
12+
$this->stopwatch = new Stopwatch();
13+
$this->delegate = $this->getMock('Geocoder\Geocoder');
14+
$this->geocoder = new TimedGeocoder($this->delegate, $this->stopwatch);
15+
}
16+
17+
public function testGeocode()
18+
{
19+
$this->delegate->expects($this->once())
20+
->method('geocode')
21+
->with($this->equalTo('foo'))
22+
->will($this->returnValue([]));
23+
24+
$this->geocoder->geocode('foo');
25+
26+
$this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
27+
}
28+
29+
public function testGeocodeThrowsException()
30+
{
31+
$this->delegate->expects($this->once())
32+
->method('geocode')
33+
->with($this->equalTo('foo'))
34+
->will($this->throwException($exception = new \Exception()));
35+
36+
try {
37+
$this->geocoder->geocode('foo');
38+
$this->fail('Geocoder::geocode should throw an exception');
39+
} catch (\Exception $e) {
40+
$this->assertSame($exception, $e);
41+
}
42+
43+
$this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
44+
}
45+
46+
public function testReverse()
47+
{
48+
$this->delegate->expects($this->once())
49+
->method('reverse')
50+
->with($this->equalTo(0, 0))
51+
->will($this->returnValue([]));
52+
53+
$this->geocoder->reverse(0, 0);
54+
55+
$this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
56+
}
57+
58+
public function testReverseThrowsException()
59+
{
60+
$this->delegate->expects($this->once())
61+
->method('reverse')
62+
->with($this->equalTo(0, 0))
63+
->will($this->throwException($exception = new \Exception()));
64+
65+
try {
66+
$this->geocoder->reverse(0, 0);
67+
$this->fail('Geocoder::reverse should throw an exception');
68+
} catch (\Exception $e) {
69+
$this->assertSame($exception, $e);
70+
}
71+
72+
$this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
73+
}
4074
}

0 commit comments

Comments
 (0)