Completed
Push — master ( 2bcd1f...1057dc )
by Tom
12s
created

YumlControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModuleTest\Yuml;
21
22
use DoctrineORMModule\Yuml\YumlController;
23
use PHPUnit\Framework\TestCase;
24
25
/**
26
 * Tests for Yuml redirector controller
27
 *
28
 * @license MIT
29
 * @link    http://www.doctrine-project.org/
30
 * @author  Marco Pivetta <[email protected]>
31
 */
32
class YumlControllerTest extends TestCase
33
{
34
    /**
35
     * @var YumlController
36
     */
37
    protected $controller;
38
39
    /**
40
     * @var \Zend\Http\Client|\PHPUnit_Framework_MockObject_MockObject
41
     */
42
    protected $httpClient;
43
44
    /**
45
     * @var \Zend\Mvc\Controller\PluginManager|\PHPUnit_Framework_MockObject_MockObject
46
     */
47
    protected $pluginManager;
48
49
    /**
50
     * {@inheritDoc}
51
     *
52
     * @covers \DoctrineORMModule\Yuml\YumlController::__construct
53
     */
54
    public function setUp()
55
    {
56
        $this->httpClient     = $this->createMock(\Zend\Http\Client::class);
57
        $this->controller     = new YumlController($this->httpClient);
58
        $this->pluginManager  = $this->getMockBuilder(\Zend\Mvc\Controller\PluginManager::class)
59
            ->disableOriginalConstructor()
60
            ->getMock();
61
        $this->controller->setPluginManager($this->pluginManager);
62
    }
63
64
    /**
65
     * @covers \DoctrineORMModule\Yuml\YumlController::indexAction
66
     */
67
    public function testIndexActionWillRedirectToYuml()
68
    {
69
        $response = $this->createMock(\Zend\Http\Response::class);
70
        $controllerResponse = $this->createMock(\Zend\Http\Response::class);
71
        $redirect = $this->createMock(\Zend\Mvc\Controller\Plugin\Redirect::class);
72
        $this->httpClient->expects($this->any())->method('send')->will($this->returnValue($response));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\Http\Client.

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...
73
        $response->expects($this->any())->method('isSuccess')->will($this->returnValue(true));
74
        $response->expects($this->any())->method('getBody')->will($this->returnValue('short-url'));
75
        $this
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\Mvc\Controller\PluginManager.

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...
76
            ->pluginManager
77
            ->expects($this->any())
78
            ->method('get')->with('redirect')
79
            ->will($this->returnValue($redirect));
80
        $redirect
81
            ->expects($this->any())
82
            ->method('toUrl')
83
            ->with('https://yuml.me/short-url')
84
            ->will($this->returnValue($controllerResponse));
85
86
        $this->assertSame($controllerResponse, $this->controller->indexAction());
87
    }
88
89
    /**
90
     * @covers \DoctrineORMModule\Yuml\YumlController::indexAction
91
     */
92
    public function testIndexActionWillFailOnMalformedResponse()
93
    {
94
        $response = $this->createMock(\Zend\Http\Response::class);
95
        $this->httpClient->expects($this->any())->method('send')->will($this->returnValue($response));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\Http\Client.

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...
96
        $response->expects($this->any())->method('isSuccess')->will($this->returnValue(false));
97
98
        $this->expectException(\UnexpectedValueException::class);
99
        $this->controller->indexAction();
100
    }
101
}
102