Completed
Push — master ( 618a28...d2b433 )
by Christophe
02:18
created

ExceptionListenerTest::testGetSubscriberEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 3
nc 1
nop 0
1
<?php
2
3
namespace Chris\Bundle\FrontRenderBundle\Tests\Listener;
4
5
use Chris\Bundle\FrontRenderBundle\Subscriber\RenderSubscriber;
6
use Phake;
7
use Phake_IMock;
8
use PHPUnit_Framework_TestCase;
9
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
11
use Twig_Environment;
12
use Twig_Loader_Filesystem;
13
14
class ExceptionListenerTest extends PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var string TEMPLATE_PATH
18
     */
19
    const TEMPLATE_PATH = 'Template';
20
21
    /**
22
     * @var Twig_Loader_Filesystem
23
     */
24
    protected $twigLoader;
25
26
    /**
27
     * @var Twig_Environment|Phake_IMock
28
     */
29
    protected $twigEnvironment;
30
31
    /**
32
     * @var GetResponseEvent
33
     */
34
    protected $responseEvent;
35
36
    /**
37
     * @var GetResponseForExceptionEvent
38
     */
39
    protected $exceptionEvent;
40
41
    /**
42
     * @var RenderSubscriber
43
     */
44
    protected $renderSubscriber;
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function setUp()
50
    {
51
        $this->twigLoader      = Phake::partialMock(Twig_Loader_Filesystem::class, [__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . self::TEMPLATE_PATH]);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::partialMock(\Twi.... self::TEMPLATE_PATH)) of type object<Phake_IMock> is incompatible with the declared type object<Twig_Loader_Filesystem> of property $twigLoader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
        $this->twigEnvironment = Phake::partialMock(Twig_Environment::class, $this->twigLoader);
53
        $this->responseEvent   = Phake::mock(GetResponseEvent::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::mock(\Symfony\Co...etResponseEvent::class) of type object<Phake_IMock> is incompatible with the declared type object<Symfony\Component...Event\GetResponseEvent> of property $responseEvent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        $this->exceptionEvent  = Phake::mock(GetResponseForExceptionEvent::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phake::mock(\Symfony\Co...rExceptionEvent::class) of type object<Phake_IMock> is incompatible with the declared type object<Symfony\Component...ponseForExceptionEvent> of property $exceptionEvent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
56
        $this->renderSubscriber = new RenderSubscriber($this->twigEnvironment);
0 ignored issues
show
Documentation introduced by
$this->twigEnvironment is of type object<Phake_IMock>, but the function expects a object<Twig_Environment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
    }
58
59
    /**
60
     * Test we have register events subscribed
61
     */
62
    public function testGetSubscriberEvent()
63
    {
64
        $eventsSubscribed = RenderSubscriber::getSubscribedEvents();
65
66
        $this->assertInternalType('array', $eventsSubscribed);
67
    }
68
69
    /**
70
     * Test the lexer update to have a custom lexer for the front
71
     */
72
    public function testSetLexerForTheFront()
73
    {
74
        $this->renderSubscriber->updateTagTwig($this->responseEvent);
75
76
        Phake::verify($this->twigEnvironment)->setLexer(Phake::anyParameters());
0 ignored issues
show
Bug introduced by
It seems like $this->twigEnvironment can also be of type object<Twig_Environment>; however, Phake::verify() does only seem to accept object<Phake_IMock>, 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...
77
    }
78
79
    /**
80
     * Test the lexer update to have the default lexer for exception
81
     */
82
    public function testSetLexerForException()
83
    {
84
        $this->renderSubscriber->updateTagTwig($this->responseEvent);
85
        $this->renderSubscriber->onKernelException($this->exceptionEvent);
86
87
        Phake::verify($this->twigEnvironment, Phake::times(2))->setLexer(Phake::anyParameters());
0 ignored issues
show
Bug introduced by
It seems like $this->twigEnvironment can also be of type object<Twig_Environment>; however, Phake::verify() does only seem to accept object<Phake_IMock>, 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...
88
    }
89
}
90