JsonParserTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 47
wmc 2
lcom 1
cbo 3
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseJsonFixture() 0 20 1
A getFinderMock() 0 17 1
1
<?php
2
3
namespace Fmaj\LaposteDatanovaBundle\Tests\Model;
4
5
use Fmaj\LaposteDatanovaBundle\Parser\JsonParser;
6
7
class JsonParserTest extends \PHPUnit_Framework_TestCase
8
{
9
    public function testParseJsonFixture()
10
    {
11
        $dataset = 'laposte_hexasmal';
12
        $path = dirname(__FILE__) . '/Fixtures/laposte_hexasmal.json';
13
        $finder = $this->getFinderMock($dataset, $path);
14
15
        $jsonParser = new JsonParser($finder);
0 ignored issues
show
Bug introduced by
It seems like $finder defined by $this->getFinderMock($dataset, $path) on line 13 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Fmaj\LaposteDatanovaBund...onParser::__construct() does only seem to accept object<Fmaj\LaposteDatanovaBundle\Service\Finder>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
16
        $result = $jsonParser->parse($dataset);
17
18
        $this->assertNotFalse($result);
19
        $this->assertCount(15, $result);
20
        $this->assertArrayHasKey('code_commune_insee', $result[0]);
21
        $this->assertArrayHasKey('nom_de_la_commune', $result[0]);
22
        $this->assertArrayHasKey('code_postal', $result[0]);
23
        $this->assertArrayHasKey('libell_d_acheminement', $result[0]);
24
        $this->assertEquals('57077', $result[0]['code_commune_insee']);
25
        $this->assertEquals('BEZANGE LA PETITE', $result[0]['nom_de_la_commune']);
26
        $this->assertEquals('57630', $result[0]['code_postal']);
27
        $this->assertEquals('BEZANGE LA PETITE', $result[0]['libell_d_acheminement']);
28
    }
29
30
    /**
31
     * @param string $dataset
32
     * @param string $path
33
     *
34
     * @return \PHPUnit_Framework_MockObject_MockObject|\Fmaj\LaposteDatanovaBundle\Service\Finder
35
     */
36
    private function getFinderMock($dataset, $path)
37
    {
38
        $finder = $this->getMockBuilder('Fmaj\LaposteDatanovaBundle\Service\Finder')
39
            ->disableOriginalConstructor()
40
            ->getMock();
41
42
        $finder->expects($this->once())
43
            ->method('findDataset')
44
            ->with($dataset, JsonParser::FORMAT)
45
            ->willReturn($path);
46
        $finder->expects($this->once())
47
            ->method('getContent')
48
            ->with($path)
49
            ->willReturn(file_get_contents($path));
50
51
        return $finder;
52
    }
53
}
54