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

GenerateRepositoriesCommandTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Tools\Console\Command;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
6
use Doctrine\ORM\Configuration;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\EntityRepository;
9
use Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand;
10
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
11
use Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository;
12
use Doctrine\Tests\OrmFunctionalTestCase;
13
use Symfony\Component\Console\Application;
14
use Symfony\Component\Console\Helper\HelperSet;
15
use Symfony\Component\Console\Tester\CommandTester;
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
        $metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl();
38
        $metadataDriver->addPaths([__DIR__ . '/../../../../Models/DDC3231/']);
39
40
        $this->application = new Application();
41
        $this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)]));
42
        $this->application->add(new GenerateRepositoriesCommand());
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 View Code Duplication
    public function tearDown()
49
    {
50
        $dirs = [];
51
52
        $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path));
53
        foreach ($ri AS $file) {
54
            /* @var $file \SplFileInfo */
55
            if ($file->isFile()) {
56
                \unlink($file->getPathname());
57
            } elseif ($file->getBasename() === '.') {
58
                $dirs[] = $file->getRealPath();
59
            }
60
        }
61
62
        arsort($dirs);
63
64
        foreach ($dirs as $dir) {
65
            \rmdir($dir);
66
        }
67
68
        parent::tearDown();
69
    }
70
71 View Code Duplication
    public function testGenerateRepositories()
72
    {
73
        $this->generateRepositories('DDC3231User1');
74
75
        $cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User1Repository';
76
        $fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';
77
78
        self::assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
79
        self::assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php');
80
81
        require $this->path . DIRECTORY_SEPARATOR . $fname;
82
        require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php';
83
84
        self::assertTrue(class_exists($cname));
85
        self::assertTrue(class_exists('DDC3231User1NoNamespaceRepository'));
86
87
        $repo1  = new \ReflectionClass($cname);
88
        $repo2  = new \ReflectionClass('DDC3231User1NoNamespaceRepository');
89
90
        self::assertSame(EntityRepository::class, $repo1->getParentClass()->getName());
91
        self::assertSame(EntityRepository::class, $repo2->getParentClass()->getName());
92
    }
93
94 View Code Duplication
    public function testGenerateRepositoriesCustomDefaultRepository()
95
    {
96
        $this->generateRepositories('DDC3231User2', DDC3231EntityRepository::class);
97
98
        $cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User2Repository';
99
        $fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';
100
101
        self::assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
102
        self::assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php');
103
104
        require $this->path . DIRECTORY_SEPARATOR . $fname;
105
        require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php';
106
107
        self::assertTrue(class_exists($cname));
108
        self::assertTrue(class_exists('DDC3231User2NoNamespaceRepository'));
109
110
        $repo1  = new \ReflectionClass($cname);
111
        $repo2  = new \ReflectionClass('DDC3231User2NoNamespaceRepository');
112
113
        self::assertSame(DDC3231EntityRepository::class, $repo1->getParentClass()->getName());
114
        self::assertSame(DDC3231EntityRepository::class, $repo2->getParentClass()->getName());
115
    }
116
117
    /**
118
     * @param string $filter
119
     * @param string $defaultRepository
120
     */
121
    private function generateRepositories($filter, $defaultRepository = null)
122
    {
123
        if ($defaultRepository) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $defaultRepository of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
124
            $this->_em->getConfiguration()->setDefaultRepositoryClassName($defaultRepository);
125
        }
126
127
        $command = $this->application->find('orm:generate-repositories');
128
        $tester  = new CommandTester($command);
129
130
        $tester->execute(
131
            [
132
                'command'   => $command->getName(),
133
                'dest-path' => $this->path,
134
                '--filter'  => $filter,
135
            ]
136
        );
137
    }
138
139
    public function testNoMetadataClassesToProcess() : void
140
    {
141
        $configuration   = $this->createMock(Configuration::class);
142
        $metadataFactory = $this->createMock(ClassMetadataFactory::class);
143
        $em              = $this->createMock(EntityManagerInterface::class);
144
145
        $configuration->method('getDefaultRepositoryClassName')
146
                      ->willReturn('fooRepository');
147
148
        $metadataFactory->method('getAllMetadata')
149
                        ->willReturn([]);
150
151
        $em->method('getMetadataFactory')
152
           ->willReturn($metadataFactory);
153
154
        $em->method('getConfiguration')
155
           ->willReturn($configuration);
156
157
        $application = new Application();
158
        $application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)]));
159
        $application->add(new GenerateRepositoriesCommand());
160
161
        $command = $application->find('orm:generate-repositories');
162
        $tester  = new CommandTester($command);
163
164
        $tester->execute(
165
            [
166
                'command'   => $command->getName(),
167
                'dest-path' => $this->path,
168
            ]
169
        );
170
171
        self::assertContains('[OK] No Metadata Classes to process.', $tester->getDisplay());
172
    }
173
}
174