Преглед изворни кода

[sluggable] fixed yaml mapping issue when there is no parameters set and slug is not an array key, closes #116

gediminasm пре 14 година
родитељ
комит
d8e8dc3cf8

+ 1 - 1
lib/Gedmo/Sluggable/Mapping/Driver/Yaml.php

@@ -74,7 +74,7 @@ class Yaml extends File implements Driver
                         $position = (isset($sluggable['position'])? $sluggable['position']:0);
                         $config['fields'][$slugField][] = array('field' => $field, 'position' => $position, 'slugField' => $slugField);
                     } elseif (isset($fieldMapping['gedmo']['slug']) || in_array('slug', $fieldMapping['gedmo'])) {
-                        $slug = $fieldMapping['gedmo']['slug'];
+                        $slug = isset($fieldMapping['gedmo']['slug']) ? $fieldMapping['gedmo']['slug'] : array();
                         if (!$this->isValidField($meta, $field)) {
                             throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
                         }

+ 31 - 0
tests/Gedmo/Sluggable/Fixture/Issue116/Country.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace Sluggable\Fixture\Issue116;
+
+class Country
+{
+    private $id;
+    private $languageCode;
+    private $originalName;
+    private $alias;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setOriginalName($originalName)
+    {
+        $this->originalName = $originalName;
+    }
+
+    public function getOriginalName()
+    {
+        return $this->originalName;
+    }
+
+    public function getAlias()
+    {
+        return $this->alias;
+    }
+}

+ 31 - 0
tests/Gedmo/Sluggable/Fixture/Issue116/Mapping/Sluggable.Fixture.Issue116.Country.dcm.yml

@@ -0,0 +1,31 @@
+Sluggable\Fixture\Issue116\Country:
+  type: entity
+  table: sta_country
+  fields:
+    id:
+      id: true
+      type: integer
+      unsigned: false
+      nullable: false
+      generator:
+        strategy: IDENTITY
+    alias:
+      type: string(50)
+      fixed: false
+      nullable: false
+      gedmo:
+        - slug
+    languageCode:
+      type: string(10)
+      fixed: false
+      nullable: true
+      column: language_code
+    originalName:
+      type: string(50)
+      fixed: false
+      nullable: false
+      column: original_name
+      gedmo:
+        sluggable:
+          slugField: 'alias'
+  lifecycleCallbacks: {  }

+ 60 - 0
tests/Gedmo/Sluggable/Issue116Test.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace Gedmo\Sluggable;
+
+use Doctrine\Common\EventManager;
+use Tool\BaseTestCaseORM;
+use Sluggable\Fixture\Issue116\Country;
+use Doctrine\ORM\Mapping\Driver\YamlDriver;
+use Doctrine\ORM\Mapping\Driver\DriverChain;
+
+/**
+ * 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 Issue116Test extends BaseTestCaseORM
+{
+    const TARGET = 'Sluggable\\Fixture\\Issue116\\Country';
+
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $evm = new EventManager;
+        $evm->addEventSubscriber(new SluggableListener);
+
+        $this->getMockSqliteEntityManager($evm);
+    }
+
+    protected function getMetadataDriverImplementation()
+    {
+        $chain = new DriverChain;
+        $chain->addDriver(
+            new YamlDriver(array(__DIR__ . '/Fixture/Issue116/Mapping')),
+            'Sluggable\Fixture\Issue116'
+        );
+        return $chain;
+    }
+
+    public function testSlugGeneration()
+    {
+        $country = new Country;
+        $country->setOriginalName('New Zealand');
+
+        $this->em->persist($country);
+        $this->em->flush();
+
+        $this->assertEquals('new-zealand', $country->getAlias());
+    }
+
+    protected function getUsedEntityFixtures()
+    {
+        return array(
+            self::TARGET
+        );
+    }
+}