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 | public function testName(): void |
||
114 | { |
||
115 | // arrange |
||
116 | $extension = $this->createCookieGuardExtension(); |
||
117 | |||
118 | // act && assert |
||
119 | Assert::assertEquals(CookieGuardExtension::class, $extension->getName()); |
||
120 | } |
||
121 | |||
122 | private function createRequestWithCookie(): Request |
||
123 | { |
||
124 | return new Request([], [], [], [ |
||
125 | self::COOKIE_NAME => true |
||
126 | ]); |
||
127 | } |
||
128 | |||
129 | private function createCookieGuardExtension(RequestStack $requestStack = null, MockObject $twigEnvironment = null): CookieGuardExtension |
||
130 | { |
||
131 | return new CookieGuardExtension( |
||
132 | $requestStack ?? $this->createRequestStackMock(), |
||
133 | $twigEnvironment ?? $this->createTwigEnvironmentMock(), |
||
134 | self::COOKIE_NAME |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | private function createRequestStackMock(Request $request = null): RequestStack |
||
139 | { |
||
140 | $requestStack = new RequestStack(); |
||
141 | $requestStack->push($request ?? Request::createFromGlobals()); |
||
142 | |||
143 | return $requestStack; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return MockObject|Environment |
||
148 | */ |
||
149 | private function createTwigEnvironmentMock(string $content = 'cookie not accepted', bool $show = false): MockObject |
||
150 | { |
||
151 | $twigEnvironmentMock = $this->getMockBuilder(Environment::class) |
||
0 ignored issues
–
show
|
|||
152 | ->disableOriginalConstructor() |
||
153 | ->setMethods([ |
||
154 | 'render' |
||
155 | ]) |
||
156 | ->getMock(); |
||
157 | |||
158 | if (!$show) { |
||
159 | $content = sprintf('<meta class="js-cookie-guarded" data-content="%s" />', $content); |
||
160 | } |
||
161 | |||
162 | $twigEnvironmentMock |
||
163 | ->method('render') |
||
164 | ->willReturn($content); |
||
165 | |||
166 | return $twigEnvironmentMock; |
||
167 | } |
||
168 | } |
||
169 |
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.