[HttpKernel] added Tests for DataCollectors

This commit is contained in:
Robert Schönthal 2011-03-06 00:15:01 +01:00
parent 87e1359ebd
commit 44b0bbc0d1
6 changed files with 305 additions and 0 deletions

View File

@ -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) {
}
}

View File

@ -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');
}
}

View File

@ -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());
}
}

View File

@ -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');
}
}

View File

@ -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());
}
}

View File

@ -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)
);
}
}