Completed
Push — master ( 666f26...e51d97 )
by Florian
02:35
created

DownloaderTest::getClientMock()   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\Service\Downloader;
6
7
class DownloaderTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * Simple find download test
11
     */
12
    public function testFindDownload()
13
    {
14
        $dataset = 'laposte_hexasmal';
15
        $format = 'json';
16
        $filter = '34000';
17
        $path = uniqid();
18
        $client = $this->getClientMock();
19
        $finder = $this->getFinderMock();
20
        $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...
21
            ->method('findDataset')
22
            ->with($dataset, $format, $filter)
23
            ->willReturn($path);
24
        $downloader = new Downloader($client, $finder);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getClientMock() on line 18 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Laposte\DatanovaBundle\S...wnloader::__construct() does only seem to accept object<Laposte\DatanovaB...Client\ClientInterface>, 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...
Bug introduced by
It seems like $finder defined by $this->getFinderMock() on line 19 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Laposte\DatanovaBundle\S...wnloader::__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...
25
        $result = $downloader->findDownload($dataset, $format, $filter);
26
        $this->assertEquals($path, $result);
27
    }
28
29
    /**
30
     * @return \PHPUnit_Framework_MockObject_MockObject|\Laposte\DatanovaBundle\Client\ClientInterface
31
     */
32
    private function getClientMock()
33
    {
34
        return $this->getMockBuilder('Laposte\DatanovaBundle\Client\ClientInterface')
35
            ->disableOriginalConstructor()
36
            ->getMock();
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
    /**
50
     * Simple test of download method
51
     */
52
    public function testDownload()
53
    {
54
        $dataset = 'laposte_hexasmal';
55
        $format = 'json';
56
        $filter = '34000';
57
        $updateExisting = false;
58
        $content = uniqid();
59
        $path = uniqid();
60
        $client = $this->getClientMock();
61
        $client->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\Client\ClientInterface.

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...
62
            ->method('setTimeout')
63
            ->with(0);
64
        $client->expects($this->once())
65
            ->method('get')
66
            ->with('download', array(
67
                'dataset' => $dataset,
68
                'format' => $format,
69
                'q' => $filter
70
            ))
71
            ->willReturn($content)
72
        ;
73
        $finder = $this->getFinderMock();
74
        $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...
75
            ->method('save')
76
            ->with($dataset, $content, $format, $filter, $updateExisting)
77
            ->willReturn($path);
78
        $finder->expects($this->once())
79
            ->method('getContent')
80
            ->with($path)
81
            ->willReturn($content);
82
        $downloader = new Downloader($client, $finder);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getClientMock() on line 60 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Laposte\DatanovaBundle\S...wnloader::__construct() does only seem to accept object<Laposte\DatanovaB...Client\ClientInterface>, 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...
Bug introduced by
It seems like $finder defined by $this->getFinderMock() on line 73 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Laposte\DatanovaBundle\S...wnloader::__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...
83
        $result = $downloader->download($dataset, $format, $filter, $updateExisting);
84
        $this->assertEquals($content, $result);
85
    }
86
}
87