Browse Source

Added AdminExtractorTest

Andrej Hudec 11 years ago
parent
commit
09cd81e9fe

+ 3 - 7
Admin/Admin.php

@@ -1321,9 +1321,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns a form depend on the given $object
-     *
-     * @return \Symfony\Component\Form\Form
+     * {@inheritdoc}
      */
     public function getForm()
     {
@@ -1933,9 +1931,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * @param string $action
-     *
-     * @return array
+     * {@inheritdoc}
      */
     public function getBreadcrumbs($action)
     {
@@ -2120,7 +2116,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * @return \Symfony\Component\Translation\TranslatorInterface
+     * {@inheritdoc}
      */
     public function getTranslator()
     {

+ 25 - 0
Admin/AdminInterface.php

@@ -70,12 +70,21 @@ interface AdminInterface
     public function getDatagridBuilder();
 
     /**
+     * Set translator
+     *
      * @param \Symfony\Component\Translation\TranslatorInterface $translator
      *
      * @return void
      */
     public function setTranslator(TranslatorInterface $translator);
 
+    /**
+     * Get translator
+     *
+     * @return \Symfony\Component\Translation\TranslatorInterface
+     */
+    public function getTranslator();
+
     /**
      * @param \Symfony\Component\HttpFoundation\Request $request
      *
@@ -194,6 +203,13 @@ interface AdminInterface
      */
     public function getFormFieldDescriptions();
 
+    /**
+     * Returns a form depend on the given $object
+     *
+     * @return \Symfony\Component\Form\Form
+     */
+    public function getForm();
+
     /**
      * @return \Symfony\Component\HttpFoundation\Request
      *
@@ -814,4 +830,13 @@ interface AdminInterface
      * @return string
      */
     public function getLabel();
+
+    /**
+     * Get breadcrumbs for $action
+     *
+     * @param string $action
+     *
+     * @return array
+     */
+    public function getBreadcrumbs($action);
 }

+ 7 - 0
CHANGELOG.md

@@ -1,6 +1,13 @@
 CHANGELOG
 =========
 
+### 2013-09-20
+
+* [BC BREAK] added ``getTranslator``, ``getForm``, ``getBreadcrumbs``
+  to the AdminInterface
+  If you do not extend the Admin class, you need to add these methods to
+  your admin.
+
 ### 2013-09-13
 
 * [BC BREAK] added ``getMaxPerPage``, ``setMaxPerPage``, ``setPage``,

+ 117 - 0
Tests/Translator/Extractor/JMSTranslatorBundle/AdminExtractorTest.php

@@ -0,0 +1,117 @@
+<?php
+
+namespace Sonata\AdminBundle\Translator\Extractor\JMSTranslatorBundle;
+
+use Symfony\Component\HttpKernel\Log\LoggerInterface;
+use Sonata\AdminBundle\Admin\Pool;
+use Sonata\AdminBundle\Admin\AdminInterface;
+use JMS\TranslationBundle\Model\MessageCatalogue;
+use JMS\TranslationBundle\Model\Message;
+use Sonata\AdminBundle\Translator\Extractor\JMSTranslatorBundle\AdminExtractor;
+
+/**
+ * Test for AdminExtractor
+ *
+ * @author Andrej Hudec <pulzarraider@gmail.com>
+ */
+class AdminExtractorTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @var AdminExtractor
+     */
+    private $adminExtractor;
+
+    /**
+     * @var Pool
+     */
+    private $pool;
+
+    /**
+     * @var AdminInterface
+     */
+    private $fooAdmin;
+
+    /**
+     * @var AdminInterface
+     */
+    private $barAdmin;
+
+    public function setUp()
+    {
+        $this->fooAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $this->barAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+
+        //php 5.3 BC
+        $fooAdmin = $this->fooAdmin;
+        $barAdmin = $this->barAdmin;
+
+        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+        $container->expects($this->any())
+            ->method('get')
+            ->will($this->returnCallback(function($id) use ($fooAdmin, $barAdmin) {
+                switch ($id) {
+                    case 'foo_admin':
+                        return $fooAdmin;
+                    case 'bar_admin':
+                        return $barAdmin;
+                }
+
+                return null;
+            }));
+
+        $logger = $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
+
+        $this->pool = new Pool($container, '', '');
+        $this->pool->setAdminServiceIds(array('foo_admin', 'bar_admin'));
+
+        $this->adminExtractor = new AdminExtractor($this->pool, $logger);
+        $this->adminExtractor->setLogger($logger);
+    }
+
+    public function testExtractEmpty()
+    {
+        $catalogue = $this->adminExtractor->extract();
+
+        $this->assertInstanceOf('JMS\TranslationBundle\Model\MessageCatalogue', $catalogue);
+        $this->assertFalse($catalogue->has(new Message('foo', 'foo_admin_domain')));
+    }
+
+    public function testExtract()
+    {
+        //php 5.3 BC
+        $translator = $this->adminExtractor;
+
+        $this->fooAdmin->expects($this->any())
+            ->method('getShow')
+            ->will($this->returnCallback(function() use ($translator) {
+                $translator->trans('foo', array(), 'foo_admin_domain');
+                $translator->transChoice('foo', 1, array(), 'foo_admin_domain');
+
+                return null;
+            }));
+
+        $catalogue = $this->adminExtractor->extract();
+
+        $this->assertTrue($catalogue->has(new Message('foo', 'foo_admin_domain')));
+        $this->assertFalse($catalogue->has(new Message('nonexistent', 'foo_admin_domain')));
+
+        $this->assertInstanceOf('JMS\TranslationBundle\Model\Message', $catalogue->get('foo', 'foo_admin_domain'));
+
+        $message = $catalogue->get('foo', 'foo_admin_domain');
+        $this->assertEquals('foo', $message->getId());
+        $this->assertEquals('foo_admin_domain', $message->getDomain());
+    }
+
+    public function testExtractWithExcewption()
+    {
+        $this->setExpectedException('RuntimeException', 'Foo throws exception');
+
+        $this->fooAdmin->expects($this->any())
+            ->method('getShow')
+            ->will($this->returnCallback(function() {
+                throw new \RuntimeException('Foo throws exception');
+            }));
+
+        $catalogue = $this->adminExtractor->extract();
+    }
+}

+ 4 - 1
Translator/Extractor/JMSTranslatorBundle/AdminExtractor.php

@@ -49,7 +49,10 @@ class AdminExtractor implements ExtractorInterface, TranslatorInterface, Securit
     }
 
     /**
-     * @return bool
+     * Extract messages to MessageCatalogue
+     *
+     * @return MessageCatalogue
+     *
      * @throws \Exception|\RuntimeException
      */
     public function extract()