mirror of
https://github.com/mosbth/cimage.git
synced 2025-07-30 21:20:11 +02:00
adding behat for feature testing
This commit is contained in:
206
features/bootstrap/FeatureContext.php
Normal file
206
features/bootstrap/FeatureContext.php
Normal file
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
use Behat\Behat\Tester\Exception\PendingException;
|
||||
use Behat\Behat\Context\Context;
|
||||
use Behat\Behat\Context\SnippetAcceptingContext;
|
||||
use Behat\Gherkin\Node\PyStringNode;
|
||||
use Behat\Gherkin\Node\TableNode;
|
||||
|
||||
/**
|
||||
* Defines application features from the specific context.
|
||||
*/
|
||||
class FeatureContext implements Context, SnippetAcceptingContext
|
||||
{
|
||||
private $url = null;
|
||||
|
||||
private $headers = [];
|
||||
private $imageString = null;
|
||||
private $image = null;
|
||||
private $imageJSON = null;
|
||||
|
||||
|
||||
/**
|
||||
* Initializes context.
|
||||
*
|
||||
* Every scenario gets its own context instance.
|
||||
* You can also pass arbitrary arguments to the
|
||||
* context constructor through behat.yml.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @Given Set mode :arg1
|
||||
*/
|
||||
public function setMode($arg1 = null)
|
||||
{
|
||||
$this->url = "http://localhost/git/cimage/webroot/";
|
||||
switch ($arg1) {
|
||||
case "development":
|
||||
$this->url .= "imgd.php";
|
||||
break;
|
||||
case "production":
|
||||
$this->url .= "imgp.php";
|
||||
break;
|
||||
case "strict":
|
||||
$this->url .= "imgs.php";
|
||||
break;
|
||||
default:
|
||||
$this->url .= "img.php";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @Given Set src :arg1
|
||||
*/
|
||||
public function setSrc($arg1)
|
||||
{
|
||||
if (is_null($this->url)) {
|
||||
$this->setMode();
|
||||
}
|
||||
|
||||
$this->url .= "?src=$arg1";
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @When Get image
|
||||
*/
|
||||
public function getImage()
|
||||
{
|
||||
//echo $this->url;
|
||||
$res = file_get_contents($this->url);
|
||||
PHPUnit_Framework_Assert::assertNotEquals(false, $res);
|
||||
|
||||
$this->imageString = $res;
|
||||
$this->headers = $http_response_header;
|
||||
|
||||
if (is_null($this->imageJSON)) {
|
||||
$this->getImageAsJson();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @When Get image as JSON
|
||||
*/
|
||||
public function getImageAsJson()
|
||||
{
|
||||
$res = file_get_contents($this->url . "&json");
|
||||
PHPUnit_Framework_Assert::assertNotEquals(false, $res);
|
||||
|
||||
$res = json_decode($res, true);
|
||||
PHPUnit_Framework_Assert::assertNotEquals(null, $res);
|
||||
|
||||
$this->imageJSON = $res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @When Get headers for image
|
||||
*/
|
||||
public function getHeadersForImage()
|
||||
{
|
||||
//echo $this->url;
|
||||
$res = get_headers($this->url);
|
||||
PHPUnit_Framework_Assert::assertNotEquals(false, $res);
|
||||
|
||||
$this->headers = $http_response_header;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @Then Returns status code :arg1
|
||||
*/
|
||||
public function returnsStatusCode($arg1)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertNotEquals(
|
||||
false,
|
||||
strpos($this->headers[0], $arg1)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function compareImageJsonToHeaders()
|
||||
{
|
||||
$contentLength = "Content-Length: " . $this->imageJSON["size"];
|
||||
PHPUnit_Framework_Assert::assertContains(
|
||||
$contentLength,
|
||||
$this->headers
|
||||
);
|
||||
|
||||
$contentType = "Content-Type: " . $this->imageJSON["mimeType"];
|
||||
PHPUnit_Framework_Assert::assertContains(
|
||||
$contentType,
|
||||
$this->headers
|
||||
);
|
||||
|
||||
$lastModified = "Last-Modified: " . $this->imageJSON["cacheGmdate"] . " GMT";
|
||||
PHPUnit_Framework_Assert::assertContains(
|
||||
$lastModified,
|
||||
$this->headers
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function compareImageJsonToSavedJson($file)
|
||||
{
|
||||
$res = file_get_contents("$file.json");
|
||||
PHPUnit_Framework_Assert::assertNotEquals(false, $res);
|
||||
|
||||
$res = json_decode($res, true);
|
||||
PHPUnit_Framework_Assert::assertNotEquals(null, $res);
|
||||
|
||||
$keys = [
|
||||
"mimeType",
|
||||
"width",
|
||||
"height",
|
||||
"size",
|
||||
"colors",
|
||||
"pngType",
|
||||
];
|
||||
foreach ($keys as $key) {
|
||||
if (array_key_exists($key, $res)
|
||||
&& array_key_exists($key, $this->imageJSON)
|
||||
) {
|
||||
PHPUnit_Framework_Assert::assertEquals(
|
||||
$res[$key],
|
||||
$this->imageJSON[$key]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @Then Compares to image :arg1
|
||||
*/
|
||||
public function comparesToImage($arg1)
|
||||
{
|
||||
$base = __DIR__ . "/../img";
|
||||
$res = file_get_contents("$base/$arg1");
|
||||
PHPUnit_Framework_Assert::assertNotEquals(false, $res);
|
||||
|
||||
PHPUnit_Framework_Assert::assertEquals($this->imageString, $res);
|
||||
|
||||
$this->compareImageJsonToHeaders();
|
||||
$this->compareImageJsonToSavedJson("$base/$arg1");
|
||||
}
|
||||
}
|
8
features/dummy.feature
Normal file
8
features/dummy.feature
Normal file
@@ -0,0 +1,8 @@
|
||||
Feature: dummy
|
||||
Display an dummy image without using a existing image on file.
|
||||
|
||||
Scenario: Set source to be dummy
|
||||
Given Set src "dummy"
|
||||
When Get image
|
||||
Then Returns status code "200"
|
||||
And Compares to image "dummy"
|
BIN
features/img/dummy
Normal file
BIN
features/img/dummy
Normal file
Binary file not shown.
After Width: | Height: | Size: 334 B |
19
features/img/dummy.json
Normal file
19
features/img/dummy.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"src": "dummy_100_100_q60_co-1",
|
||||
"srcGmdate": "Fri, 03 Jun 2016 07:38:56",
|
||||
"cache": "_._dummy_100_100_q60_co-1_q60_co-1",
|
||||
"cacheGmdate": "Fri, 03 Jun 2016 07:38:56",
|
||||
"filename": "_._dummy_100_100_q60_co-1_q60_co-1",
|
||||
"mimeType": "image/png",
|
||||
"width": 100,
|
||||
"height": 100,
|
||||
"aspectRatio": 1,
|
||||
"size": 334,
|
||||
"colors": 1,
|
||||
"includedFiles": 6,
|
||||
"memoryPeek": "0.341 MB",
|
||||
"memoryCurrent": "0.316 MB",
|
||||
"memoryLimit": "128M",
|
||||
"loadTime": "0.01s",
|
||||
"pngType": "PNG is type 6, RGB with alpha channel (PNG 32-bit)"
|
||||
}
|
BIN
features/img/test_100x100.png
Normal file
BIN
features/img/test_100x100.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 392 B |
19
features/img/test_100x100.png.json
Normal file
19
features/img/test_100x100.png.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"src": "test_100x100.png",
|
||||
"srcGmdate": "Fri, 03 Jun 2016 08:12:39",
|
||||
"cache": "_._test_100x100.png_q60_co-1",
|
||||
"cacheGmdate": "Fri, 03 Jun 2016 08:19:37",
|
||||
"filename": "_._test_100x100.png_q60_co-1",
|
||||
"mimeType": "image/png",
|
||||
"width": 100,
|
||||
"height": 100,
|
||||
"aspectRatio": 1,
|
||||
"size": 392,
|
||||
"colors": 6,
|
||||
"includedFiles": 6,
|
||||
"memoryPeek": "0.339 MB",
|
||||
"memoryCurrent": "0.316 MB",
|
||||
"memoryLimit": "128M",
|
||||
"loadTime": "0.012s",
|
||||
"pngType": "PNG is type 6, RGB with alpha channel (PNG 32-bit)"
|
||||
}
|
13
features/src.feature
Normal file
13
features/src.feature
Normal file
@@ -0,0 +1,13 @@
|
||||
Feature: src
|
||||
Display an image by selecting its source.
|
||||
|
||||
Scenario: Source is not a valid image name
|
||||
Given Set src "NO_IMAGE"
|
||||
When Get headers for image
|
||||
Then Returns status code "404"
|
||||
|
||||
Scenario: Get only source image
|
||||
Given Set src "test_100x100.png"
|
||||
When Get image
|
||||
Then Returns status code "200"
|
||||
And Compares to image "test_100x100.png"
|
Reference in New Issue
Block a user