Completed
Push — master ( a6fe0f...b0c0c1 )
by Sebastian
02:05
created

SmokeTestTrait::assertHeaderExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 7
rs 9.4285
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
        $errorMessage = "This SmokeTest was not successful\n" . $result->asString();
65
66
        Assert::assertTrue($result->isValidResult(), $errorMessage);
67
        Assert::assertSame(200, $result->getStatusCode()->asInteger(), $errorMessage);
68
    }
69
70
    /**
71
     * @param TimeToFirstByte $maxTimeToFirstByte
72
     * @param Result $result
73
     */
74
    protected function assertTimeToFirstByteBelow(TimeToFirstByte $maxTimeToFirstByte, Result $result)
75
    {
76
        $errorMessage = "This SmokeTest was to slow\n" . $result->asString();
77
78
        Assert::assertLessThanOrEqual(
79
            $maxTimeToFirstByte->inMilliSeconds(),
80
            $result->getTimeToFirstByte()->inMilliSeconds(),
81
            $errorMessage
82
        );
83
    }
84
85
    /**
86
     * @param Result $result
87
     */
88
    protected function assertBodyNotEmpty(Result $result)
89
    {
90
        $errorMessage = "The body of this SmokeTest is empty\n" . $result->asString();
91
92
        Assert::assertNotEmpty($result->getBody()->asString(), $errorMessage);
93
    }
94
95
    /**
96
     * @param HeaderKey $key
97
     * @param Result $result
98
     */
99 View Code Duplication
    protected function assertHeaderKeyExists(HeaderKey $key, Result $result)
100
    {
101
        $errorMessage = "HeaderKey not found in this SmokeTest\n" . $result->asString();
102
103
        Assert::assertGreaterThan(0, $result->getHeaders()->count(), $errorMessage);
104
        Assert::assertTrue($result->getHeaders()->headerKeyExists($key), $errorMessage);
105
    }
106
107
    /**
108
     * @param Header $searchHeader
109
     * @param Result $result
110
     */
111 View Code Duplication
    protected function assertHeaderExists(Header $searchHeader, Result $result)
112
    {
113
        $errorMessage = "Header not found in this SmokeTest\n" . $result->asString();
114
115
        Assert::assertGreaterThan(0, $result->getHeaders()->count(), $errorMessage);
116
        Assert::assertTrue($result->getHeaders()->headerExists($searchHeader), $errorMessage);
117
    }
118
}
119