Browse Source

test for mongodb sluggable, unique slug support for mongo comming up

gediminasm 14 years ago
parent
commit
7d2656b003

+ 1 - 1
lib/Gedmo/Sluggable/AbstractSluggableListener.php

@@ -198,7 +198,7 @@ abstract class AbstractSluggableListener extends MappedEventSubscriber
         
         // cut slug if exceeded in length
         $mapping = $meta->getFieldMapping($config['slug']);
-        if (strlen($slug) > $mapping['length']) {
+        if (isset($mapping['length']) && strlen($slug) > $mapping['length']) {
             $slug = substr($slug, 0, $mapping['length']);
         }
 

+ 3 - 2
lib/Gedmo/Sluggable/Mapping/Driver/Annotation.php

@@ -63,7 +63,7 @@ class Annotation implements Driver
         foreach ($class->getProperties() as $property) {
             if ($meta->isMappedSuperclass && !$property->isPrivate() ||
                 $meta->isInheritedField($property->name) ||
-                $meta->isInheritedAssociation($property->name)
+                isset($meta->associationMappings[$property->name]['inherited'])
             ) {
                 continue;
             }
@@ -109,6 +109,7 @@ class Annotation implements Driver
      */
     protected function _isValidField($meta, $field)
     {
-        return in_array($meta->getTypeOfField($field), $this->_validTypes);
+        $mapping = $meta->getFieldMapping($field);
+        return $mapping && in_array($mapping['type'], $this->_validTypes);
     }
 }

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

@@ -104,6 +104,7 @@ class Yaml extends File implements Driver
      */
     protected function _isValidField($meta, $field)
     {
-        return in_array($meta->getTypeOfField($field), $this->_validTypes);
+        $mapping = $meta->getFieldMapping($field);
+        return $mapping && in_array($mapping['type'], $this->_validTypes);
     }
 }

+ 2 - 1
lib/Gedmo/Sluggable/ODM/MongoDB/SluggableListener.php

@@ -3,7 +3,8 @@
 namespace Gedmo\Sluggable\ODM\MongoDB;
 
 use Doctrine\ODM\MongoDB\Events,
-    Doctrine\Common\EventArgs;
+    Doctrine\Common\EventArgs,
+    Gedmo\Sluggable\AbstractSluggableListener;
 
 /**
  * The SluggableListener handles the generation of slugs

+ 60 - 0
tests/Gedmo/Sluggable/Fixture/Document/Article.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace Sluggable\Fixture\Document;
+
+/** 
+ * @Document(collection="users")
+ */
+class Article
+{
+    /** @Id */
+    private $id;
+
+    /**
+     * @gedmo:Sluggable
+     * @String
+     */
+    private $title;
+
+    /**
+     * @gedmo:Sluggable
+     * @String
+     */
+    private $code;
+    
+    /**
+     * @gedmo:Slug
+     * @String
+     */
+    private $slug;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+    
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function setCode($code)
+    {
+        $this->code = $code;
+    }
+
+    public function getCode()
+    {
+        return $this->code;
+    }
+    
+    public function getSlug()
+    {
+        return $this->slug;
+    }
+}

+ 63 - 0
tests/Gedmo/Sluggable/Hydrator/SluggableFixtureDocumentArticleHydrator.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace Hydrator;
+
+use Doctrine\ODM\MongoDB\DocumentManager;
+use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
+use Doctrine\ODM\MongoDB\Hydrator\HydratorInterface;
+use Doctrine\ODM\MongoDB\UnitOfWork;
+
+/**
+ * THIS CLASS WAS GENERATED BY THE DOCTRINE ODM. DO NOT EDIT THIS FILE.
+ */
+class SluggableFixtureDocumentArticleHydrator implements HydratorInterface
+{
+    private $dm;
+    private $unitOfWork;
+    private $class;
+
+    public function __construct(DocumentManager $dm, UnitOfWork $uow, ClassMetadata $class)
+    {
+        $this->dm = $dm;
+        $this->unitOfWork = $uow;
+        $this->class = $class;
+    }
+
+    public function hydrate($document, $data)
+    {
+        $hydratedData = array();
+
+        /** @Field(type="id") */
+        if (isset($data['_id'])) {
+            $value = $data['_id'];
+            $return = (string) $value;
+            $this->class->reflFields['id']->setValue($document, $return);
+            $hydratedData['id'] = $return;
+        }
+
+        /** @Field(type="string") */
+        if (isset($data['title'])) {
+            $value = $data['title'];
+            $return = (string) $value;
+            $this->class->reflFields['title']->setValue($document, $return);
+            $hydratedData['title'] = $return;
+        }
+
+        /** @Field(type="string") */
+        if (isset($data['code'])) {
+            $value = $data['code'];
+            $return = (string) $value;
+            $this->class->reflFields['code']->setValue($document, $return);
+            $hydratedData['code'] = $return;
+        }
+
+        /** @Field(type="string") */
+        if (isset($data['slug'])) {
+            $value = $data['slug'];
+            $return = (string) $value;
+            $this->class->reflFields['slug']->setValue($document, $return);
+            $hydratedData['slug'] = $return;
+        }
+        return $hydratedData;
+    }
+}

+ 77 - 0
tests/Gedmo/Sluggable/SluggableDocumentTest.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace Gedmo\Sluggable;
+
+use Sluggable\Fixture\Document\Article;
+
+/**
+ * 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 SluggableDocumentTest extends \PHPUnit_Framework_TestCase
+{
+    const TEST_ENTITY_CLASS = 'Sluggable\Fixture\Document\Article';
+    
+    /**
+     * @var DocumentManager
+     */
+    private $dm;
+    
+    public function setUp()
+    {
+        $config = new \Doctrine\ODM\MongoDB\Configuration();
+        $config->setProxyDir(__DIR__ . '/Proxy');
+        $config->setProxyNamespace('Gedmo\Sluggable\Proxies');
+        $config->setHydratorDir(__DIR__ . '/Hydrator');
+        $config->setHydratorNamespace('Hydrator');
+        $config->setDefaultDB('doctrine_odm_sluggable_tests');
+        
+        
+        $config->setLoggerCallable(function(array $log) {
+            print_r($log);
+        });
+        
+        
+        $reader = new \Doctrine\Common\Annotations\AnnotationReader();
+        $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
+        $config->setMetadataDriverImpl(
+            new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader, __DIR__ . '/Fixture/Document')
+        );
+        
+        $evm = new \Doctrine\Common\EventManager();
+        $sluggableListener = new ODM\MongoDB\SluggableListener();
+        $evm->addEventSubscriber($sluggableListener);
+        $this->dm = \Doctrine\ODM\MongoDB\DocumentManager::create(
+            new \Doctrine\MongoDB\Connection(),
+            $config, 
+            $evm
+        );
+        
+    }
+    
+    public function testSluggable()
+    {
+        $art0 = new Article();
+        $art0->setTitle('hello');
+        $art0->setCode('code');
+        
+        $this->dm->persist($art0);
+        $this->dm->flush();
+        
+        $this->assertEquals('hello-code', $art0->getSlug());
+        
+        $repo = $this->dm->getRepository(self::TEST_ENTITY_CLASS);
+        $art = $repo->findOneByTitle('hello');
+        $art->setTitle('New Title');
+        
+        $this->dm->persist($art);
+        $this->dm->flush();
+        
+        $art = $repo->findOneByTitle('New Title');
+        $this->assertEquals('new-title-code', $art->getSlug());
+    }
+}

+ 7 - 1
tests/bootstrap.php

@@ -41,7 +41,13 @@ $classLoader->register();
 
 $classLoader = new Doctrine\Common\ClassLoader('Symfony', 'Doctrine');
 $classLoader->register();
-      
+
+$classLoader = new Doctrine\Common\ClassLoader('Doctrine\ODM', 'Doctrine');
+$classLoader->register();
+
+$classLoader = new Doctrine\Common\ClassLoader('Doctrine\MongoDB', 'Doctrine');
+$classLoader->register();
+
 $classLoader = new Doctrine\Common\ClassLoader('Gedmo', __DIR__ . '/../lib');
 $classLoader->register();