Completed
Pull Request — master (#508)
by Oliver
02:47
created

YumlControllerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 70
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testIndexActionWillRedirectToYuml() 0 21 1
A testIndexActionWillFailOnMalformedResponse() 0 9 1
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
24
/**
25
 * Tests for Yuml redirector controller
26
 *
27
 * @license MIT
28
 * @link    http://www.doctrine-project.org/
29
 * @author  Marco Pivetta <[email protected]>
30
 */
31
class YumlControllerTest extends \PHPUnit_Framework_TestCase
32
{
33
    /**
34
     * @var YumlController
35
     */
36
    protected $controller;
37
38
    /**
39
     * @var \Zend\Http\Client|\PHPUnit_Framework_MockObject_MockObject
40
     */
41
    protected $httpClient;
42
43
    /**
44
     * @var \Zend\Mvc\Controller\PluginManager|\PHPUnit_Framework_MockObject_MockObject
45
     */
46
    protected $pluginManager;
47
48
    /**
49
     * {@inheritDoc}
50
     *
51
     * @covers \DoctrineORMModule\Yuml\YumlController::__construct
52
     */
53
    public function setUp()
54
    {
55
        $this->httpClient     = $this->createMock(\Zend\Http\Client::class);
56
        $this->controller     = new YumlController($this->httpClient);
57
        $this->pluginManager  = $this->getMockBuilder(\Zend\Mvc\Controller\PluginManager::class)
58
            ->disableOriginalConstructor()
59
            ->getMock();
60
        $this->controller->setPluginManager($this->pluginManager);
61
    }
62
63
    /**
64
     * @covers \DoctrineORMModule\Yuml\YumlController::indexAction
65
     */
66
    public function testIndexActionWillRedirectToYuml()
67
    {
68
        $response = $this->createMock(\Zend\Http\Response::class);
69
        $controllerResponse = $this->createMock(\Zend\Http\Response::class);
70
        $redirect = $this->createMock(\Zend\Mvc\Controller\Plugin\Redirect::class);
71
        $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...
72
        $response->expects($this->any())->method('isSuccess')->will($this->returnValue(true));
73
        $response->expects($this->any())->method('getBody')->will($this->returnValue('short-url'));
74
        $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...
75
            ->pluginManager
76
            ->expects($this->any())
77
            ->method('get')->with('redirect')
78
            ->will($this->returnValue($redirect));
79
        $redirect
80
            ->expects($this->any())
81
            ->method('toUrl')
82
            ->with('http://yuml.me/short-url')
83
            ->will($this->returnValue($controllerResponse));
84
85
        $this->assertSame($controllerResponse, $this->controller->indexAction());
86
    }
87
88
    /**
89
     * @covers \DoctrineORMModule\Yuml\YumlController::indexAction
90
     */
91
    public function testIndexActionWillFailOnMalformedResponse()
92
    {
93
        $response = $this->createMock(\Zend\Http\Response::class);
94
        $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...
95
        $response->expects($this->any())->method('isSuccess')->will($this->returnValue(false));
96
97
        $this->expectException(\UnexpectedValueException::class);
98
        $this->controller->indexAction();
99
    }
100
}
101