summaryrefslogtreecommitdiff
path: root/tests/Unit/Renderer/Twig/Extensions/CsrfTest.php
blob: f81f210a72ddf291319a2c3ea9509de945091588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;

use Engelsystem\Renderer\Twig\Extensions\Csrf;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class CsrfTest extends ExtensionTest
{
    /**
     * @covers \Engelsystem\Renderer\Twig\Extensions\Csrf::getFunctions
     */
    public function testGetGlobals()
    {
        /** @var SessionInterface|MockObject $session */
        $session = $this->createMock(SessionInterface::class);

        $extension = new Csrf($session);
        $functions = $extension->getFunctions();

        $this->assertExtensionExists('csrf', [$extension, 'getCsrfField'], $functions, ['is_safe' => ['html']]);
        $this->assertExtensionExists('csrf_token', [$extension, 'getCsrfToken'], $functions);
    }

    /**
     * @covers \Engelsystem\Renderer\Twig\Extensions\Csrf::getCsrfField
     */
    public function testGetCsrfField()
    {
        /** @var Csrf|MockObject $extension */
        $extension = $this->getMockBuilder(Csrf::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['getCsrfToken'])
            ->getMock();

        $extension->expects($this->once())
            ->method('getCsrfToken')
            ->willReturn('SomeRandomCsrfToken');

        $this->assertEquals(
            '<input type="hidden" name="_token" value="SomeRandomCsrfToken">',
            $extension->getCsrfField()
        );
    }

    /**
     * @covers \Engelsystem\Renderer\Twig\Extensions\Csrf::__construct
     * @covers \Engelsystem\Renderer\Twig\Extensions\Csrf::getCsrfToken
     */
    public function testGetCsrfToken()
    {
        /** @var SessionInterface|MockObject $session */
        $session = $this->createMock(SessionInterface::class);
        $session->expects($this->once())
            ->method('get')
            ->with('_token')
            ->willReturn('SomeOtherCsrfToken');

        $extension = new Csrf($session);
        $this->assertEquals('SomeOtherCsrfToken', $extension->getCsrfToken());
    }
}