1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Unit Tests && simple fixes

This commit is contained in:
encoder64 2014-09-07 18:26:09 +03:00
parent cd547fecea
commit 409f6b4bc1
5 changed files with 269 additions and 21 deletions

View File

@ -3,10 +3,10 @@
namespace Doctrine\Tests\Models\DDC3231;
/**
* @Entity(repositoryClass="DDC3231UserRepository")
* @Entity(repositoryClass="DDC3231User1Repository")
* @Table(name="users")
*/
class DDC3231User
class DDC3231User1
{
/**
* @Id

View File

@ -0,0 +1,21 @@
<?php
/**
* @Entity(repositoryClass="DDC3231User1NoNamespaceRepository")
* @Table(name="no_namespace_users")
*/
class DDC3231User1NoNamespace
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="string", length=255)
*/
protected $name;
}

View File

@ -0,0 +1,21 @@
<?php
/**
* @Entity(repositoryClass="DDC3231User2NoNamespaceRepository")
* @Table(name="no_namespace_users2")
*/
class DDC3231User2NoNamespace
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="string", length=255)
*/
protected $name;
}

View File

@ -0,0 +1,143 @@
<?php
namespace Doctrine\Tests\ORM\Tools\Console\Command;
use Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Application;
use Doctrine\Tests\OrmFunctionalTestCase;
/**
* GenerateRepositoriesCommandTest
*/
class GenerateRepositoriesCommandTest extends OrmFunctionalTestCase
{
/**
* @var \Symfony\Component\Console\Application
*/
private $application;
private $path;
/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->path = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('doctrine_');
\mkdir($this->path);
$metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl();
$metadataDriver->addPaths(array(
__DIR__ . '/../../../../Models/DDC3231/'
));
$this->application = new Application();
$this->application->setHelperSet(new HelperSet(array(
'em' => new EntityManagerHelper($this->_em)
)));
$this->application->add(new GenerateRepositoriesCommand());
}
/**
* @inheritdoc
*/
public function tearDown()
{
$dirs = array();
$ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path));
foreach ($ri AS $file) {
/* @var $file \SplFileInfo */
if ($file->isFile()) {
\unlink($file->getPathname());
} elseif ($file->getBasename() === '.') {
$dirs[] = $file->getRealpath();
}
}
arsort($dirs);
foreach ($dirs as $dir) {
\rmdir($dir);
}
parent::tearDown();
}
public function testGenerateRepositories()
{
$this->generateRepositories('DDC3231User1');
$cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User1Repository';
$fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';
$this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
$this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php');
require $this->path . DIRECTORY_SEPARATOR . $fname;
require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php';
$this->assertTrue(class_exists($cname));
$this->assertTrue(class_exists('DDC3231User1NoNamespaceRepository'));
$repo1 = new \ReflectionClass($cname);
$repo2 = new \ReflectionClass('DDC3231User1NoNamespaceRepository');
$this->assertSame('Doctrine\ORM\EntityRepository', $repo1->getParentClass()->getName());
$this->assertSame('Doctrine\ORM\EntityRepository', $repo2->getParentClass()->getName());
}
public function testGenerateRepositoriesCustomDefaultRepository()
{
$this->generateRepositories('DDC3231User2', 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository');
$cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User2Repository';
$fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';
$this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
$this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php');
require $this->path . DIRECTORY_SEPARATOR . $fname;
require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php';
$this->assertTrue(class_exists($cname));
$this->assertTrue(class_exists('DDC3231User2NoNamespaceRepository'));
$repo1 = new \ReflectionClass($cname);
$repo2 = new \ReflectionClass('DDC3231User2NoNamespaceRepository');
$this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo1->getParentClass()->getName());
$this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo2->getParentClass()->getName());
}
/**
* @param string $filter
* @param string $defaultRepository
*/
private function generateRepositories($filter, $defaultRepository = null)
{
if ($defaultRepository) {
$this->_em->getConfiguration()->setDefaultRepositoryClassName($defaultRepository);
}
$command = $this->application->find('orm:generate-repositories');
$tester = new CommandTester($command);
$tester->execute(array(
'command' => $command->getName(),
'dest-path' => $this->path,
'--filter' => $filter,
));
}
}

View File

@ -26,10 +26,10 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase
*/
public function setUp()
{
$this->_namespace = uniqid("doctrine_");
$this->_tmpDir = \sys_get_temp_dir();
\mkdir($this->_tmpDir . \DIRECTORY_SEPARATOR . $this->_namespace);
$this->_namespace = uniqid('doctrine_');
$this->_tmpDir = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . $this->_namespace;
\mkdir($this->_tmpDir);
$this->_generator = new EntityGenerator();
$this->_generator->setAnnotationPrefix("");
$this->_generator->setGenerateAnnotations(true);
@ -46,14 +46,23 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase
*/
public function tearDown()
{
$ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace));
$dirs = array();
$ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir));
foreach ($ri AS $file) {
/* @var $file \SplFileInfo */
if ($file->isFile()) {
\unlink($file->getPathname());
} elseif ($file->getBasename() === '.') {
$dirs[] = $file->getRealpath();
}
}
rmdir($this->_tmpDir . '/' . $this->_namespace);
arsort($dirs);
foreach ($dirs as $dir) {
\rmdir($dir);
}
}
/**
@ -64,38 +73,92 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase
$em = $this->_getTestEntityManager();
$ns = $this->_namespace;
$className = 'DDC3231User';
$this->writeEntityClass('Doctrine\Tests\Models\DDC3231\\' . $className, $ns . '\\' . $className);
$rpath = $this->writeRepositoryClass($ns . '\\' . $className, 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository');
require_once __DIR__.'/../../Models/DDC3231/DDC3231User1.php';
$className = $ns . '\DDC3231User1Tmp';
$this->writeEntityClass('Doctrine\Tests\Models\DDC3231\DDC3231User1', $className);
$rpath = $this->writeRepositoryClass($className);
$this->assertNotNull($rpath);
$this->assertFileExists($rpath);
require $rpath;
$repo = new \ReflectionClass($em->getRepository($ns . '\\' . $className));
$this->assertSame($ns . '\\' . $className . 'Repository', $repo->getName());
$this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo->getParentClass()->getName());
$repo = new \ReflectionClass($em->getRepository($className));
$this->assertTrue($repo->inNamespace());
$this->assertSame($className . 'Repository', $repo->getName());
$this->assertSame('Doctrine\ORM\EntityRepository', $repo->getParentClass()->getName());
$className2 = 'DDC3231User2';
$this->writeEntityClass('Doctrine\Tests\Models\DDC3231\\' . $className2, $ns . '\\' . $className2);
require_once __DIR__.'/../../Models/DDC3231/DDC3231User1NoNamespace.php';
$rpath2 = $this->writeRepositoryClass($ns . '\\' . $className2);
$className2 = 'DDC3231User1NoNamespaceTmp';
$this->writeEntityClass('DDC3231User1NoNamespace', $className2);
$rpath2 = $this->writeRepositoryClass($className2);
$this->assertNotNull($rpath2);
$this->assertFileExists($rpath2);
require $rpath2;
$repo2 = new \ReflectionClass($em->getRepository($ns . '\\' . $className2));
$repo2 = new \ReflectionClass($em->getRepository($className2));
$this->assertSame($ns . '\\' . $className2 . 'Repository', $repo2->getName());
$this->assertFalse($repo2->inNamespace());
$this->assertSame($className2 . 'Repository', $repo2->getName());
$this->assertSame('Doctrine\ORM\EntityRepository', $repo2->getParentClass()->getName());
}
/**
* @group DDC-3231
*/
public function testGeneratedEntityRepositoryClassCustomDefaultRepository()
{
$em = $this->_getTestEntityManager();
$ns = $this->_namespace;
require_once __DIR__.'/../../Models/DDC3231/DDC3231User2.php';
$className = $ns . '\DDC3231User2Tmp';
$this->writeEntityClass('Doctrine\Tests\Models\DDC3231\DDC3231User2', $className);
$rpath = $this->writeRepositoryClass($className, 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository');
$this->assertNotNull($rpath);
$this->assertFileExists($rpath);
require $rpath;
$repo = new \ReflectionClass($em->getRepository($className));
$this->assertTrue($repo->inNamespace());
$this->assertSame($className . 'Repository', $repo->getName());
$this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo->getParentClass()->getName());
require_once __DIR__.'/../../Models/DDC3231/DDC3231User2NoNamespace.php';
$className2 = 'DDC3231User2NoNamespaceTmp';
$this->writeEntityClass('DDC3231User2NoNamespace', $className2);
$rpath2 = $this->writeRepositoryClass($className2, 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository');
$this->assertNotNull($rpath2);
$this->assertFileExists($rpath2);
require $rpath2;
$repo2 = new \ReflectionClass($em->getRepository($className2));
$this->assertFalse($repo2->inNamespace());
$this->assertSame($className2 . 'Repository', $repo2->getName());
$this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo2->getParentClass()->getName());
}
/**
* @param string $className
* @param string $newClassName
@ -114,7 +177,7 @@ class EntityRepositoryGeneratorTest extends \Doctrine\Tests\OrmTestCase
$this->_generator->writeEntityClass($metadata, $this->_tmpDir);
require $this->_tmpDir . '/' . str_replace('\\', '/', $newClassName) . ".php";
require $this->_tmpDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $newClassName) . ".php";
}
/**