Skip to content

Instantly share code, notes, and snippets.

@havvg
Created November 30, 2012 09:52
Show Gist options
  • Save havvg/4174855 to your computer and use it in GitHub Desktop.
Save havvg/4174855 to your computer and use it in GitHub Desktop.
Symfony2 controller test "304 - Not Modified"
<?php
namespace Ormigo\Tests;
use DateTime;
use DateTimeZone;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
abstract class AbstractFunctionalTest extends AbstractTest
{
// ..
public static function assertNotModifiedResponse(Response $response)
{
self::assertEquals(304, $response->getStatusCode());
}
/**
* Assert that the request will result in a Not-Modified response.
*
* @param string $uri The URI to request.
* @param Client $client The client to issue the request with.
* @param DateTime $date The date time to be sent as If-Modified-Since header.
* @param string $method The HTTP verb for the request.
*
* @return Response
*/
public static function assertNotModifiedSince($uri, Client $client, DateTime $date, $method = 'GET')
{
$lastModified = clone $date;
$lastModified->setTimezone(new DateTimeZone('UTC'));
$ifModifiedSince = $date->format('D, d M Y H:i:s').' GMT';
$client->request($method, $uri, array(), array(), array(
'HTTP_IF_MODIFIED_SINCE' => $ifModifiedSince,
));
$response = $client->getResponse();
self::assertNotModifiedResponse($response);
return $response;
}
}
<?php
namespace Ormigo\Bundle\OrmigoBundle\Tests\Controller;
use Ormigo\Tests\AbstractFunctionalTest;
use Ormigo\Model\Banking\Bank;
use Ormigo\Model\Banking\BankQuery;
/**
* @covers \Ormigo\Bundle\OrmigoBundle\Controller\BankController
*/
class BankControllerTest extends AbstractFunctionalTest
{
// ..
public function testShowNotModified()
{
/* @var $bank Bank */
$bank = BankQuery::create()->findOneByBic('HYVEDEMM488');
$uri = sprintf('/bank/%s.de_DE.json', $bank->getId());
$this->assertNotModifiedSince($uri, static::createClient(), $bank->getVersionCreatedAt());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment