Skip to content

[HttpKernel] Tests for DataCollectors [HttpFoundation] more sophisticated expiration check #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Mar 6, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Symfony/Component/HttpFoundation/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ public function __construct($name, $value = null, $expire = 0, $path = null, $do
if (empty($name)) {
throw new \InvalidArgumentException('The cookie name cannot be empty');
}

//check if the expiration is valid
if(!$expire instanceof \DateTime && !is_numeric($expire) && (strtotime($expire) === false || strtotime($expire) === -1)){
throw new \InvalidArgumentException('The cookie expiration is not valid');
}

$this->name = $name;
$this->value = $value;
$this->domain = $domain;
$this->expire = (integer) $expire;
$this->expire = $expire;
$this->path = $path;
$this->secure = (Boolean) $secure;
$this->httponly = (Boolean) $httponly;
Expand Down
8 changes: 8 additions & 0 deletions tests/Symfony/Tests/Component/HttpFoundation/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public function testInstantiationThrowsExceptionIfCookieValueContainsInvalidChar
{
new Cookie('MyCookie', $value);
}

/**
* @expectedException InvalidArgumentException
*/
public function testInvalidExpiration()
{
$cookie = new Cookie('MyCookie', 'foo','bar');
}

/**
* @covers Symfony\Component\HttpFoundation\Cookie::getValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\ConfigDataCollector;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ConfigDataCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testCollect()
{
$kernel = new KernelForTest('test',true);
$c = new ConfigDataCollector($kernel);
$c->collect(new Request(), new Response());

$this->assertSame('test',$c->getEnv());
$this->assertTrue($c->isDebug());
$this->assertSame('config',$c->getName());
$this->assertSame('testkernel',$c->getAppName());
$this->assertSame(PHP_VERSION,$c->getPhpVersion());
$this->assertSame(Kernel::VERSION,$c->getSymfonyVersion());
$this->assertNull($c->getToken());

//if else clause because we dont know it
if(extension_loaded('xdebug')){
$this->assertTrue($c->hasXdebug());
}else{
$this->assertFalse($c->hasXdebug());
}

//if else clause because we dont know it
if(((extension_loaded('eaccelerator') && ini_get('eaccelerator.enable'))
||
(extension_loaded('apc') && ini_get('apc.enabled'))
||
(extension_loaded('xcache') && ini_get('xcache.cacher')))){
$this->assertTrue($c->hasAccelerator());
}else{
$this->assertFalse($c->hasAccelerator());
}

}

}

class KernelForTest extends Kernel
{
public function getName()
{
return 'testkernel';
}

public function registerRootDir() {
}

public function registerBundles() {
}

public function registerContainerConfiguration(LoaderInterface $loader) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\EventDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Debug\EventDispatcherTraceableInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;


class EventDataCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testCollect()
{
$c = new EventDataCollector();
$c->setEventDispatcher(new TestEventDispatcher());

$c->collect(new Request(), new Response());

$this->assertSame('events',$c->getName());
$this->assertSame(array('foo'),$c->getCalledListeners());
$this->assertSame(array('bar'),$c->getNotCalledListeners());
}

}

class TestEventDispatcher extends EventDispatcher implements EventDispatcherTraceableInterface
{
function getCalledListeners()
{
return array('foo');
}

function getNotCalledListeners()
{
return array('bar');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\ExceptionDataCollector;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ExceptionDataCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testCollect()
{
$e = new \Exception('foo',500);
$c = new ExceptionDataCollector();
$flattened = FlattenException::create($e);
$trace = $flattened->getTrace();

$this->assertFalse($c->hasException());

$c->collect(new Request(), new Response(),$e);

$this->assertTrue($c->hasException());
$this->assertEquals($flattened,$c->getException());
$this->assertSame('foo',$c->getMessage());
$this->assertSame(500,$c->getCode());
$this->assertSame('exception',$c->getName());
$this->assertSame($trace,$c->getTrace());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Tests\Component\HttpKernel\Logger;

class LoggerDataCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testCollect()
{
$c = new LoggerDataCollector(new TestLogger());

$c->collect(new Request(), new Response());

$this->assertSame('logger',$c->getName());
$this->assertSame(1337,$c->countErrors());
$this->assertSame(array('foo'),$c->getLogs());
}

}

class TestLogger extends Logger implements DebugLoggerInterface
{
public function countErrors()
{
return 1337;
}

public function getDebugLogger()
{
return new static();
}

public function getLogs($priority = false)
{
return array('foo');
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MemoryDataCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testCollect()
{
$c = new MemoryDataCollector();

$c->collect(new Request(), new Response());

$this->assertInternalType('integer',$c->getMemory());
$this->assertSame('memory',$c->getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;

class RequestDataCollectorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provider
*/
public function testCollect(Request $request, Response $response)
{
$c = new RequestDataCollector();

$c->collect($request, $response);

$this->assertSame('request',$c->getName());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\HeaderBag',$c->getRequestHeaders());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag',$c->getRequestServer());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag',$c->getRequestCookies());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag',$c->getRequestAttributes());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag',$c->getRequestRequest());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag',$c->getRequestQuery());
$this->assertEquals('html',$c->getFormat());
$this->assertEquals(array(),$c->getSessionAttributes());

$this->assertInstanceOf('Symfony\Component\HttpFoundation\HeaderBag',$c->getResponseHeaders());
$this->assertEquals(200,$c->getStatusCode());
$this->assertEquals('application/json',$c->getContentType());
}

public function provider()
{
$request = Request::create('http://test.com/foo?bar=baz');
$request->attributes->set('foo', 'bar');

$response = new Response();
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/json');
$response->headers->setCookie(new Cookie('foo','bar',1,'/foo','localhost',true,true));
$response->headers->setCookie(new Cookie('bar','foo',new \DateTime('@946684800')));
$response->headers->setCookie(new Cookie('bazz','foo','2000-12-12'));

return array(
array($request,$response)
);
}

}