Bläddra i källkod

[HttpKernel] added tests for debug stuff

Robert Schönthal 14 år sedan
förälder
incheckning
3e7ccae2ba

+ 67 - 0
tests/Symfony/Tests/Component/HttpKernel/Debug/ErrorHandlerTest.php

@@ -0,0 +1,67 @@
+<?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\Debug;
+
+use Symfony\Component\HttpKernel\Debug\ErrorHandler;
+use Symfony\Component\HttpKernel\Debug\ErrorException;
+
+/**
+ * ErrorHandlerTest
+ *
+ * @author Robert Schönthal <seroscho@googlemail.com>
+ */
+class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
+{
+    
+    public function testConstruct()
+    {
+        $e = new ErrorHandler(3);
+        
+        $this->assertInstanceOf('Symfony\Component\HttpKernel\Debug\ErrorHandler',$e);
+        
+        $level = new \ReflectionProperty(get_class($e),'level');
+        $level->setAccessible(true);
+        
+        $this->assertEquals(3,$level->getValue($e));
+    }
+    
+    public function testRegister()
+    {
+        $e = new ErrorHandler(3);
+        $e = $this->getMock(get_class($e), array('handle'));
+        $e->expects($this->once())->method('handle');
+        
+        $e->register();
+        
+        try{
+            trigger_error('foo');
+        }catch(\Exception $e){
+        }
+    }
+
+    public function testHandle()
+    {
+        $e = new ErrorHandler(0);
+        $this->assertFalse($e->handle(0,'foo','foo.php',12,'foo'));
+        
+        $e = new ErrorHandler(3);
+        $this->assertFalse($e->handle(4,'foo','foo.php',12,'foo'));
+        
+        $e = new ErrorHandler(3);
+        try{
+            $e->handle(1, 'foo', 'foo.php', 12,'foo');
+        }catch(\ErrorException $e){
+            $this->assertSame('1: foo in foo.php line 12',$e->getMessage());
+        }
+    }
+
+}

+ 128 - 0
tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionListenerTest.php

@@ -0,0 +1,128 @@
+<?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\Debug;
+
+use Symfony\Component\HttpKernel\Debug\ExceptionListener;
+use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
+use Symfony\Component\EventDispatcher\Event;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpKernel\Debug\ErrorException;
+use Symfony\Tests\Component\HttpKernel\Logger;
+
+/**
+ * ExceptionListenerTest
+ *
+ * @author Robert Schönthal <seroscho@googlemail.com>
+ */
+class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
+{
+    public function testConstruct()
+    {
+        $logger = new TestLogger();
+        $l = new ExceptionListener('foo', $logger);
+        
+        $_logger = new \ReflectionProperty(get_class($l),'logger');
+        $_logger->setAccessible(true);
+        $_controller = new \ReflectionProperty(get_class($l),'controller');
+        $_controller->setAccessible(true);
+        
+        $this->assertSame($logger,$_logger->getValue($l));
+        $this->assertSame('foo',$_controller->getValue($l));
+    }
+    
+    /**
+     * @dataProvider provider
+     */
+    public function testHandleWithoutLogger($event,$event2)
+    {
+        //store the current error_log, and set the new one to dev/null
+        $error_log = ini_get('error_log');
+        ini_set('error_log','/dev/null');
+        
+        $l = new ExceptionListener('foo');
+
+        $this->assertEquals('foo', $l->handle($event));
+        
+        try{
+            $response = $l->handle($event2);
+        }catch(\Exception $e){
+            $this->assertSame('foo',$e->getMessage());
+        }
+        
+        //restore the old error_log
+        ini_set('error_log',$error_log);
+    }
+    
+    /**
+     * @dataProvider provider
+     */
+    public function testHandleWithLogger($event, $event2)
+    {
+        $logger = new TestLogger();
+        
+        $l = new ExceptionListener('foo',$logger);
+        
+        $this->assertSame('foo', $l->handle($event));
+        
+        try{
+            $response = $l->handle($event2);
+        }catch(\Exception $e){
+            $this->assertSame('foo',$e->getMessage());
+        }
+        
+        $this->assertEquals(3,$logger->countErrors());
+        $this->assertEquals(3,count($logger->getLogs('err')));
+    }
+    
+    public function provider()
+    {
+        $args = array('exception'=>new ErrorException('foo'),'request'=>new Request());
+        
+        $event = new Event(new Subject(),'bar',$args);
+        $event2 = new Event(new SubjectException(),'bar',$args);
+
+        return array(
+            array($event,$event2)
+        );
+    }
+    
+}
+
+class TestLogger extends Logger implements DebugLoggerInterface
+{
+    public function countErrors()
+    {
+        return count($this->logs['err']);
+    }
+    
+    public function getDebugLogger()
+    {
+        return new static();
+    }
+}
+
+class Subject
+{
+    public function handle()
+    {
+        return 'foo';
+    }
+ 
+}
+
+class SubjectException
+{
+    public function handle()
+    {
+        throw new \Exception('bar');
+    }
+}