Completed
Push — master ( 1b0014...e530af )
by Tobias
03:11
created

TimedGeocoderTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 64.1 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 50
loc 78
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Tests;
14
15
use Geocoder\Model\AddressCollection;
16
use Geocoder\Provider\Provider;
17
use Geocoder\TimedGeocoder;
18
use PHPUnit\Framework\TestCase;
19
use Symfony\Component\Stopwatch\Stopwatch;
20
21
class TimedGeocoderTest extends TestCase
22
{
23
    /**
24
     * @var Stopwatch
25
     */
26
    private $stopwatch;
27
28
    /**
29
     * @var Provider
30
     */
31
    private $delegate;
32
33
    /**
34
     * @var TimedGeocoder
35
     */
36
    private $geocoder;
37
38
    protected function setUp()
39
    {
40
        $this->stopwatch = new Stopwatch();
41
        $this->delegate = $this->getMockBuilder(Provider::class)->getMock();
42
        $this->geocoder = new TimedGeocoder($this->delegate, $this->stopwatch);
43
    }
44
45
    public function testGeocode()
46
    {
47
        $this->delegate->expects($this->once())
48
             ->method('geocodeQuery')
49
             ->will($this->returnValue(new AddressCollection([])));
50
51
        $this->geocoder->geocode('foo');
52
53
        $this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
54
    }
55
56
    public function testGeocodeThrowsException()
57
    {
58
        $this->delegate->expects($this->once())
59
             ->method('geocodeQuery')
60
             ->will($this->throwException($exception = new \Exception()));
61
62
        try {
63
            $this->geocoder->geocode('foo');
64
            $this->fail('Geocoder::geocode should throw an exception');
65
        } catch (\Exception $e) {
66
            $this->assertSame($exception, $e);
67
        }
68
69
        $this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
70
    }
71
72
    public function testReverse()
73
    {
74
        $this->delegate->expects($this->once())
75
             ->method('reverseQuery')
76
             ->will($this->returnValue(new AddressCollection([])));
77
78
        $this->geocoder->reverse(0, 0);
79
80
        $this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
81
    }
82
83
    public function testReverseThrowsException()
84
    {
85
        $this->delegate->expects($this->once())
86
             ->method('reverseQuery')
87
             ->will($this->throwException($exception = new \Exception()));
88
89
        try {
90
            $this->geocoder->reverse(0, 0);
91
            $this->fail('Geocoder::reverse should throw an exception');
92
        } catch (\Exception $e) {
93
            $this->assertSame($exception, $e);
94
        }
95
96
        $this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
97
    }
98
}
99