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

CsvParserTest::getFinderMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Laposte\DatanovaBundle\Tests\Model;
4
5
use Laposte\DatanovaBundle\Parser\CsvParser;
6
7
class CsvParserTest extends \PHPUnit_Framework_TestCase
8
{
9
    public function testParseCsvFixture()
10
    {
11
        $dataset = 'laposte_hexasmal';
12
        $path = dirname(__FILE__) . '/Fixtures/laposte_hexasmal.csv';
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, CsvParser::FORMAT)
17
            ->willReturn($path);
18
19
        $csvParser = new CsvParser($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...svParser::__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...
20
21
        $result = $csvParser->parse($dataset);
22
        $this->assertNotFalse($result);
23
        $this->assertCount(15, $result);
24
        $this->assertArrayHasKey('code_commune_insee', $result[0]);
25
        $this->assertArrayHasKey('nom_commune', $result[0]);
26
        $this->assertArrayHasKey('code_postal', $result[0]);
27
        $this->assertArrayHasKey('libelle_acheminement', $result[0]);
28
        $this->assertArrayHasKey('ligne_5', $result[0]);
29
        $this->assertEquals('57077', $result[0]['code_commune_insee']);
30
        $this->assertEquals('BEZANGE LA PETITE', $result[0]['nom_commune']);
31
        $this->assertEquals('57630', $result[0]['code_postal']);
32
        $this->assertEquals('BEZANGE LA PETITE', $result[0]['libelle_acheminement']);
33
        $this->assertEquals('', $result[0]['ligne_5']);
34
    }
35
36
    /**
37
     * @return \PHPUnit_Framework_MockObject_MockObject|\Laposte\DatanovaBundle\Service\Finder
38
     */
39
    private function getFinderMock()
40
    {
41
        return $this->getMockBuilder('Laposte\DatanovaBundle\Service\Finder')
42
            ->disableOriginalConstructor()
43
            ->getMock();
44
    }
45
}
46