PageRenderTime 31ms CodeModel.GetById 17ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Doctrine/ODM/MongoDB/Tests/Tools/DocumentGeneratorTest.php

https://github.com/CrAzE124/mongodb-odm
PHP | 197 lines | 155 code | 37 blank | 5 comment | 9 complexity | ad664ac729e3396a9ae3864385b37233 MD5 | raw file
  1<?php
  2
  3namespace Doctrine\ODM\MongoDB\Tests\Tools;
  4
  5use Doctrine\ODM\MongoDB\Tools\DocumentGenerator;
  6use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
  7
  8class DocumentGeneratorTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
  9{
 10    private $generator;
 11    private $tmpDir;
 12    private $namespace;
 13
 14    public function setUp()
 15    {
 16        $this->namespace = uniqid("doctrine_");
 17        $this->tmpDir = \sys_get_temp_dir();
 18        \mkdir($this->tmpDir . \DIRECTORY_SEPARATOR . $this->namespace);
 19        $this->generator = new DocumentGenerator();
 20        $this->generator->setGenerateAnnotations(true);
 21        $this->generator->setGenerateStubMethods(true);
 22        $this->generator->setRegenerateDocumentIfExists(false);
 23        $this->generator->setUpdateDocumentIfExists(true);
 24    }
 25
 26    public function tearDown()
 27    {
 28        $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->tmpDir . '/' . $this->namespace));
 29        foreach ($ri AS $file) {
 30            /* @var $file \SplFileInfo */
 31            if ($file->isFile()) {
 32                \unlink($file->getPathname());
 33            }
 34        }
 35        rmdir($this->tmpDir . '/' . $this->namespace);
 36    }
 37
 38    public function generateBookDocumentFixture()
 39    {
 40        $metadata = new ClassMetadataInfo($this->namespace . '\DocumentGeneratorBook');
 41        $metadata->namespace = $this->namespace;
 42        $metadata->customRepositoryClassName = $this->namespace  . '\DocumentGeneratorBookRepository';
 43
 44        $metadata->collection = 'book';
 45        $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
 46        $metadata->mapField(array('fieldName' => 'status', 'type' => 'string'));
 47        $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
 48        $metadata->mapOneReference(array('fieldName' => 'author', 'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorAuthor'));
 49        $metadata->mapManyReference(array(
 50            'fieldName' => 'comments',
 51            'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorComment'
 52        ));
 53        $metadata->mapManyReference(array(
 54                'fieldName' => 'searches',
 55                'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorSearch'
 56        ));
 57        $metadata->addLifecycleCallback('loading', 'postLoad');
 58        $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
 59        $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_CUSTOM);
 60
 61        $this->generator->writeDocumentClass($metadata, $this->tmpDir);
 62
 63        return $metadata;
 64    }
 65
 66    /**
 67     * @param  ClassMetadataInfo $metadata
 68     * @return DocumentGeneratorBook
 69     */
 70    public function newInstance($metadata)
 71    {
 72        $path = $this->tmpDir . '/'. $this->namespace . '/DocumentGeneratorBook.php';
 73        $this->assertFileExists($path);
 74        require_once $path;
 75
 76        return new $metadata->name;
 77    }
 78
 79    public function testGeneratedDocumentClass()
 80    {
 81        $metadata = $this->generateBookDocumentFixture();
 82
 83        $book = $this->newInstance($metadata);
 84
 85        $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
 86        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', '__construct'), "DocumentGeneratorBook::__construct() missing.");
 87        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getId'), "DocumentGeneratorBook::getId() missing.");
 88        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'setName'), "DocumentGeneratorBook::setName() missing.");
 89        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getName'), "DocumentGeneratorBook::getName() missing.");
 90        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'setAuthor'), "DocumentGeneratorBook::setAuthor() missing.");
 91        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getAuthor'), "DocumentGeneratorBook::getAuthor() missing.");
 92        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getComments'), "DocumentGeneratorBook::getComments() missing.");
 93        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addComment'), "DocumentGeneratorBook::addComment() missing.");
 94        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeComment'), "DocumentGeneratorBook::removeComment() missing.");
 95        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getSearches'), "DocumentGeneratorBook::getSearches() missing.");
 96        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addSearch'), "DocumentGeneratorBook::addSearch() missing.");
 97        $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeSearch'), "DocumentGeneratorBook::removeSearch() missing.");
 98
 99        $book->setName('Jonathan H. Wage');
100        $this->assertEquals('Jonathan H. Wage', $book->getName());
101
102        $author = new DocumentGeneratorAuthor();
103        $book->setAuthor($author);
104        $this->assertEquals($author, $book->getAuthor());
105
106        $comment = new DocumentGeneratorComment();
107        $book->addComment($comment);
108        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
109        $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
110        $book->removeComment($comment);
111        $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array()), $book->getComments());
112    }
113
114    public function testDocumentUpdatingWorks()
115    {
116        $metadata = $this->generateBookDocumentFixture();
117        $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
118
119        $this->generator->writeDocumentClass($metadata, $this->tmpDir);
120
121        $this->assertFileExists($this->tmpDir . "/" . $this->namespace . "/DocumentGeneratorBook.php");
122
123        $book = $this->newInstance($metadata);
124        $reflClass = new \ReflectionClass($metadata->name);
125
126        $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
127        $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
128        $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
129
130        $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
131        $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed.");
132        $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
133        $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
134        $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
135        $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
136    }
137
138    public function testDocumentExtendsStdClass()
139    {
140        $this->generator->setClassToExtend('stdClass');
141        $metadata = $this->generateBookDocumentFixture();
142
143        $book = $this->newInstance($metadata);
144        $this->assertInstanceOf('stdClass', $book);
145    }
146
147    public function testLifecycleCallbacks()
148    {
149        $metadata = $this->generateBookDocumentFixture();
150
151        $book = $this->newInstance($metadata);
152        $reflClass = new \ReflectionClass($metadata->name);
153
154        $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
155        $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
156    }
157
158    public function testLoadMetadata()
159    {
160        $metadata = $this->generateBookDocumentFixture();
161
162        $book = $this->newInstance($metadata);
163
164        $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
165        $reader = new \Doctrine\Common\Annotations\AnnotationReader();
166        $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
167        $driver->loadMetadataForClass($cm->name, $cm);
168
169        $this->assertEquals($cm->getCollection(), $metadata->getCollection());
170        $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
171        $this->assertEquals($cm->identifier, $metadata->identifier);
172        $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
173        $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
174    }
175
176    public function testLoadPrefixedMetadata()
177    {
178        $metadata = $this->generateBookDocumentFixture();
179
180        $book = $this->newInstance($metadata);
181
182        $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
183        $reader = new \Doctrine\Common\Annotations\AnnotationReader();
184        $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
185        $driver->loadMetadataForClass($cm->name, $cm);
186
187        $this->assertEquals($cm->getCollection(), $metadata->getCollection());
188        $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
189        $this->assertEquals($cm->identifier, $metadata->identifier);
190        $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
191        $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
192    }
193}
194
195class DocumentGeneratorAuthor {}
196class DocumentGeneratorComment {}
197class DocumentGeneratorSearch {}