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

convertResultCollectionToDataProviderArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 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