TestControllerTest::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * (c) webfactory GmbH <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Webfactory\Bundle\ExceptionsBundle\Tests\Controller;
11
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\HttpKernel\Exception\HttpException;
14
use Webfactory\Bundle\ExceptionsBundle\Controller\TestController;
15
16
/**
17
 * Tests for the TestController.
18
 */
19
class TestControllerTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * System under test.
23
     *
24
     * @var TestController
25
     */
26
    private $controller;
27
28
    /**
29
     * Mocked container of the TestController.
30
     *
31
     * @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    private $container;
34
35
    /**
36
     * @see PHPUnit_Framework_TestCase::setUp()
37
     */
38
    protected function setUp()
39
    {
40
        $this->controller = new TestController();
41
        $this->container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerInterface');
42
        $this->controller->setContainer($this->container);
43
        parent::setUp();
44
    }
45
46
    /**
47
     * @see PHPUnit_Framework_TestCase::tearDown()
48
     */
49
    protected function tearDown()
50
    {
51
        $this->container = null;
52
        $this->controller = null;
53
        parent::tearDown();
54
    }
55
56
    /**
57
     * Ensures that testErrorPageAction() throws a HttpException having the passed code as it's status code.
58
     */
59
    public function testTestErrorPageActionThrowsHttpExceptionWithPassedCode()
60
    {
61
        $expectedExceptionWasThrown = false;
62
        $expectedCode = 404;
63
64
        // $this->setExpectedException() would be much shorter but to expect a certain status code, one would also have
65
        // to expect a certain message, which we don't
66
        try {
67
            $this->controller->testErrorPageAction($expectedCode);
68
        } catch (HttpException $exception) {
69
            if ($expectedCode === 404) {
70
                $expectedExceptionWasThrown = true;
71
            }
72
        }
73
74
        if ($expectedExceptionWasThrown === false) {
75
            $message = 'Either the thrown HttpException had not the correct code (' . $expectedCode . '), or no '
76
                     . 'HttpException was thrown at all.';
77
            $this->fail($message);
78
        }
79
    }
80
81
    /**
82
     * Ensures that testErrorPageAction() does not call setDebug(false) if the Twig Controller retrieved from the
83
     * container is not an ExceptionsBundle's ExceptionController (because it probably has no setDebug or it has another
84
     * meaning).
85
     */
86
    public function testTestErrorPageActionDoesNotSetDebugToFalseForTwigControllerThatsNotAnExceptionController()
87
    {
88
        $notAnExceptionController = $this->getMock('stdClass')
89
                                         ->expects($this->never())
90
                                         ->method('setDebug');
91
        $this->container->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Depend...tion\ContainerInterface.

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...
92
                        ->method('get')
93
                        ->with('twig.controller.exception')
94
                        ->will($this->returnValue($notAnExceptionController));
95
        $this->setExpectedException('Symfony\Component\HttpKernel\Exception\HttpException');
96
97
        $this->controller->testErrorPageAction(404);
98
    }
99
100
    /**
101
     * Ensures that testErrorPageAction() calls setDebug(false) if the Twig Controller retrieved from the container is
102
     * an ExceptionsBundle's ExceptionController.
103
     */
104
    public function testTestErrorPageActionSetsDebugToFalseForTwigExceptionController()
105
    {
106
        $mockedClass = 'Webfactory\Bundle\ExceptionsBundle\Controller\ExceptionController';
107
        $exceptionController = $this->getMockBuilder($mockedClass)
108
                                    ->disableOriginalConstructor()
109
                                    ->getMock();
110
        $exceptionController->expects($this->atLeastOnce())
111
                            ->method('setDebug')
112
                            ->with(false);
113
        $this->container->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Depend...tion\ContainerInterface.

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...
114
                        ->method('get')
115
                        ->with('twig.controller.exception')
116
                        ->will($this->returnValue($exceptionController));
117
        $this->setExpectedException('Symfony\Component\HttpKernel\Exception\HttpException');
118
119
        $this->controller->testErrorPageAction(404);
120
    }
121
}
122