Completed
Pull Request — develop (#2)
by Sebastian
02:18
created

SmokeTestTrait::assertHeaderExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
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\Header;
9
use DjThossi\SmokeTestingPhp\ValueObject\HeaderKey;
10
use DjThossi\SmokeTestingPhp\ValueObject\TimeToFirstByte;
11
use PHPUnit\Framework\Assert;
12
13
trait SmokeTestTrait
14
{
15
    /**
16
     * @param Result $result
17
     */
18
    public function successOutput(Result $result)
19
    {
20
        //Please override this method in you test class if you would like to do an output
21
    }
22
23
    /**
24
     * @param Result $result
25
     */
26
    public function errorOutput(Result $result)
27
    {
28
        //Please override this method in you test class if you would like to do an output
29
    }
30
31
    /**
32
     * @param SmokeTestOptions $smokeTestOptions
33
     *
34
     * @return array
35
     */
36
    protected function runSmokeTests(SmokeTestOptions $smokeTestOptions)
37
    {
38
        $httpRunner = new CurlHttpRunner(
39
            $smokeTestOptions->getConcurrency(),
40
            $smokeTestOptions->getBodyLength(),
41
            [$this, 'successOutput'],
42
            [$this, 'errorOutput']
43
        );
44
45
        $runner = new SmokeTest($httpRunner);
46
47
        $runnerOptions = new RunnerOptions(
48
            $smokeTestOptions->getUrls(),
49
            $smokeTestOptions->getRequestTimeout(),
50
            $smokeTestOptions->getFollowRedirect(),
51
            $smokeTestOptions->getBasicAuth()
52
        );
53
54
        $resultCollection = $runner->run($runnerOptions);
55
56
        return $resultCollection->asDataProviderArray();
57
    }
58
59
    /**
60
     * @param Result $result
61
     */
62
    protected function assertSuccess(Result $result)
63
    {
64
        Assert::assertTrue($result->isValidResult(), $result->asString());
65
        Assert::assertSame(200, $result->getStatusCode()->asInteger(), $result->asString());
66
    }
67
68
    /**
69
     * @param TimeToFirstByte $maxTimeToFirstByte
70
     * @param Result $result
71
     */
72
    protected function assertTimeToFirstByteBelow(TimeToFirstByte $maxTimeToFirstByte, Result $result)
73
    {
74
        Assert::assertLessThanOrEqual(
75
            $maxTimeToFirstByte->inMilliSeconds(),
76
            $result->getTimeToFirstByte()->inMilliSeconds(),
77
            $result->asString()
78
        );
79
    }
80
81
    /**
82
     * @param Result $result
83
     */
84
    protected function assertBodyNotEmpty(Result $result)
85
    {
86
        Assert::assertNotNull($result->getBody(), $result->asString());
87
        Assert::assertNotEmpty($result->getBody()->asString(), $result->asString());
88
    }
89
90
    /**
91
     * @param HeaderKey $key
92
     * @param Result $result
93
     */
94
    protected function assertHeaderKeyExists(HeaderKey $key, Result $result)
95
    {
96
        Assert::assertNotNull($result->getHeaders(), $result->asString());
97
        Assert::assertGreaterThan(0, $result->getHeaders()->count(), $result->asString());
98
        Assert::assertTrue($result->getHeaders()->headerKeyExists($key), $result->asString());
99
    }
100
101
    /**
102
     * @param Header $searchHeader
103
     * @param Result $result
104
     */
105
    protected function assertHeaderExists(Header $searchHeader, Result $result)
106
    {
107
        Assert::assertNotNull($result->getHeaders(), $result->asString());
108
        Assert::assertGreaterThan(0, $result->getHeaders()->count(), $result->asString());
109
        Assert::assertTrue($result->getHeaders()->headerExists($searchHeader), $result->asString());
110
    }
111
}
112