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");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user