Skip to content

Commit c522231

Browse files
authored
Convert all tests to pest (#186)
* install Pest * refactor: CookieConsentTest * refactor: CookieConsentMiddlewareTest * feat: replace test suite in Github action and `composer.json` * fix: use Str helper class instead of str() * Fix styling Co-authored-by: alexmanase <[email protected]>
1 parent 118431b commit c522231

File tree

6 files changed

+159
-170
lines changed

6 files changed

+159
-170
lines changed

.github/workflows/run-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ jobs:
3737
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
3838
3939
- name: Execute tests
40-
run: vendor/bin/phpunit
40+
run: vendor/bin/pest

composer.json

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626
}
2727
],
2828
"require": {
29-
"php" : "^8.0",
29+
"php": "^8.0",
3030
"illuminate/support": "^8.0|^9.0",
3131
"illuminate/view": "^8.0|^9.0",
3232
"illuminate/cookie": "^8.0|^9.0",
3333
"spatie/laravel-package-tools": "^1.9"
3434
},
3535
"require-dev": {
3636
"fakerphp/faker": "^1.9",
37-
"orchestra/testbench": "^6.0|^7.0"
37+
"orchestra/testbench": "^6.0|^7.0",
38+
"pestphp/pest": "^1.22"
3839
},
3940
"autoload": {
4041
"psr-4": {
@@ -47,7 +48,7 @@
4748
}
4849
},
4950
"scripts": {
50-
"test": "vendor/bin/phpunit"
51+
"test": "vendor/bin/pest"
5152
},
5253
"extra": {
5354
"laravel": {
@@ -57,5 +58,10 @@
5758
}
5859
},
5960
"prefer-stable": true,
60-
"minimum-stability": "dev"
61-
}
61+
"minimum-stability": "dev",
62+
"config": {
63+
"allow-plugins": {
64+
"pestphp/pest-plugin": true
65+
}
66+
}
67+
}

tests/CookieConsentMiddlewareTest.php

+73-92
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,111 @@
11
<?php
22

3-
namespace Spatie\CookieConsent\Test;
4-
53
use Illuminate\Http\Request;
64
use Illuminate\Http\Response;
75
use Spatie\CookieConsent\CookieConsentMiddleware;
86

9-
class CookieConsentMiddlewareTest extends TestCase
10-
{
11-
/** @test */
12-
public function it_injects_the_if_a_closing_body_tag_is_found()
13-
{
14-
$request = new Request();
15-
16-
$middleware = new CookieConsentMiddleware();
7+
it('injects the if a closing body tag is found', function () {
8+
$request = new Request();
179

18-
$result = $middleware->handle($request, function ($request) {
19-
return (new Response())->setContent('<html><head></head><body></body></html>');
20-
});
10+
$middleware = new CookieConsentMiddleware();
2111

22-
$content = $result->getContent();
12+
$result = $middleware->handle($request, function ($request) {
13+
return (new Response())->setContent('<html><head></head><body></body></html>');
14+
});
2315

24-
$this->assertStringContainsString('<html><head></head><body>', $content);
25-
$this->assertStringContainsString('window.laravelCookieConsent', $content);
26-
$this->assertStringContainsString('</body></html>', $content);
27-
}
16+
$content = $result->getContent();
2817

29-
/** @test */
30-
public function it_does_not_alter_content_that_does_not_contain_a_body_tag()
31-
{
32-
$request = new Request();
18+
expect($content)->toContain(
19+
'<html><head></head><body>',
20+
'window.laravelCookieConsent',
21+
'</body></html>'
22+
);
23+
});
3324

34-
$middleware = new CookieConsentMiddleware();
25+
it('does not alter content that does not contain a body tag', function () {
26+
$request = new Request();
3527

36-
$result = $middleware->handle($request, function ($request) {
37-
return (new Response())->setContent('<html></html>');
38-
});
28+
$middleware = new CookieConsentMiddleware();
3929

40-
$content = $result->getContent();
30+
$result = $middleware->handle($request, function ($request) {
31+
return (new Response())->setContent('<html></html>');
32+
});
4133

42-
$this->assertEquals('<html></html>', $content);
43-
}
34+
expect($result->getContent())->toContain('<html></html>');
35+
});
4436

45-
/** @test */
46-
public function it_does_not_use_a_secure_cookie_if_session_secure_is_false()
47-
{
48-
config(['session.secure' => false]);
37+
it('does not use a secure cookie if session secure is false', function () {
38+
config()->set('session.secure', false);
4939

50-
$middleware = new CookieConsentMiddleware();
40+
$middleware = new CookieConsentMiddleware();
5141

52-
$result = $middleware->handle(new Request(), function () {
53-
return (new Response())->setContent('<html><head></head><body></body></html>');
54-
});
42+
$result = $middleware->handle(new Request(), function () {
43+
return (new Response())->setContent('<html><head></head><body></body></html>');
44+
});
5545

56-
$this->assertStringContainsString(';path=/\'', $result->getContent());
57-
$this->assertStringNotContainsString(';path=/;secure\'', $result->getContent());
58-
}
46+
expect($result->getContent())
47+
->toContain(';path=/\'')
48+
->not->toContain(';path=/;secure\'');
49+
});
5950

60-
/** @test */
61-
public function it_uses_a_secure_cookie_if_config_session_is_set_to_secure()
62-
{
63-
config(['session.secure' => true]);
51+
it('uses a secure cookie if config session is set to secure', function () {
52+
config(['session.secure' => true]);
6453

65-
$middleware = new CookieConsentMiddleware();
54+
$middleware = new CookieConsentMiddleware();
6655

67-
$result = $middleware->handle(new Request(), function () {
68-
return (new Response())->setContent('<html><head></head><body></body></html>');
69-
});
56+
$result = $middleware->handle(new Request(), function () {
57+
return (new Response())->setContent('<html><head></head><body></body></html>');
58+
});
7059

71-
$this->assertStringNotContainsString(';path=/\'', $result->getContent());
72-
$this->assertStringContainsString(';path=/;secure\'', $result->getContent());
73-
}
60+
expect($result->getContent())
61+
->not->toContain(';path=/\'')
62+
->toContain(';path=/;secure\'');
63+
});
7464

75-
/** @test */
76-
public function the_cookie_domain_is_set_by_the_session_domain_config_variable()
77-
{
78-
config(['session.domain' => 'some domain']);
65+
test('the cookie domain is set by the session domain config variable', function () {
66+
config(['session.domain' => 'some domain']);
7967

80-
$middleware = new CookieConsentMiddleware();
68+
$middleware = new CookieConsentMiddleware();
8169

82-
$result = $middleware->handle(new Request(), function () {
83-
return (new Response())->setContent('<html><head></head><body></body></html>');
84-
});
70+
$result = $middleware->handle(new Request(), function () {
71+
return (new Response())->setContent('<html><head></head><body></body></html>');
72+
});
8573

86-
$this->assertStringContainsString('const COOKIE_DOMAIN = \'some domain\'', $result->getContent());
87-
}
74+
expect($result->getContent())->toContain('const COOKIE_DOMAIN = \'some domain\'');
75+
});
8876

89-
/** @test */
90-
public function the_cookie_samesite_attribute_is_not_set_if_config_session_is_set_to_false()
91-
{
92-
config(['session.same_site' => null]);
77+
test('the cookie "samesite" attribute is not set if config session is set to false', function () {
78+
config(['session.same_site' => null]);
9379

94-
$middleware = new CookieConsentMiddleware();
80+
$middleware = new CookieConsentMiddleware();
9581

96-
$result = $middleware->handle(new Request(), function () {
97-
return (new Response())->setContent('<html><head></head><body></body></html>');
98-
});
82+
$result = $middleware->handle(new Request(), function () {
83+
return (new Response())->setContent('<html><head></head><body></body></html>');
84+
});
9985

100-
$this->assertStringNotContainsString(';samesite=', $result->getContent());
101-
}
86+
expect($result->getContent())->not->toContain(';samesite=');
87+
});
10288

103-
/** @test */
104-
public function the_cookie_samesite_attribute_is_by_the_session_samesite_config_variable()
105-
{
106-
config(['session.same_site' => 'strict']);
89+
test('the cookie "samesite" attribute is by the session "samesite" config variable', function () {
90+
config(['session.same_site' => 'strict']);
10791

108-
$middleware = new CookieConsentMiddleware();
92+
$middleware = new CookieConsentMiddleware();
10993

110-
$result = $middleware->handle(new Request(), function () {
111-
return (new Response())->setContent('<html><head></head><body></body></html>');
112-
});
94+
$result = $middleware->handle(new Request(), function () {
95+
return (new Response())->setContent('<html><head></head><body></body></html>');
96+
});
11397

114-
$this->assertStringContainsString(';samesite=strict', $result->getContent());
115-
}
98+
expect($result->getContent())->toContain(';samesite=strict');
99+
});
116100

117-
/** @test */
118-
public function it_uses_the_request_host_unless_session_domain_is_set()
119-
{
120-
config(['session.domain' => null]);
101+
it('uses the request host unless session domain is set', function () {
102+
config(['session.domain' => null]);
121103

122-
$middleware = new CookieConsentMiddleware();
104+
$middleware = new CookieConsentMiddleware();
123105

124-
$result = $middleware->handle(new Request(), function () {
125-
return (new Response())->setContent('<html><head></head><body></body></html>');
126-
});
106+
$result = $middleware->handle(new Request(), function () {
107+
return (new Response())->setContent('<html><head></head><body></body></html>');
108+
});
127109

128-
$this->assertStringContainsString('const COOKIE_DOMAIN = \'localhost\'', $result->getContent());
129-
}
130-
}
110+
expect($result->getContent())->toContain('const COOKIE_DOMAIN = \'localhost\'');
111+
});

tests/CookieConsentTest.php

+32-48
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,34 @@
11
<?php
22

3-
namespace Spatie\CookieConsent\Test;
4-
5-
class CookieConsentTest extends TestCase
6-
{
7-
/** @test */
8-
public function it_provides_translations()
9-
{
10-
$this->assertTranslationExists('cookie-consent::texts.message');
11-
$this->assertTranslationExists('cookie-consent::texts.agree');
12-
}
13-
14-
/** @test */
15-
public function it_can_display_a_cookie_consent_view()
16-
{
17-
$html = view('layout')->render();
18-
19-
$this->assertConsentDialogDisplayed($html);
20-
}
21-
22-
/** @test */
23-
public function it_will_not_show_the_cookie_consent_view_when_the_package_is_disabled()
24-
{
25-
$this->app['config']->set('cookie-consent.enabled', false);
26-
27-
$html = view('layout')->render();
28-
29-
$this->assertConsentDialogIsNotDisplayed($html);
30-
}
31-
32-
/** @test */
33-
public function it_will_not_show_the_cookie_consent_view_when_the_user_has_already_consented()
34-
{
35-
request()->cookies->set(config('cookie-consent.cookie_name'), config('cookie-consent.cookie_name'), 1);
36-
37-
$html = view('layout')->render();
38-
39-
$this->assertConsentDialogIsNotDisplayed($html);
40-
}
41-
42-
/** @test */
43-
public function it_contains_the_necessary_css_classes_for_javascript_functionality()
44-
{
45-
$html = view('dialog')->render();
46-
47-
$this->assertStringContainsString('js-cookie-consent', $html);
48-
$this->assertStringContainsString('js-cookie-consent-agree', $html);
49-
}
50-
}
3+
it('provides translations', function () {
4+
assertTranslationExists('cookie-consent::texts.message');
5+
assertTranslationExists('cookie-consent::texts.agree');
6+
});
7+
8+
it('can display a cookie consent view', function () {
9+
$html = view('layout')->render();
10+
11+
assertConsentDialogDisplayed($html);
12+
});
13+
14+
it('will not show the cookie consent view when the package is disabled', function () {
15+
config()->set('cookie-consent.enabled', false);
16+
17+
$html = view('layout')->render();
18+
19+
assertConsentDialogIsNotDisplayed($html);
20+
});
21+
22+
it('will not show the cookie consent view when the user has already consented', function () {
23+
request()->cookies->set(config('cookie-consent.cookie_name'), config('cookie-consent.cookie_name'), 1);
24+
25+
$html = view('layout')->render();
26+
27+
assertConsentDialogIsNotDisplayed($html);
28+
});
29+
30+
it('contains the necessary CSS classes for Javascript functionality', function () {
31+
$html = view('dialog')->render();
32+
33+
expect($html)->toContain('js-cookie-consent', 'js-cookie-consent-agree');
34+
});

tests/Pest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use \Illuminate\Support\Str;
4+
use function PHPUnit\Framework\assertFalse;
5+
use function PHPUnit\Framework\assertTrue;
6+
7+
uses(Spatie\CookieConsent\Test\TestCase::class)->in('.');
8+
9+
// Functions
10+
11+
function assertTranslationExists(string $key)
12+
{
13+
assertTrue(
14+
trans($key) != $key,
15+
"Failed to assert that a translation exists for key `{$key}`"
16+
);
17+
}
18+
19+
function assertConsentDialogDisplayed(string $html)
20+
{
21+
assertTrue(
22+
isConsentDialogDisplayed($html),
23+
'Failed to assert that the consent dialog is displayed.'
24+
);
25+
}
26+
27+
function assertConsentDialogIsNotDisplayed(string $html)
28+
{
29+
assertFalse(
30+
isConsentDialogDisplayed($html),
31+
'Failed to assert that the consent dialog is not being displayed.'
32+
);
33+
}
34+
35+
function isConsentDialogDisplayed(string $html): bool
36+
{
37+
return Str::contains($html, [
38+
trans('cookie-consent::texts.message'),
39+
trans('cookie-consent::texts.button_text'),
40+
]);
41+
}

0 commit comments

Comments
 (0)