Completed
Push — master ( ec4e5d...fd3bfb )
by Sebastian
03:44 queued 48s
created

SmokeTestTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
c 5
b 0
f 0
lcom 0
cbo 10
dl 0
loc 77
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A successOutput() 0 4 1
A errorOutput() 0 4 1
A runSmokeTests() 0 22 1
A assertSuccess() 0 5 1
A assertTimeToFirstByteBelow() 0 8 1
A assertBodyNotEmpty() 0 5 1
1
<?php
2
namespace DjThossi\SmokeTestingPhp;
3
4
use DjThossi\SmokeTestingPhp\Options\RunnerOptions;
5
use DjThossi\SmokeTestingPhp\Options\SmokeTestOptions;
6
use DjThossi\SmokeTestingPhp\Result\Result;
7
use DjThossi\SmokeTestingPhp\Runner\CurlHttpRunner;
8
use DjThossi\SmokeTestingPhp\ValueObject\TimeToFirstByte;
9
use PHPUnit\Framework\Assert;
10
11
trait SmokeTestTrait
12
{
13
    /**
14
     * @param Result $result
15
     */
16
    public function successOutput(Result $result)
17
    {
18
        //Please override this method in you test class if you would like to do an output
19
    }
20
21
    /**
22
     * @param Result $result
23
     */
24
    public function errorOutput(Result $result)
25
    {
26
        //Please override this method in you test class if you would like to do an output
27
    }
28
29
    /**
30
     * @param SmokeTestOptions $smokeTestOptions
31
     *
32
     * @return array
33
     */
34
    protected function runSmokeTests(SmokeTestOptions $smokeTestOptions)
35
    {
36
        $httpRunner = new CurlHttpRunner(
37
            $smokeTestOptions->getConcurrency(),
38
            $smokeTestOptions->getBodyLength(),
39
            [$this, 'successOutput'],
40
            [$this, 'errorOutput']
41
        );
42
43
        $runner = new SmokeTest($httpRunner);
44
45
        $runnerOptions = new RunnerOptions(
46
            $smokeTestOptions->getUrls(),
47
            $smokeTestOptions->getRequestTimeout(),
48
            $smokeTestOptions->getFollowRedirect(),
49
            $smokeTestOptions->getBasicAuth()
50
        );
51
52
        $resultCollection = $runner->run($runnerOptions);
53
54
        return $resultCollection->asDataProviderArray();
55
    }
56
57
    /**
58
     * @param Result $result
59
     */
60
    protected function assertSuccess(Result $result)
61
    {
62
        Assert::assertTrue($result->isValidResult(), $result->asString());
63
        Assert::assertSame(200, $result->getStatusCode()->asInteger(), $result->asString());
64
    }
65
66
    /**
67
     * @param TimeToFirstByte $maxTimeToFirstByte
68
     * @param Result $result
69
     */
70
    protected function assertTimeToFirstByteBelow(TimeToFirstByte $maxTimeToFirstByte, Result $result)
71
    {
72
        Assert::assertLessThanOrEqual(
73
            $maxTimeToFirstByte->inMilliSeconds(),
74
            $result->getTimeToFirstByte()->inMilliSeconds(),
75
            $result->asString()
76
        );
77
    }
78
79
    /**
80
     * @param Result $result
81
     */
82
    protected function assertBodyNotEmpty(Result $result)
83
    {
84
        Assert::assertNotNull($result->getBody(), $result->asString());
85
        Assert::assertNotEmpty($result->getBody()->asString(), $result->asString());
86
    }
87
}
88