PhpWebServer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A startWebserver() 0 16 1
A stopWebserver() 0 3 1
A verifyProcessRunning() 0 7 2
1
<?php declare(strict_types = 1);
2
3
namespace Simplex\Quickstart\Shared\Testing;
4
5
use Symfony\Component\Process\Process;
6
7
trait PhpWebServer
8
{
9
    /** @var string */
10
    private static $webserverDocroot = 'public';
11
12
    /** @var string */
13
    private static $webserverRouter = 'public/app.php';
14
15
    /** @var Process */
16
    private static $webserverProcess;
17
18
    /**
19
     * @beforeClass
20
     */
21
    public static function startWebserver(): void
22
    {
23
        /** @noinspection PhpUndefinedConstantInspection */
24
        $command = sprintf(
25
            'php -S %s:%d -t %s %s',
26
            PHP_WEBSERVER_HOST,
27
            (int) PHP_WEBSERVER_PORT,
28
            self::$webserverDocroot,
29
            self::$webserverRouter
30
        );
31
32
        self::$webserverProcess = new Process($command);
33
34
        self::$webserverProcess->start();
35
36
        self::verifyProcessRunning(self::$webserverProcess);
37
    }
38
39
    private static function verifyProcessRunning(Process $process): void
40
    {
41
        if (!$process->isRunning()) {
42
            throw new \RuntimeException(sprintf(
43
                'Failed to start "%s" in background: %s',
44
                $process->getCommandLine(),
45
                $process->getErrorOutput()
46
            ));
47
        }
48
    }
49
50
    /**
51
     * @afterClass
52
     */
53
    public static function stopWebserver(): void
54
    {
55
        self::$webserverProcess->stop();
56
    }
57
}
58