PageRenderTime 174ms CodeModel.GetById 80ms app.highlight 15ms RepoModel.GetById 56ms app.codeStats 0ms

/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php

https://github.com/maxbeutel/doctrine2
PHP | 206 lines | 161 code | 40 blank | 5 comment | 9 complexity | 69d3d80987f1ad8acf88a3c758da1bd4 MD5 | raw file
  1<?php
  2
  3namespace Doctrine\Tests\ORM\Tools;
  4
  5use Doctrine\ORM\Tools\SchemaTool,
  6    Doctrine\ORM\Tools\EntityGenerator,
  7    Doctrine\ORM\Tools\Export\ClassMetadataExporter,
  8    Doctrine\ORM\Mapping\ClassMetadataInfo;
  9
 10require_once __DIR__ . '/../../TestInit.php';
 11
 12class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
 13{
 14    private $_generator;
 15    private $_tmpDir;
 16    private $_namespace;
 17
 18    public function setUp()
 19    {
 20        $this->_namespace = uniqid("doctrine_");
 21        $this->_tmpDir = \sys_get_temp_dir();
 22        \mkdir($this->_tmpDir . \DIRECTORY_SEPARATOR . $this->_namespace);
 23        $this->_generator = new EntityGenerator();
 24        $this->_generator->setGenerateAnnotations(true);
 25        $this->_generator->setGenerateStubMethods(true);
 26        $this->_generator->setRegenerateEntityIfExists(false);
 27        $this->_generator->setUpdateEntityIfExists(true);
 28    }
 29
 30    public function tearDown()
 31    {
 32        $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace));
 33        foreach ($ri AS $file) {
 34            /* @var $file \SplFileInfo */
 35            if ($file->isFile()) {
 36                \unlink($file->getPathname());
 37            }
 38        }
 39        rmdir($this->_tmpDir . '/' . $this->_namespace);
 40    }
 41
 42    public function generateBookEntityFixture()
 43    {
 44        $metadata = new ClassMetadataInfo($this->_namespace . '\EntityGeneratorBook');
 45        $metadata->namespace = $this->_namespace;
 46        $metadata->customRepositoryClassName = $this->_namespace  . '\EntityGeneratorBookRepository';
 47        
 48        $metadata->table['name'] = 'book';
 49        $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
 50        $metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
 51        $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
 52        $metadata->mapOneToOne(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', 'mappedBy' => 'book'));
 53        $joinColumns = array(
 54            array('name' => 'author_id', 'referencedColumnName' => 'id')
 55        );
 56        $metadata->mapManyToMany(array(
 57            'fieldName' => 'comments',
 58            'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorComment',
 59            'joinTable' => array(
 60                'name' => 'book_comment',
 61                'joinColumns' => array(array('name' => 'book_id', 'referencedColumnName' => 'id')),
 62                'inverseJoinColumns' => array(array('name' => 'comment_id', 'referencedColumnName' => 'id')),
 63            ),
 64        ));
 65        $metadata->addLifecycleCallback('loading', 'postLoad');
 66        $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
 67        $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
 68
 69        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
 70
 71        return $metadata;
 72    }
 73
 74    /**
 75     * @param  ClassMetadataInfo $metadata
 76     * @return EntityGeneratorBook
 77     */
 78    public function newInstance($metadata)
 79    {
 80        $path = $this->_tmpDir . '/'. $this->_namespace . '/EntityGeneratorBook.php';
 81        $this->assertFileExists($path);
 82        require_once $path;
 83
 84        return new $metadata->name;
 85    }
 86
 87    public function testGeneratedEntityClass()
 88    {
 89        $metadata = $this->generateBookEntityFixture();
 90
 91        $book = $this->newInstance($metadata);
 92
 93        $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
 94        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', '__construct'), "EntityGeneratorBook::__construct() missing.");
 95        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getId'), "EntityGeneratorBook::getId() missing.");
 96        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() missing.");
 97        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getName'), "EntityGeneratorBook::getName() missing.");
 98        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setAuthor'), "EntityGeneratorBook::setAuthor() missing.");
 99        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getAuthor'), "EntityGeneratorBook::getAuthor() missing.");
100        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getComments'), "EntityGeneratorBook::getComments() missing.");
101        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addComments'), "EntityGeneratorBook::addComments() missing.");
102        
103        $this->assertEquals('published', $book->getStatus());
104
105        $book->setName('Jonathan H. Wage');
106        $this->assertEquals('Jonathan H. Wage', $book->getName());
107
108        $author = new EntityGeneratorAuthor();
109        $book->setAuthor($author);
110        $this->assertEquals($author, $book->getAuthor());
111
112        $comment = new EntityGeneratorComment();
113        $book->addComments($comment);
114        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
115        $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
116    }
117
118    public function testEntityUpdatingWorks()
119    {
120        $metadata = $this->generateBookEntityFixture();
121        $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
122        
123        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
124
125        $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/~EntityGeneratorBook.php");
126
127        $book = $this->newInstance($metadata);
128        $reflClass = new \ReflectionClass($metadata->name);
129
130        $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
131        $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
132        $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
133
134        $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
135        $this->assertTrue($reflClass->getProperty('test')->isPrivate(), "Check for private property test failed.");
136        $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
137        $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
138        $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
139        $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
140    }
141
142    public function testEntityExtendsStdClass()
143    {
144        $this->_generator->setClassToExtend('stdClass');
145        $metadata = $this->generateBookEntityFixture();
146
147        $book = $this->newInstance($metadata);
148        $this->assertInstanceOf('stdClass', $book);
149    }
150
151    public function testLifecycleCallbacks()
152    {
153        $metadata = $this->generateBookEntityFixture();
154
155        $book = $this->newInstance($metadata);
156        $reflClass = new \ReflectionClass($metadata->name);
157
158        $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
159        $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
160    }
161
162    public function testLoadMetadata()
163    {
164        $metadata = $this->generateBookEntityFixture();
165
166        $book = $this->newInstance($metadata);
167
168        $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
169        $reader = new \Doctrine\Common\Annotations\AnnotationReader();
170        $reader->setDefaultAnnotationNamespace("Doctrine\\ORM\\Mapping\\");
171        $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
172        $driver->loadMetadataForClass($cm->name, $cm);
173
174        $this->assertEquals($cm->columnNames, $metadata->columnNames);
175        $this->assertEquals($cm->getTableName(), $metadata->getTableName());
176        $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
177        $this->assertEquals($cm->identifier, $metadata->identifier);
178        $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
179        $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
180    }
181
182    public function testLoadPrefixedMetadata()
183    {
184        $this->_generator->setAnnotationPrefix('orm:');
185        $metadata = $this->generateBookEntityFixture();
186
187
188        $book = $this->newInstance($metadata);
189
190        $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
191        $reader = new \Doctrine\Common\Annotations\AnnotationReader();
192        $reader->setAnnotationNamespaceAlias("Doctrine\\ORM\\Mapping\\", "orm");
193        $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
194        $driver->loadMetadataForClass($cm->name, $cm);
195
196        $this->assertEquals($cm->columnNames, $metadata->columnNames);
197        $this->assertEquals($cm->getTableName(), $metadata->getTableName());
198        $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
199        $this->assertEquals($cm->identifier, $metadata->identifier);
200        $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
201        $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
202    }
203}
204
205class EntityGeneratorAuthor {}
206class EntityGeneratorComment {}