Completed
Push — master ( 3a69fd...c72125 )
by Florian
02:24
created

JsonParserTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testParseJsonFixture() 0 29 1
A getFinderMock() 0 6 1
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();
14
        $finder->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Laposte\DatanovaBundle\Service\Finder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
15
            ->method('findDataset')
16
            ->with($dataset, JsonParser::FORMAT)
17
            ->willReturn($path);
18
        $finder->expects($this->once())
19
            ->method('getContent')
20
            ->with($path)
21
            ->willReturn(file_get_contents($path));
22
23
        $jsonParser = new JsonParser($finder);
0 ignored issues
show
Bug introduced by
It seems like $finder defined by $this->getFinderMock() 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...
24
25
        $result = $jsonParser->parse($dataset);
26
        $this->assertNotFalse($result);
27
        $this->assertCount(15, $result);
28
29
        $this->assertArrayHasKey('code_commune_insee', $result[0]);
30
        $this->assertArrayHasKey('nom_de_la_commune', $result[0]);
31
        $this->assertArrayHasKey('code_postal', $result[0]);
32
        $this->assertArrayHasKey('libell_d_acheminement', $result[0]);
33
        $this->assertEquals('57077', $result[0]['code_commune_insee']);
34
        $this->assertEquals('BEZANGE LA PETITE', $result[0]['nom_de_la_commune']);
35
        $this->assertEquals('57630', $result[0]['code_postal']);
36
        $this->assertEquals('BEZANGE LA PETITE', $result[0]['libell_d_acheminement']);
37
    }
38
39
    /**
40
     * @return \PHPUnit_Framework_MockObject_MockObject|\Laposte\DatanovaBundle\Service\Finder
41
     */
42
    private function getFinderMock()
43
    {
44
        return $this->getMockBuilder('Laposte\DatanovaBundle\Service\Finder')
45
            ->disableOriginalConstructor()
46
            ->getMock();
47
    }
48
}
49