Test Setup Failed
Pull Request — develop (#17)
by Evert
04:12
created

CookieGuardExtensionTest::testName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace FH\Bundle\CookieGuardBundle\Tests\Twig;
5
6
use FH\Bundle\CookieGuardBundle\Twig\CookieGuardExtension;
7
use PHPUnit\Framework\Assert;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
use Twig\Environment;
13
use Twig\TwigFilter;
14
use Twig\TwigFunction;
15
16
/**
17
 * @author Evert Harmeling <[email protected]>
18
 */
19
final class CookieGuardExtensionTest extends TestCase
20
{
21
    private const COOKIE_NAME = 'test-cookie';
22
23
    public function testFilterExistence(): void
24
    {
25
        // arrange
26
        $extension = $this->createCookieGuardExtension();
27
28
        $filterNames = [];
29
        /** @var TwigFilter $filter */
30
        foreach ($extension->getFilters() as $filter) {
31
            Assert::assertInstanceOf(TwigFilter::class, $filter);
32
            $filterNames[] = $filter->getName();
33
        }
34
35
        Assert::assertContains('cookie_guard', $filterNames);
36
    }
37
38
    public function testFunctionExistence(): void
39
    {
40
        // arrange
41
        $extension = $this->createCookieGuardExtension();
42
43
        $functionNames = [];
44
        /** @var TwigFunction $filter */
45
        foreach ($extension->getFunctions() as $function) {
46
            Assert::assertInstanceOf(TwigFunction::class, $function);
47
            $functionNames[] = $function->getName();
48
        }
49
50
        Assert::assertContains('cookie_settings_submitted', $functionNames);
51
        Assert::assertContains('cookie_settings_accepted', $functionNames);
52
    }
53
54
    public function testCookieSettingsAreNotAccepted(): void
55
    {
56
        // arrange
57
        $extension = $this->createCookieGuardExtension();
58
59
        // act && assert
60
        Assert::assertFalse($extension->cookieSettingsAreAccepted());
61
    }
62
63
    public function testCookieSettingsAreAccepted(): void
64
    {
65
        // arrange
66
        $request = $this->createRequestWithCookie();
67
        $extension = $this->createCookieGuardExtension($this->createRequestStackMock($request));
68
69
        // act && assert
70
        Assert::assertTrue($extension->cookieSettingsAreAccepted());
71
    }
72
73
    public function testCookieSettingsAreNotSubmitted(): void
74
    {
75
        // arrange
76
        $extension = $this->createCookieGuardExtension();
77
78
        // act && assert
79
        Assert::assertFalse($extension->cookieSettingsSubmitted());
80
    }
81
82
    public function testCookieSettingsAreSubmitted(): void
83
    {
84
        // arrange
85
        $request = $this->createRequestWithCookie();
86
        $extension = $this->createCookieGuardExtension($this->createRequestStackMock($request));
87
88
        // act && assert
89
        Assert::assertTrue($extension->cookieSettingsSubmitted());
90
    }
91
92
    public function testShowIfCookieIsNotAccepted(): void
93
    {
94
        // arrange
95
        $extension = $this->createCookieGuardExtension();
96
97
        // act && assert
98
        Assert::assertEquals(
99
            sprintf('<meta class="js-cookie-guarded" data-content="%s" />', 'cookie not accepted'),
100
            $extension->showIfCookieAccepted('cookie not accepted')
101
        );
102
    }
103
104
    public function testShowIfCookieAccepted(): void
105
    {
106
        // arrange
107
        $extension = $this->createCookieGuardExtension(null, $this->createTwigEnvironmentMock('cookie accepted', true));
108
109
        // act && assert
110
        Assert::assertEquals('cookie accepted', $extension->showIfCookieAccepted('cookie accepted'));
111
    }
112
113
    private function createRequestWithCookie(): Request
114
    {
115
        return new Request([], [], [], [
116
            self::COOKIE_NAME => true
117
        ]);
118
    }
119
120
    private function createCookieGuardExtension(RequestStack $requestStack = null, MockObject $twigEnvironment = null): CookieGuardExtension
121
    {
122
        return new CookieGuardExtension(
123
            $requestStack ?? $this->createRequestStackMock(),
124
            $twigEnvironment ?? $this->createTwigEnvironmentMock(),
125
            self::COOKIE_NAME
126
        );
127
    }
128
129
    private function createRequestStackMock(Request $request = null): RequestStack
130
    {
131
        $requestStack = new RequestStack();
132
        $requestStack->push($request ?? Request::createFromGlobals());
133
134
        return $requestStack;
135
    }
136
137
    /**
138
     * @return MockObject|Environment
139
     */
140
    private function createTwigEnvironmentMock(string $content = 'cookie not accepted', bool $show = false): MockObject
141
    {
142
        $twigEnvironmentMock = $this->getMockBuilder(Environment::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

142
        $twigEnvironmentMock = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(Environment::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
143
            ->disableOriginalConstructor()
144
            ->setMethods([
145
                'render'
146
            ])
147
            ->getMock();
148
149
        if (!$show) {
150
            $content = sprintf('<meta class="js-cookie-guarded" data-content="%s" />', $content);
151
        }
152
153
        $twigEnvironmentMock
154
            ->method('render')
155
            ->willReturn($content);
156
157
        return $twigEnvironmentMock;
158
    }
159
}
160