Passed
Push — v0.2 ( 4d3792...642965 )
by Freddie
02:51
created

WebApplicationTest::runProcessWithRetry()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 5
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Simplex\Quickstart\Functional\Test;
4
5
use PHPUnit\Framework\TestCase;
6
use Simplex\Quickstart\Module\Demo\DataFixture\PersonLoader;
7
use Simplex\Quickstart\Shared\Testing\PhpWebServer;
8
use Symfony\Component\Process\Exception\ProcessFailedException;
9
use Symfony\Component\Process\Process;
10
11
class WebApplicationTest extends TestCase
12
{
13
    use PhpWebServer;
14
15
    private $intervalDuration = 60000; // 60 ms
16
    private $numberOfRetries = 34; // max 2.04 seconds
17
18
    public function testWebApplication()
19
    {
20
        $acceptHeader = '"Accept: application/json"';
21
22
        /** @noinspection PhpUndefinedConstantInspection */
23
        $host = PHP_WEBSERVER_HOST;
24
        /** @noinspection PhpUndefinedConstantInspection */
25
        $port = PHP_WEBSERVER_PORT;
26
27
        $process = new Process("curl -H $acceptHeader http://$host:$port/");
28
29
        $this->runProcessWithRetry($process);
30
31
        self::assertContains('"name":"'. PersonLoader::PERSON_1_NAME . '"', $process->getOutput());
32
    }
33
34
    private function runProcessWithRetry(Process $process): void
35
    {
36
        for ($i = 0; $i < $this->numberOfRetries; ++$i) {
37
38
            usleep($this->intervalDuration);
39
40
            $process->run();
41
42
            if ($process->isSuccessful()) {
43
                return;
44
            }
45
        }
46
47
        if (!$process->isSuccessful()) {
48
            throw new ProcessFailedException($process);
49
        }
50
    }
51
}
52