浏览代码

Merge pull request #4174 from greg0ire/increase_coverage

Increase coverage
Oskar Stark 8 年之前
父节点
当前提交
66518885aa

+ 61 - 0
Tests/Form/Type/AdminTypeTest.php

@@ -11,8 +11,11 @@
 
 namespace Sonata\AdminBundle\Tests\Form\Type;
 
+use Prophecy\Argument\Token\AnyValueToken;
+use Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension;
 use Sonata\AdminBundle\Form\Type\AdminType;
 use Symfony\Component\Form\Test\TypeTestCase;
+use Symfony\Component\Form\Tests\Fixtures\TestExtension;
 use Symfony\Component\OptionsResolver\OptionsResolver;
 
 class AdminTypeTest extends TypeTestCase
@@ -38,4 +41,62 @@ class AdminTypeTest extends TypeTestCase
         $this->assertSame('link_delete', $options['btn_delete']);
         $this->assertSame('SonataAdminBundle', $options['btn_catalogue']);
     }
+
+    public function testSubmitValidData()
+    {
+        if (!method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
+            $this->markTestSkipped('Testing ancient versions would be more complicated.');
+
+            return;
+        }
+        $parentAdmin = $this->prophesize('Sonata\AdminBundle\Admin\AdminInterface');
+        $parentField = $this->prophesize('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
+
+        $modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $modelManager->modelReverseTransform(
+            'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo',
+            array()
+        )->shouldBeCalled();
+
+        $admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
+        $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(false);
+        $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
+        $admin->isGranted('DELETE')->shouldBeCalled()->willReturn(false);
+        $admin->setSubject(null)->shouldBeCalled();
+        $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
+        $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
+        $admin->getClass()->shouldBeCalled()->willReturn('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo');
+
+        $field = $this->prophesize('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
+        $field->getAdmin()->shouldBeCalled();
+        $field->getName()->shouldBeCalled();
+        $field->getOption('edit', 'standard')->shouldBeCalled();
+        $field->getOption('inline', 'natural')->shouldBeCalled();
+        $field->getOption('block_name', false)->shouldBeCalled();
+        $formData = array();
+
+        $form = $this->factory->create(
+            'Sonata\AdminBundle\Form\Type\AdminType',
+            null,
+            array(
+                'sonata_field_description' => $field->reveal(),
+            )
+        );
+        $form->submit($formData);
+        $this->assertTrue($form->isSynchronized());
+    }
+
+    protected function getExtensions()
+    {
+        $extensions = parent::getExtensions();
+        $guesser = $this->prophesize('Symfony\Component\Form\FormTypeGuesserInterface')->reveal();
+        $extension = new TestExtension($guesser);
+
+        $extension->addTypeExtension(new FormTypeFieldExtension(array(), array()));
+        $extensions[] = $extension;
+
+        return $extensions;
+    }
 }

+ 47 - 0
Tests/Form/Type/ModelListTypeTest.php

@@ -0,0 +1,47 @@
+<?php
+
+/*
+ * This file is part of the Sonata Project package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Tests\Form\Type;
+
+use Symfony\Component\Form\Test\TypeTestCase;
+
+class ModelListTypeTest extends TypeTestCase
+{
+    private $modelManager;
+
+    protected function setUp()
+    {
+        if (!method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
+            $this->markTestSkipped('Testing ancient versions would be more complicated.');
+        }
+
+        $this->modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface');
+
+        parent::setUp();
+    }
+
+    public function testSubmitValidData()
+    {
+        $formData = 42;
+
+        $form = $this->factory->create(
+            'Sonata\AdminBundle\Form\Type\ModelListType',
+            null,
+            array(
+                'model_manager' => $this->modelManager->reveal(),
+                'class' => 'My\Entity',
+            )
+        );
+        $this->modelManager->find('My\Entity', 42)->shouldBeCalled();
+        $form->submit($formData);
+        $this->assertTrue($form->isSynchronized());
+    }
+}

+ 58 - 0
Tests/Form/Type/ModelReferenceTypeTest.php

@@ -0,0 +1,58 @@
+<?php
+
+/*
+ * This file is part of the Sonata Project package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Tests\Form\Type;
+
+use Sonata\AdminBundle\Form\Type\ModelReferenceType;
+use Symfony\Component\Form\PreloadedExtension;
+use Symfony\Component\Form\Test\TypeTestCase;
+
+class ModelReferenceTypeTest extends TypeTestCase
+{
+    private $modelManager;
+
+    protected function setUp()
+    {
+        if (!method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
+            $this->markTestSkipped('Testing ancient versions would be more complicated.');
+        }
+
+        $this->modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface');
+
+        parent::setUp();
+    }
+
+    public function testSubmitValidData()
+    {
+        $formData = 42;
+
+        $form = $this->factory->create(
+            'Sonata\AdminBundle\Form\Type\ModelReferenceType',
+            null,
+            array(
+                'model_manager' => $this->modelManager->reveal(),
+                'class' => 'My\Entity',
+            )
+        );
+        $this->modelManager->find('My\Entity', 42)->shouldBeCalled();
+        $form->submit($formData);
+        $this->assertTrue($form->isSynchronized());
+    }
+
+    protected function getExtensions()
+    {
+        return array(
+            new PreloadedExtension(array(
+                new ModelReferenceType($this->modelManager->reveal()),
+            ), array()),
+        );
+    }
+}

+ 74 - 0
Tests/Security/Acl/Permission/AdminPermissionMapTest.php

@@ -0,0 +1,74 @@
+<?php
+
+/*
+ * This file is part of the Sonata Project package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Tests\Security\Permission;
+
+use Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap;
+use Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder;
+
+class AdminPermissionMapTest extends \PHPUnit_Framework_TestCase
+{
+    protected function setUp()
+    {
+        $this->permissionMap = new AdminPermissionMap();
+    }
+
+    public function testGetMaskReturnsAnArrayOfMasks()
+    {
+        $reflection = new \ReflectionClass(
+            'Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'
+        );
+        foreach ($reflection->getConstants() as $permission) {
+            $masks = $this->permissionMap->getMasks(
+                $permission,
+                new \stdClass()
+            );
+
+            $this->assertInternalType('array', $masks);
+
+            foreach ($masks as $mask) {
+                $this->assertInternalType('string', MaskBuilder::getCode($mask));
+            }
+        }
+    }
+
+    public function testGetMaskReturnsNullIfPermissionIsNotSupported()
+    {
+        $this->assertNull($this->permissionMap->getMasks(
+            'unknown permission',
+            new \stdClass()
+        ));
+    }
+
+    public function permissionProvider()
+    {
+        $dataSet = array();
+        $reflection = new \ReflectionClass(
+            'Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'
+        );
+
+        foreach ($reflection->getConstants() as $permission) {
+            $dataSet[$permission] = array(true, $permission);
+        }
+
+        return $dataSet + array(
+            'unknown permission' => array(false, 'unknown permission'),
+        );
+    }
+
+    /**
+     * @dataProvider permissionProvider
+     */
+    public function testContainsReturnsABoolean($expectedResult, $permission)
+    {
+        $this->assertSame($expectedResult, $this->permissionMap->contains($permission));
+    }
+}