浏览代码

[OutputEscaper] Add some tests

Fabien Pennequin 14 年之前
父节点
当前提交
dbfa06c54f

+ 28 - 0
tests/Symfony/Tests/Component/OutputEscaper/ObjectDecoratorTest.php

@@ -30,6 +30,9 @@ class ObjectDecoratorTest extends \PHPUnit_Framework_TestCase
 
         $array = self::$escaped->getTitles();
         $this->assertEquals('<strong>escaped!</strong>', $array[2], 'The escaped object behaves like the real object');
+
+        $this->assertEquals('Hello <strong>Fabien</strong>', self::$escaped->sayHello('Fabien'), 'The escaped object behaves like the real object');
+        $this->assertEquals('Hello <strong>Fabien</strong>', self::$escaped->sayHello('Fabien', 'esc_raw'), 'The escaped object behaves like the real object');
     }
 
     public function testMagicToString()
@@ -47,6 +50,21 @@ class ObjectDecoratorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue(isset(self::$escaped->someMember), 'The escaped object behaves like the real object');
         $this->assertFalse(isset(self::$escaped->invalidMember), 'The escaped object behaves like the real object');
     }
+
+    public function testGetRaw()
+    {
+        $this->assertEquals('<em>escape me</em>', self::$escaped->getRaw('someMember'), '->getRaw() returns result with any escaping');
+    }
+
+    /**
+     * @expectedException LogicException
+     */
+    public function testGetRawException()
+    {
+        $object = new \stdClass();
+        $escaped = Escaper::escape('entities', $object);
+        $escaped->getRaw('something');
+    }
 }
 
 class OutputEscaperTest
@@ -63,8 +81,18 @@ class OutputEscaperTest
         return '<strong>escaped!</strong>';
     }
 
+    public function sayHello($name)
+    {
+        return sprintf('Hello <strong>%s</strong>', $name);
+    }
+
     public function getTitles()
     {
         return array(1, 2, '<strong>escaped!</strong>');
     }
+
+    public function get($key)
+    {
+        return isset($this->{$key}) ? $this->{$key} : null;
+    }
 }

+ 15 - 0
tests/Symfony/Tests/Component/OutputEscaper/SafeDecoratorTest.php

@@ -37,6 +37,13 @@ class SafeDecoratorTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('ok', $safe->doSomething(), '->__call() invokes the embedded method');
     }
 
+    public function testMagicToString()
+    {
+        $safe = new SafeDecorator(new TestClass4());
+
+        $this->assertEquals('TestClass4', (string)$safe, '->__toString() invokes the embedded __toString method');
+    }
+
     public function testMagicIssetAndUnset()
     {
         $safe = new SafeDecorator(new TestClass3());
@@ -93,3 +100,11 @@ class TestClass3
         $boolValue = true,
         $nullValue = null;
 }
+
+class TestClass4
+{
+    public function __toString()
+    {
+        return 'TestClass4';
+    }
+}