Ver código fonte

[sluggable] validate boolean annotation settings, closes #206

gedi 13 anos atrás
pai
commit
0bea9d713f

+ 6 - 0
lib/Gedmo/Sluggable/Mapping/Driver/Annotation.php

@@ -128,6 +128,12 @@ class Annotation implements AnnotationDriverInterface
                         throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
                         throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
                     }
                     }
                 }
                 }
+                if (!is_bool($slug->updatable)) {
+                    throw new InvalidMappingException("Slug annotation [updatable], type is not valid and must be 'boolean' in class - {$meta->name}");
+                }
+                if (!is_bool($slug->unique)) {
+                    throw new InvalidMappingException("Slug annotation [unique], type is not valid and must be 'boolean' in class - {$meta->name}");
+                }
                 // set all options
                 // set all options
                 $config['slugs'][$field] = array(
                 $config['slugs'][$field] = array(
                     'fields' => $slug->fields,
                     'fields' => $slug->fields,

+ 50 - 0
tests/Gedmo/Sluggable/AnnotationValidationTest.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace Gedmo\Sluggable;
+
+use Doctrine\Common\EventManager;
+use Tool\BaseTestCaseORM;
+use Sluggable\Fixture\Validate;
+
+/**
+ * These are tests for Sluggable behavior
+ *
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Sluggable
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class AnnotationValidationTest extends BaseTestCaseORM
+{
+    const TARGET = 'Sluggable\\Fixture\\Validate';
+
+    /**
+     * @test
+     * @expectedException Gedmo\Exception\InvalidMappingException
+     */
+    function shouldFailValidationOnInvalidAnnotation()
+    {
+        $evm = new EventManager;
+        $evm->addEventSubscriber(new SluggableListener);
+        $this->getMockSqliteEntityManager($evm);
+
+        $slug = new Validate;
+        $slug->setTitle('My Slug');
+
+        $slug2 = new Validate;
+        $slug2->setTitle('My Slug');
+
+        $this->em->persist($slug);
+        $this->em->persist($slug2);
+        $this->em->flush();
+
+        $this->assertEquals('my-slug', $slug2->getSlug());
+    }
+
+    protected function getUsedEntityFixtures()
+    {
+        return array(
+            self::TARGET
+        );
+    }
+}

+ 50 - 0
tests/Gedmo/Sluggable/Fixture/Validate.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace Sluggable\Fixture;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ */
+class Validate
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(length=32)
+     */
+    private $title;
+
+    /**
+     * @Gedmo\Slug(updatable="false", fields={"title"}, unique="false")
+     * @ORM\Column(length=64, unique=true)
+     */
+    private $slug;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function getSlug()
+    {
+        return $this->slug;
+    }
+}