Преглед изворни кода

Merge remote branch 'digitalkaoz/httpkernel-datacollector'

* digitalkaoz/httpkernel-datacollector:
  [HttpKernel] added Tests for DataCollectors
  [HttpFoundation] more sophisticated checks for valid expiration
Fabien Potencier пре 14 година
родитељ
комит
c02ebbf2d8

+ 6 - 1
src/Symfony/Component/HttpFoundation/Cookie.php

@@ -40,11 +40,16 @@ class Cookie
         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;

+ 8 - 0
tests/Symfony/Tests/Component/HttpFoundation/CookieTest.php

@@ -68,6 +68,14 @@ class CookieTest extends \PHPUnit_Framework_TestCase
     {
         new Cookie('MyCookie', $value);
     }
+    
+    /**
+     * @expectedException InvalidArgumentException
+     */
+    public function testInvalidExpiration()
+    {
+        $cookie = new Cookie('MyCookie', 'foo','bar');        
+    }
 
     /**
      * @covers Symfony\Component\HttpFoundation\Cookie::getValue

+ 73 - 0
tests/Symfony/Tests/Component/HttpKernel/DataCollector/ConfigDataCollectorTest.php

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

+ 48 - 0
tests/Symfony/Tests/Component/HttpKernel/DataCollector/EventDataCollectorTest.php

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

+ 40 - 0
tests/Symfony/Tests/Component/HttpKernel/DataCollector/ExceptionDataCollectorTest.php

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

+ 52 - 0
tests/Symfony/Tests/Component/HttpKernel/DataCollector/LoggerDataCollectorTest.php

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

+ 30 - 0
tests/Symfony/Tests/Component/HttpKernel/DataCollector/MemoryDataCollectorTest.php

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

+ 62 - 0
tests/Symfony/Tests/Component/HttpKernel/DataCollector/RequestDataCollectorTest.php

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