Skip to content

Instantly share code, notes, and snippets.

@peterlafferty
Created October 25, 2017 20:20
Show Gist options
  • Save peterlafferty/2348af5bfff9910dda938b6ca2f8f903 to your computer and use it in GitHub Desktop.
Save peterlafferty/2348af5bfff9910dda938b6ca2f8f903 to your computer and use it in GitHub Desktop.
unit test for silex controller
<?php
use pl\tddcontroller\TddController;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
class TddControllerTest extends TestCase
{
/** @var TddController */
private $controller;
/** @var string */
private $charset = 'UTF-8';
public function setUp()
{
$this->controller = new TddController(
$this->charset
);
}
public function testGetCity()
{
$cityId = 583737;
$expected = '"' . $cityId . $this->charset . '"';
/** @var JsonResponse $jsonResponse */
$jsonResponse = $this->controller->get($cityId);
$this->assertEquals($expected, $jsonResponse->getContent());
}
public function testCitiesWithQueryString()
{
$queryString = ['returnsecondvalue' => 'true'];
$expected = '"value2' . $this->charset . '"';
$jsonResponse = $this->controller->getAll($queryString);
$this->assertEquals(
$expected,
$jsonResponse->getContent()
);
}
public function testCitiesWithQueryStringNotTrue()
{
$queryString = ['returnsecondvalue' => 'false'];
$expected = '"value1' . $this->charset . '"';
$jsonResponse = $this->controller->getAll($queryString);
$this->assertEquals(
$expected,
$jsonResponse->getContent()
);
}
public function testCitiesWithOutQueryString()
{
$queryString = [];
$expected = '"value1' . $this->charset . '"';
$jsonResponse = $this->controller->getAll($queryString);
$this->assertEquals(
$expected,
$jsonResponse->getContent()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment