PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/gnat42/doctrine2
PHP | 108 lines | 80 code | 20 blank | 8 comment | 0 complexity | 08036086c04f4eeb021e2b0ed8a01527 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
 16    public function setUp()
 17    {
 18        $this->_generator = new EntityGenerator();
 19        $this->_generator->setGenerateAnnotations(true);
 20        $this->_generator->setGenerateStubMethods(true);
 21        $this->_generator->setRegenerateEntityIfExists(false);
 22        $this->_generator->setUpdateEntityIfExists(true);
 23    }
 24
 25    public function testWriteEntityClass()
 26    {
 27        $metadata = new ClassMetadataInfo('EntityGeneratorBook');
 28        $metadata->table['name'] = 'book';
 29        $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
 30        $metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
 31        $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
 32        $metadata->mapOneToOne(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', 'mappedBy' => 'book'));
 33        $joinColumns = array(
 34            array('name' => 'author_id', 'referencedColumnName' => 'id')
 35        );
 36        $metadata->mapManyToMany(array(
 37            'fieldName' => 'comments',
 38            'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorComment',
 39            'joinTable' => array(
 40                'name' => 'book_comment',
 41                'joinColumns' => array(array('name' => 'book_id', 'referencedColumnName' => 'id')),
 42                'inverseJoinColumns' => array(array('name' => 'comment_id', 'referencedColumnName' => 'id')),
 43            ),
 44        ));
 45        $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
 46
 47        $this->_generator->writeEntityClass($metadata, __DIR__);
 48
 49        $path = __DIR__ . '/EntityGeneratorBook.php';
 50        $this->assertTrue(file_exists($path));
 51        require_once $path;
 52
 53        return $metadata;
 54    }
 55
 56    /**
 57     * @depends testWriteEntityClass
 58     * @param ClassMetadata $metadata
 59     */
 60    public function testGeneratedEntityClass($metadata)
 61    {
 62        $this->assertTrue(method_exists('\EntityGeneratorBook', 'getId'));
 63        $this->assertTrue(method_exists('\EntityGeneratorBook', 'setName'));
 64        $this->assertTrue(method_exists('\EntityGeneratorBook', 'getName'));
 65        $this->assertTrue(method_exists('\EntityGeneratorBook', 'setAuthor'));
 66        $this->assertTrue(method_exists('\EntityGeneratorBook', 'getAuthor'));
 67        $this->assertTrue(method_exists('\EntityGeneratorBook', 'getComments'));
 68        $this->assertTrue(method_exists('\EntityGeneratorBook', 'addComments'));
 69
 70        $book = new \EntityGeneratorBook();
 71        $this->assertEquals('published', $book->getStatus());
 72
 73        $book->setName('Jonathan H. Wage');
 74        $this->assertEquals('Jonathan H. Wage', $book->getName());
 75
 76        $author = new EntityGeneratorAuthor();
 77        $book->setAuthor($author);
 78        $this->assertEquals($author, $book->getAuthor());
 79
 80        $comment = new EntityGeneratorComment();
 81        $book->addComments($comment);
 82        $this->assertEquals(array($comment), $book->getComments());
 83
 84        return $metadata;
 85    }
 86
 87    /**
 88     * @depends testGeneratedEntityClass
 89     * @param ClassMetadata $metadata
 90     */
 91    public function testEntityUpdatingWorks($metadata)
 92    {
 93        $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
 94        $this->_generator->writeEntityClass($metadata, __DIR__);
 95
 96        $code = file_get_contents(__DIR__ . '/EntityGeneratorBook.php');
 97
 98        $this->assertTrue(strstr($code, 'private $test;') !== false);
 99        $this->assertTrue(strstr($code, 'private $test;') !== false);
100        $this->assertTrue(strstr($code, 'public function getTest(') !== false);
101        $this->assertTrue(strstr($code, 'public function setTest(') !== false);
102
103        unlink(__DIR__ . '/EntityGeneratorBook.php');
104    }
105}
106
107class EntityGeneratorAuthor {}
108class EntityGeneratorComment {}