Completed
Push — master ( ba3223...b47a39 )
by Luís
17s
created

Command/GenerateRepositoriesCommandTest.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Doctrine\Tests\ORM\Tools\Console\Command;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand;
7
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
8
use Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository;
9
use Symfony\Component\Console\Tester\CommandTester;
10
use Symfony\Component\Console\Helper\HelperSet;
11
use Symfony\Component\Console\Application;
12
use Doctrine\Tests\OrmFunctionalTestCase;
13
14
/**
15
 * GenerateRepositoriesCommandTest
16
 */
17
class GenerateRepositoriesCommandTest extends OrmFunctionalTestCase
18
{
19
    /**
20
     * @var \Symfony\Component\Console\Application
21
     */
22
    private $application;
23
24
    private $path;
25
26
    /**
27
     * @inheritdoc
28
     */
29
    protected function setUp()
30
    {
31
        parent::setUp();
32
33
        $this->path = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('doctrine_');
34
35
        \mkdir($this->path);
36
37
38
        $metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl();
39
40
        $metadataDriver->addPaths(
0 ignored issues
show
The method addPaths() does not exist on Doctrine\Common\Persiste...ng\Driver\MappingDriver. It seems like you code against a sub-type of Doctrine\Common\Persiste...ng\Driver\MappingDriver such as Doctrine\Common\Persiste...Driver\AnnotationDriver or Doctrine\Common\Persiste...\Driver\StaticPHPDriver. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $metadataDriver->/** @scrutinizer ignore-call */ 
41
                         addPaths(
Loading history...
41
            [
42
            __DIR__ . '/../../../../Models/DDC3231/'
43
            ]
44
        );
45
46
        $this->application = new Application();
47
48
        $this->application->setHelperSet(new HelperSet(
49
            [
50
            'em' => new EntityManagerHelper($this->_em)
51
            ]
52
        ));
53
54
        $this->application->add(new GenerateRepositoriesCommand());
55
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 View Code Duplication
    public function tearDown()
62
    {
63
        $dirs = [];
64
65
        $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path));
66
        foreach ($ri AS $file) {
67
            /* @var $file \SplFileInfo */
68
            if ($file->isFile()) {
69
                \unlink($file->getPathname());
70
            } elseif ($file->getBasename() === '.') {
71
                $dirs[] = $file->getRealPath();
72
            }
73
        }
74
75
        arsort($dirs);
76
77
        foreach ($dirs as $dir) {
78
            \rmdir($dir);
79
        }
80
81
        parent::tearDown();
82
    }
83
84 View Code Duplication
    public function testGenerateRepositories()
85
    {
86
        $this->generateRepositories('DDC3231User1');
87
88
        $cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User1Repository';
89
        $fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';
90
91
        $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
92
        $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php');
93
94
        require $this->path . DIRECTORY_SEPARATOR . $fname;
95
        require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php';
96
97
        $this->assertTrue(class_exists($cname));
98
        $this->assertTrue(class_exists('DDC3231User1NoNamespaceRepository'));
99
100
        $repo1  = new \ReflectionClass($cname);
101
        $repo2  = new \ReflectionClass('DDC3231User1NoNamespaceRepository');
102
103
        $this->assertSame(EntityRepository::class, $repo1->getParentClass()->getName());
104
        $this->assertSame(EntityRepository::class, $repo2->getParentClass()->getName());
105
    }
106
107 View Code Duplication
    public function testGenerateRepositoriesCustomDefaultRepository()
108
    {
109
        $this->generateRepositories('DDC3231User2', DDC3231EntityRepository::class);
110
111
        $cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User2Repository';
112
        $fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';
113
114
        $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
115
        $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php');
116
117
        require $this->path . DIRECTORY_SEPARATOR . $fname;
118
        require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php';
119
120
        $this->assertTrue(class_exists($cname));
121
        $this->assertTrue(class_exists('DDC3231User2NoNamespaceRepository'));
122
123
        $repo1  = new \ReflectionClass($cname);
124
        $repo2  = new \ReflectionClass('DDC3231User2NoNamespaceRepository');
125
126
        $this->assertSame(DDC3231EntityRepository::class, $repo1->getParentClass()->getName());
127
        $this->assertSame(DDC3231EntityRepository::class, $repo2->getParentClass()->getName());
128
    }
129
130
    /**
131
     * @param string $filter
132
     * @param string $defaultRepository
133
     */
134
    private function generateRepositories($filter, $defaultRepository = null)
135
    {
136
        if ($defaultRepository) {
137
            $this->_em->getConfiguration()->setDefaultRepositoryClassName($defaultRepository);
138
        }
139
140
        $command    = $this->application->find('orm:generate-repositories');
141
        $tester     = new CommandTester($command);
142
        $tester->execute(
143
            [
144
            'command'   => $command->getName(),
145
            'dest-path' => $this->path,
146
            '--filter'  => $filter,
147
            ]
148
        );
149
    }
150
151
}
152