Completed
Push — master ( 75c9f6...102369 )
by Mike
13s
created

CaffeineTest::testRouteMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php namespace GeneaLabs\LaravelCaffeine\Tests\Feature;
2
3
use GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware;
4
use GeneaLabs\LaravelCaffeine\Tests\TestCase;
5
6
class CaffeineTest extends TestCase
7
{
8
    public function testDripEndpoint()
9
    {
10
        $dripRoute = config('genealabs-laravel-caffeine.route', 'genealabs/laravel-caffeine/drip');
11
12
        $response = $this->get($dripRoute);
13
14
        $response->assertStatus(204);
15
    }
16
17
    public function testMiddlewareInjectsDripScript()
18
    {
19
        $expectedResult = file_get_contents(__DIR__ . '/../Fixtures/expired_script.txt');
20
21
        $response = $this->get(route('genealabs-laravel-caffeine.tests.form'));
22
23
        $response->assertSee($expectedResult);
24
    }
25
26 View Code Duplication
    public function testSuccessfulDrip()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $dripRoute = config('genealabs-laravel-caffeine.route', 'genealabs/laravel-caffeine/drip');
29
        $html = $this->get(route('genealabs-laravel-caffeine.tests.form'))
30
            ->getContent();
31
        $matches = [];
32
        preg_match('/<meta name="csrf-token" content="(.*?)">/', $html, $matches);
33
        $csrfToken = $matches[1];
34
35
        $response = $this->get($dripRoute, [
36
            '_token' => $csrfToken,
37
        ]);
38
39
        $response->assertStatus(204);
40
    }
41
42 View Code Duplication
    public function testExpiredDrip()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $dripRoute = config(
45
            'genealabs-laravel-caffeine.dripInterval',
46
            'genealabs/laravel-caffeine/drip'
47
        );
48
        $html = $this->get(route('genealabs-laravel-caffeine.tests.form'))
49
            ->getContent();
50
        $matches = [];
51
        preg_match(
52
            '/<meta name="csrf-token" content="(.*?)">/',
53
            $html,
54
            $matches
55
        );
56
        $csrfToken = $matches[1];
57
58
        app('session')->flush();
59
        $response = $this->get($dripRoute, [
60
            '_token' => $csrfToken,
61
        ]);
62
63
        $response->assertStatus(404);
64
    }
65
66
    public function testDisabledCaffeination()
67
    {
68
        $html = $this
69
            ->get(route('genealabs-laravel-caffeine.tests.disabled-page'))
70
            ->getContent();
71
72
        $isDisabled = (bool) preg_match(
73
            '/<meta name="caffeinated" content="false">/',
74
            $html
75
        );
76
        $hasDripper = (bool) preg_match(
77
            '/\bconst caffeineSendDrip\b/',
78
            $html
79
        );
80
81
        $this->assertTrue($isDisabled);
82
        $this->assertFalse($hasDripper);
83
    }
84
85
    public function testNonStringReturnContent()
86
    {
87
        $response = $this->get(route('genealabs-laravel-caffeine.tests.null-response'));
88
89
        $response->assertDontSee('const caffeineSendDrip');
90
    }
91
92
    public function testRouteMiddleware()
93
    {
94
        app('router')->aliasMiddleware(
95
            'caffeinated',
96
            '\\' . LaravelCaffeineDripMiddleware::class
97
        );
98
99
        $response = $this
100
            ->get(route('genealabs-laravel-caffeine.tests.route-middleware'));
101
102
        $response->assertSee('const caffeineSendDrip');
103
    }
104
}
105