From 44b0bbc0d1b64177c08f685eaf4f97e5576a228a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=B6nthal?= Date: Sun, 6 Mar 2011 00:15:01 +0100 Subject: [PATCH] [HttpKernel] added Tests for DataCollectors --- .../DataCollector/ConfigDataCollectorTest.php | 73 +++++++++++++++++++ .../DataCollector/EventDataCollectorTest.php | 48 ++++++++++++ .../ExceptionDataCollectorTest.php | 40 ++++++++++ .../DataCollector/LoggerDataCollectorTest.php | 52 +++++++++++++ .../DataCollector/MemoryDataCollectorTest.php | 30 ++++++++ .../RequestDataCollectorTest.php | 62 ++++++++++++++++ 6 files changed, 305 insertions(+) create mode 100644 tests/Symfony/Tests/Component/HttpKernel/DataCollector/ConfigDataCollectorTest.php create mode 100644 tests/Symfony/Tests/Component/HttpKernel/DataCollector/EventDataCollectorTest.php create mode 100644 tests/Symfony/Tests/Component/HttpKernel/DataCollector/ExceptionDataCollectorTest.php create mode 100644 tests/Symfony/Tests/Component/HttpKernel/DataCollector/LoggerDataCollectorTest.php create mode 100644 tests/Symfony/Tests/Component/HttpKernel/DataCollector/MemoryDataCollectorTest.php create mode 100644 tests/Symfony/Tests/Component/HttpKernel/DataCollector/RequestDataCollectorTest.php diff --git a/tests/Symfony/Tests/Component/HttpKernel/DataCollector/ConfigDataCollectorTest.php b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/ConfigDataCollectorTest.php new file mode 100644 index 0000000000..6187b10ca2 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/ConfigDataCollectorTest.php @@ -0,0 +1,73 @@ + + * + * 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) { + } +} \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/HttpKernel/DataCollector/EventDataCollectorTest.php b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/EventDataCollectorTest.php new file mode 100644 index 0000000000..54d64cfcb2 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/EventDataCollectorTest.php @@ -0,0 +1,48 @@ + + * + * 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'); + } +} diff --git a/tests/Symfony/Tests/Component/HttpKernel/DataCollector/ExceptionDataCollectorTest.php b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/ExceptionDataCollectorTest.php new file mode 100644 index 0000000000..e637a461fb --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/ExceptionDataCollectorTest.php @@ -0,0 +1,40 @@ + + * + * 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()); + } + +} diff --git a/tests/Symfony/Tests/Component/HttpKernel/DataCollector/LoggerDataCollectorTest.php b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/LoggerDataCollectorTest.php new file mode 100644 index 0000000000..2a2a096a12 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/LoggerDataCollectorTest.php @@ -0,0 +1,52 @@ + + * + * 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'); + } +} + diff --git a/tests/Symfony/Tests/Component/HttpKernel/DataCollector/MemoryDataCollectorTest.php b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/MemoryDataCollectorTest.php new file mode 100644 index 0000000000..11fabefd43 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/MemoryDataCollectorTest.php @@ -0,0 +1,30 @@ + + * + * 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()); + } + +} diff --git a/tests/Symfony/Tests/Component/HttpKernel/DataCollector/RequestDataCollectorTest.php b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/RequestDataCollectorTest.php new file mode 100644 index 0000000000..1a88fbf23b --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/DataCollector/RequestDataCollectorTest.php @@ -0,0 +1,62 @@ + + * + * 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) + ); + } + +}