PhpWebServer::verifyProcessRunning()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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