Failed Conditions
Push — master ( 18822f...664709 )
by Adrien
07:21
created

bin/test-nginx-rules.php (3 issues)

1
#! /usr/bin/env php
2
<?php
3
4
/**
5
 * Script to test the most important nginx rules
6
 */
7
function run(string $cmd): string
8
{
9
    echo $cmd . PHP_EOL;
10
11
    return shell_exec($cmd);
1 ignored issue
show
Bug Best Practice introduced by
The expression return shell_exec($cmd) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
12
}
13
14
function test(bool $loggedIn, string $url): void
15
{
16
    if ($loggedIn) {
17
        $cookie = "--cookie 'PHPSESSID=foo'";
18
        $pattern = '<img src="assets/logo-artisans-de-la-transition.svg"';
19
        $message = 'logged in user should get raw Angular';
20
    } else {
21
        $cookie = '';
22
        $pattern = '<natural-icon';
23
        $message = 'anonymous should get pre-rendered page by SSR';
24
    }
25
26
    // Assert HTTP status code
27
    $status = run("curl --insecure --silent --output /dev/null --write-out '%{http_code}' '$url' $cookie");
28
    if ($status !== '200') {
29
        echo "FAILED STATUS CODE: $status: $message" . PHP_EOL;
30
        exit(1);
1 ignored issue
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
31
    }
32
33
    // Assert HTML content
34
    $content = run("curl --insecure --silent '$url' $cookie");
35
    if (mb_strpos($content, $pattern) === false) {
36
        echo "FAILED PATTERN: '$pattern': $message" . PHP_EOL;
37
        exit(1);
1 ignored issue
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
38
    }
39
}
40
41
test(true, 'https://artisans.lan');
42
test(true, 'https://artisans.lan/larevuedurable/numeros');
43
test(true, 'https://artisans.lan/mon-compte');
44
45
test(false, 'https://artisans.lan');
46
test(false, 'https://artisans.lan/larevuedurable/numeros');
47
test(false, 'https://artisans.lan/mon-compte');
48
49
echo 'All tests OK' . PHP_EOL;
50