Passed
Pull Request — master (#11)
by Sebastian
02:07
created

SmokeTestTrait::assertHasHeaderKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 6
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)
0 ignored issues
show
Unused Code introduced by
The parameter $result is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $result is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 = sprintf(
65
            "This SmokeTest was not successful\n%s",
66
            $result->asString()
67
        );
68
69
        Assert::assertTrue($result->isValidResult(), $errorMessage);
70
        Assert::assertSame(200, $result->getStatusCode()->asInteger(), $errorMessage);
71
    }
72
73
    /**
74
     * @param TimeToFirstByte $maxTimeToFirstByte
75
     * @param Result $result
76
     */
77
    protected function assertTimeToFirstByteBelow(TimeToFirstByte $maxTimeToFirstByte, Result $result)
78
    {
79
        $errorMessage = sprintf(
80
            "This SmokeTest was to slow\n%s",
81
            $result->asString()
82
        );
83
84
        Assert::assertLessThanOrEqual(
85
            $maxTimeToFirstByte->inMilliSeconds(),
86
            $result->getTimeToFirstByte()->inMilliSeconds(),
87
            $errorMessage
88
        );
89
    }
90
91
    /**
92
     * @param Result $result
93
     */
94
    protected function assertBodyNotEmpty(Result $result)
95
    {
96
        $errorMessage = sprintf(
97
            "The body of this SmokeTest is empty\n%s",
98
            $result->asString()
99
        );
100
101
        Assert::assertNotEmpty($result->getBody()->asString(), $errorMessage);
102
    }
103
104
    /**
105
     * @param HeaderKey $key
106
     * @param Result $result
107
     */
108 View Code Duplication
    protected function assertHasHeaderKey(HeaderKey $key, Result $result)
109
    {
110
        $errorMessage = sprintf(
111
            "HeaderKey not found in this SmokeTest\n%s",
112
            $result->asString()
113
        );
114
115
        Assert::assertGreaterThan(0, $result->getHeaders()->count(), $errorMessage);
116
        Assert::assertTrue($result->getHeaders()->headerKeyExists($key), $errorMessage);
117
    }
118
119
    /**
120
     * @param Header $searchHeader
121
     * @param Result $result
122
     */
123 View Code Duplication
    protected function assertHasHeader(Header $searchHeader, Result $result)
124
    {
125
        $errorMessage = sprintf(
126
            "Header not found in this SmokeTest\n%s",
127
            $result->asString()
128
        );
129
130
        Assert::assertGreaterThan(0, $result->getHeaders()->count(), $errorMessage);
131
        Assert::assertTrue($result->getHeaders()->headerExists($searchHeader), $errorMessage);
132
    }
133
134
    /**
135
     * @param Header $searchHeader
136
     * @param Result $result
137
     */
138
    protected function assertNotHasHeader(Header $searchHeader, Result $result)
139
    {
140
        $errorMessage = sprintf(
141
            "Header found in this SmokeTest\n%s",
142
            $result->asString()
143
        );
144
145
        Assert::assertFalse($result->getHeaders()->headerExists($searchHeader), $errorMessage);
146
    }
147
}
148