Completed
Push — master ( d8fb1f...ea1baf )
by Florian
02:18
created

JsonParserTest::testParseJsonFixture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Laposte\DatanovaBundle\Tests\Model;
4
5
use Laposte\DatanovaBundle\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, Laposte\DatanovaBundle\P...onParser::__construct() does only seem to accept object<Laposte\DatanovaBundle\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|\Laposte\DatanovaBundle\Service\Finder
35
     */
36
    private function getFinderMock($dataset, $path)
37
    {
38
        $finder = $this->getMockBuilder('Laposte\DatanovaBundle\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