Completed
Push — resize ( 74428f...fbaf05 )
by Mikael
07:41
created

FeatureContext::setMode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
rs 9.2
1
<?php
2
3
use Behat\Behat\Tester\Exception\PendingException;
4
use Behat\Behat\Context\Context;
5
use Behat\Behat\Context\SnippetAcceptingContext;
6
use Behat\Gherkin\Node\PyStringNode;
7
use Behat\Gherkin\Node\TableNode;
8
9
/**
10
 * Defines application features from the specific context.
11
 */
12
class FeatureContext implements Context, SnippetAcceptingContext
13
{
14
    private $url = null;
15
16
    private $headers = [];
17
    private $imageString = null;
18
    private $image = null;
19
    private $imageJSON = null;
20
21
22
    /**
23
     * Initializes context.
24
     *
25
     * Every scenario gets its own context instance.
26
     * You can also pass arbitrary arguments to the
27
     * context constructor through behat.yml.
28
     */
29
    public function __construct()
30
    {
31
    }
32
33
34
35
    /**
36
     * @Given Set mode :arg1
37
     */
38
    public function setMode($arg1 = null)
39
    {
40
        $this->url = "http://localhost/git/cimage/webroot/";
41
        switch ($arg1) {
42
            case "development":
43
                $this->url .= "imgd.php";
44
                break;
45
            case "production":
46
                $this->url .= "imgp.php";
47
                break;
48
            case "strict":
49
                $this->url .= "imgs.php";
50
                break;
51
            default:
52
                $this->url .= "img.php";
53
        }
54
    }
55
56
57
58
    /**
59
     * @Given Set src :arg1
60
     */
61
    public function setSrc($arg1)
62
    {
63
        if (is_null($this->url)) {
64
            $this->setMode();
65
        }
66
67
        $this->url .= "?src=$arg1";
68
    }
69
70
71
72
    /**
73
     * @When Get image
74
     */
75
    public function getImage()
76
    {
77
        //echo $this->url;
78
        $res = file_get_contents($this->url);
79
        PHPUnit_Framework_Assert::assertNotEquals(false, $res);
80
81
        $this->imageString = $res;
82
        $this->headers = $http_response_header;
83
        
84
        if (is_null($this->imageJSON)) {
85
            $this->getImageAsJson();
86
        }
87
    }
88
89
90
91
    /**
92
     * @When Get image as JSON
93
     */
94
    public function getImageAsJson()
95
    {
96
        $res = file_get_contents($this->url . "&json");
97
        PHPUnit_Framework_Assert::assertNotEquals(false, $res);
98
99
        $res = json_decode($res, true);
100
        PHPUnit_Framework_Assert::assertNotEquals(null, $res);
101
102
        $this->imageJSON = $res;
103
    }
104
105
106
107
    /**
108
     * @When Get headers for image
109
     */
110
    public function getHeadersForImage()
111
    {
112
        //echo $this->url;
113
        $res = get_headers($this->url);
114
        PHPUnit_Framework_Assert::assertNotEquals(false, $res);
115
116
        $this->headers = $http_response_header;
0 ignored issues
show
Bug introduced by
The variable $http_response_header does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
117
    }
118
119
120
121
    /**
122
     * @Then Returns status code :arg1
123
     */
124
    public function returnsStatusCode($arg1)
125
    {
126
        PHPUnit_Framework_Assert::assertNotEquals(
127
            false,
128
            strpos($this->headers[0], $arg1)
129
        );
130
    }
131
132
133
    /**
134
     *
135
     */
136
    private function compareImageJsonToHeaders()
137
    {
138
        $contentLength = "Content-Length: " . $this->imageJSON["size"];
139
        PHPUnit_Framework_Assert::assertContains(
140
            $contentLength,
141
            $this->headers
142
        );
143
144
        $contentType = "Content-Type: " . $this->imageJSON["mimeType"];
145
        PHPUnit_Framework_Assert::assertContains(
146
            $contentType,
147
            $this->headers
148
        );
149
150
        $lastModified = "Last-Modified: " . $this->imageJSON["cacheGmdate"] . " GMT";
151
        PHPUnit_Framework_Assert::assertContains(
152
            $lastModified,
153
            $this->headers
154
        );
155
    }
156
157
158
159
    /**
160
     *
161
     */
162
    private function compareImageJsonToSavedJson($file)
163
    {
164
        $res = file_get_contents("$file.json");
165
        PHPUnit_Framework_Assert::assertNotEquals(false, $res);
166
167
        $res = json_decode($res, true);
168
        PHPUnit_Framework_Assert::assertNotEquals(null, $res);
169
170
        $keys = [
171
            "mimeType",
172
            "width",
173
            "height",
174
            "size",
175
            "colors",
176
            "pngType",
177
        ];
178
        foreach ($keys as $key) {
179
            if (array_key_exists($key, $res)
180
                && array_key_exists($key, $this->imageJSON)
181
            ) {
182
                PHPUnit_Framework_Assert::assertEquals(
183
                    $res[$key],
184
                    $this->imageJSON[$key]
185
                );
186
            }
187
        }
188
    }
189
190
191
192
    /**
193
     * @Then Compares to image :arg1
194
     */
195
    public function comparesToImage($arg1)
196
    {
197
        $base = __DIR__ . "/../img";
198
        $res = file_get_contents("$base/$arg1");
199
        PHPUnit_Framework_Assert::assertNotEquals(false, $res);
200
201
        PHPUnit_Framework_Assert::assertEquals($this->imageString, $res);
202
203
        $this->compareImageJsonToHeaders();
204
        $this->compareImageJsonToSavedJson("$base/$arg1");
205
    }
206
}
207