Browse Source

Merge remote branch 'pborreli/unit-test-coverage'

* pborreli/unit-test-coverage:
  [Validator] Added unit tests
  [Locale] Silenting error to test return value
  [Locale] Added symfony 1 test cases
  [EventDispatcher] Full coverage
  [DomCrawler] Full coverage
  [Translation] Full coverage
Fabien Potencier 14 years ago
parent
commit
787935f5dc

+ 5 - 0
tests/Symfony/Tests/Component/DomCrawler/CrawlerTest.php

@@ -62,6 +62,11 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
         $crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
 
         $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string');
+
+        $crawler->addHtmlContent('<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>', 'UTF-8');
+
+        $this->assertEquals('http://symfony.com', $crawler->filter('base')->attr('href'), '->addHtmlContent() adds nodes from an HTML string');
+        $this->assertEquals('http://symfony.com/contact', $crawler->filter('a')->link()->getUri(), '->addHtmlContent() adds nodes from an HTML string');
     }
 
     /**

+ 1 - 0
tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php

@@ -147,6 +147,7 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
         $this->dispatcher->removeListener(array('preBar'), $this->listener);
         $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
+        $this->dispatcher->removeListener(array('notExists'), $this->listener);
     }
 
     public function testAddSubscriber()

+ 19 - 1
tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php

@@ -158,12 +158,18 @@ class StubNumberFormatterTest extends LocaleTestCase
             array(-100, 'JPY', '(¥100)'),
             array(1000.12, 'JPY', '¥1,000'),
 
+            array(100, 'EUR', '€100.00'),
+            array(-100, 'EUR', '(€100.00)'),
+            array(1000.12, 'EUR', '€1,000.12'),
+
             // Rounding checks
             array(1000.121, 'BRL', 'R$1,000.12'),
             array(1000.123, 'BRL', 'R$1,000.12'),
             array(1000.125, 'BRL', 'R$1,000.12'),
             array(1000.127, 'BRL', 'R$1,000.13'),
             array(1000.129, 'BRL', 'R$1,000.13'),
+            array(11.50999, 'BRL', 'R$11.51'),
+            array(11.9999464, 'BRL', 'R$12.00')
         );
     }
 
@@ -200,13 +206,19 @@ class StubNumberFormatterTest extends LocaleTestCase
             array(100, 'CHF', $chf, '%s100.00'),
             array(-100, 'CHF', $chf, '(%s100.00)'),
             array(1000.12, 'CHF', $chf, '%s1,000.10'),
+            array('1000.12', 'CHF', $chf, '%s1,000.10'),
 
             // Rounding checks
             array(1000.121, 'CHF', $chf, '%s1,000.10'),
             array(1000.123, 'CHF', $chf, '%s1,000.10'),
             array(1000.125, 'CHF', $chf, '%s1,000.10'),
             array(1000.127, 'CHF', $chf, '%s1,000.15'),
-            array(1000.129, 'CHF', $chf, '%s1,000.15')
+            array(1000.129, 'CHF', $chf, '%s1,000.15'),
+
+            array(1200000.00, 'CHF', $chf, '%s1,200,000.00'),
+            array(1200000.1, 'CHF', $chf, '%s1,200,000.10'),
+            array(1200000.10, 'CHF', $chf, '%s1,200,000.10'),
+            array(1200000.101, 'CHF', $chf, '%s1,200,000.10')
         );
     }
 
@@ -356,6 +368,12 @@ class StubNumberFormatterTest extends LocaleTestCase
         $formatter->format(1, StubNumberFormatter::TYPE_CURRENCY);
     }
 
+    public function testFormatTypeCurrencyReturnStub()
+    {
+        $formatter = $this->getStubFormatterWithDecimalStyle();
+        $this->assertFalse(@$formatter->format(1, StubNumberFormatter::TYPE_CURRENCY));
+    }
+
     /**
      * @dataProvider formatTypeCurrencyProvider
      * @expectedException PHPUnit_Framework_Error_Warning

+ 10 - 0
tests/Symfony/Tests/Component/Translation/Loader/CsvFileLoaderTest.php

@@ -37,4 +37,14 @@ class CsvFileLoaderTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('en', $catalogue->getLocale());
         $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
     }
+
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testLoadThrowsAnExceptionIfFileNotExists()
+    {
+        $loader = new CsvFileLoader();
+        $resource = __DIR__.'/../fixtures/not-exists.csv';
+        $loader->load($resource, 'en', 'domain1');
+    }
 }

+ 4 - 1
tests/Symfony/Tests/Component/Translation/fixtures/resources.csv

@@ -1 +1,4 @@
-"foo"; "bar"
+"foo"; "bar"
+#"bar"; "foo"
+"incorrect"; "number"; "columns"; "will"; "be"; "ignored"
+"incorrect"

+ 5 - 0
tests/Symfony/Tests/Component/Validator/ConstraintTest.php

@@ -78,6 +78,11 @@ class ConstraintTest extends \PHPUnit_Framework_TestCase
         new ConstraintC();
     }
 
+    public function testRequiredOptionsPassed()
+    {
+        new ConstraintC(array('option1' => 'default'));
+    }
+
     public function testGroupsAreConvertedToArray()
     {
         $constraint = new ConstraintA(array('groups' => 'Foo'));

+ 102 - 0
tests/Symfony/Tests/Component/Validator/ExecutionContextTest.php

@@ -0,0 +1,102 @@
+<?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\Tests\Component\Validator;
+
+use Symfony\Component\Validator\ExecutionContext;
+
+class ExecutionContextTest extends \PHPUnit_Framework_TestCase
+{
+    protected $walker;
+    protected $metadataFactory;
+    protected $context;
+
+    protected function setUp()
+    {
+        $this->walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
+        $this->metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
+        $this->context = new ExecutionContext('Root', $this->walker, $this->metadataFactory);
+    }
+
+    public function testClone()
+    {
+        $clone = clone $this->context;
+
+        $this->assertNotSame($this->context, $clone);
+    }
+
+    public function testAddViolation()
+    {
+        $this->assertEquals(0, count($this->context->getViolations()));
+        $this->context->addViolation('', array(), '');
+
+        $this->assertEquals(1, count($this->context->getViolations()));
+    }
+
+    public function testGetViolations()
+    {
+        $this->context->addViolation('', array(), '');
+
+        $this->assertEquals(1, count($this->context->getViolations()));
+        $this->assertInstanceOf('Symfony\Component\Validator\ConstraintViolationList', $this->context->getViolations());
+    }
+
+    public function testGetRoot()
+    {
+        $this->assertEquals('Root', $this->context->getRoot());
+    }
+
+    public function testSetGetPropertyPath()
+    {
+        $this->context->setPropertyPath('property_path');
+
+        $this->assertEquals('property_path', $this->context->getPropertyPath());
+    }
+
+    public function testSetGetCurrentClass()
+    {
+        $this->context->setCurrentClass('current_class');
+
+        $this->assertEquals('current_class', $this->context->getCurrentClass());
+    }
+
+    public function testSetGetCurrentProperty()
+    {
+        $this->context->setCurrentProperty('current_property');
+
+        $this->assertEquals('current_property', $this->context->getCurrentProperty());
+    }
+
+    public function testSetGetGroup()
+    {
+        $this->context->setGroup('group');
+
+        $this->assertEquals('group', $this->context->getGroup());
+    }
+
+    public function testGetGraphWalker()
+    {
+        $this->assertSame($this->walker, $this->context->getGraphWalker());
+        $this->assertInstanceOf(
+            'Symfony\Component\Validator\GraphWalker',
+            $this->context->getGraphWalker()
+        );
+    }
+
+    public function testGetMetadataFactory()
+    {
+        $this->assertSame($this->metadataFactory, $this->context->getMetadataFactory());
+        $this->assertInstanceOf(
+            'Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface',
+            $this->context->getMetadataFactory()
+        );
+    }
+}

+ 8 - 0
tests/Symfony/Tests/Component/Validator/ValidatorTest.php

@@ -146,4 +146,12 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals(1, count($result));
     }
+
+    public function testGetMetadataFactory()
+    {
+        $this->assertInstanceOf(
+            'Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface',
+            $this->validator->getMetadataFactory()
+        );
+    }
 }