Sfoglia il codice sorgente

Additional tests for PhpEngine + Helpers
More tests for UrlValidator

stloyd 14 anni fa
parent
commit
99f9337517

+ 5 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/CodeHelperTest.php

@@ -55,4 +55,9 @@ class CodeHelperTest extends \PHPUnit_Framework_TestCase
             array('Method', '<abbr title="Method">Method</abbr>()')
         );
     }
+
+    public function testGetName()
+    {
+        $this->assertEquals('code', self::$helper->getName());
+    }
 }

+ 43 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/RequestHelperTest.php

@@ -0,0 +1,43 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Bundle\FrameworkBundle\Templating\Helper\RequestHelper;
+
+class RequestHelperTest extends \PHPUnit_Framework_TestCase
+{
+    protected $request;
+
+    public function setUp()
+    {
+		$this->request = new Request();
+        $this->request->initialize(array('foobar' => 'bar'));
+    }
+
+	public function testGetParameter()
+	{
+        $helper = new RequestHelper($this->request);
+
+        $this->assertEquals('bar', $helper->getParameter('foobar'));
+        $this->assertEquals('foo', $helper->getParameter('bar', 'foo'));
+
+        $this->assertNull($helper->getParameter('foo'));
+	}
+
+	public function testGetName()
+	{
+        $helper = new RequestHelper($this->request);
+
+        $this->assertEquals('request', $helper->getName());
+	}
+}

+ 62 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php

@@ -0,0 +1,62 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Session;
+use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
+use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper;
+
+class SessionHelperTest extends \PHPUnit_Framework_TestCase
+{
+    protected $request;
+
+    public function setUp()
+    {
+		$this->request = new Request();
+		
+		$session = new Session(new ArraySessionStorage());
+		$session->set('foobar', 'bar');
+		$session->setFlash('foo', 'bar');
+
+        $this->request->setSession($session);
+    }
+
+	public function testFlash()
+	{
+        $helper = new SessionHelper($this->request);
+
+        $this->assertTrue($helper->hasFlash('foo'));
+
+        $this->assertEquals('bar', $helper->getFlash('foo'));
+        $this->assertEquals('foo', $helper->getFlash('bar', 'foo'));
+
+        $this->assertNull($helper->getFlash('foobar'));
+	}
+
+	public function testGet()
+	{
+        $helper = new SessionHelper($this->request);
+
+        $this->assertEquals('bar', $helper->get('foobar'));
+        $this->assertEquals('foo', $helper->get('bar', 'foo'));
+
+        $this->assertNull($helper->get('foo'));
+	}
+
+	public function testGetName()
+	{
+        $helper = new SessionHelper($this->request);
+
+        $this->assertEquals('session', $helper->getName());
+	}
+}

+ 12 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php

@@ -43,6 +43,18 @@ class PhpEngineTest extends TestCase
         $this->assertEmpty($globals['app']->getRequest());
     }
 
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testGetInvalidHelper()
+    {
+        $container = $this->getContainer();
+        $loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
+        $engine = new PhpEngine(new TemplateNameParser(), $container, $loader);
+
+        $engine->get('non-existing-helper');
+    }
+
     /**
      * Creates a Container with a Session-containing Request service.
      *

+ 44 - 0
tests/Symfony/Tests/Component/Validator/Constraints/UrlValidatorTest.php

@@ -51,14 +51,28 @@ class UrlValidatorTest extends \PHPUnit_Framework_TestCase
     public function getValidUrls()
     {
         return array(
+            array('http://a.pl'),
             array('http://www.google.com'),
+            array('http://www.google.museum'),
+            array('http://google.বাংলা/'),
+            array('http://google.ąęź/'),
             array('https://google.com/'),
             array('https://google.com:80/'),
+            array('http://א-ת.com/'),
+            array('http://żółw.żółw/'),
+            array('http://àlàl.com:80/'),
+            array('http://www.example.coop/'),
+            array('http://www.test-example.com/'),
             array('http://www.symfony.com/'),
+            array('http://symfony.fake/blog/'),
+            array('http://symfony.com/search?type=&q=url+validator'),
+            array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
+            array('http://very.long.domain.name.com/'),
             array('http://127.0.0.1/'),
             array('http://127.0.0.1:80/'),
             array('http://[::1]/'),
             array('http://[::1]:80/'),
+            array('http://[1:2:3::4:5:6:7]/'),
         );
     }
 
@@ -74,10 +88,40 @@ class UrlValidatorTest extends \PHPUnit_Framework_TestCase
     {
         return array(
             array('google.com'),
+            array('://google.com'),
+            array('http ://google.com'),
             array('http:/google.com'),
+            array('http://goog_le.com'),
             array('http://google.com::aa'),
+            array('http://google.com:aa'),
             array('http://google.foobar'),
             array('ftp://google.fr'),
+            array('faked://google.fr'),
+            array('http://127.0.0.1:aa/'),
+            array('http://127.0.0.1:123456/'),
+            array('ftp://[::1]/'),
+            array('http://[::1'),
+        );
+    }
+
+    /**
+     * @dataProvider getValidCustomUrls
+     */
+    public function testCustomProtocolIsValid($url)
+    {
+        $constraint = new Url(array(
+            'protocols' => array('ftp', 'file', 'git')
+        ));
+
+        $this->assertTrue($this->validator->isValid($url, $constraint));
+    }
+
+    public function getValidCustomUrls()
+    {
+        return array(
+            array('ftp://google.com'),
+            array('file://127.0.0.1'),
+            array('git://[::1]/'),
         );
     }