File: EntityRepositoryGeneratorTest.php

package info (click to toggle)
doctrine 2.8.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,284 kB
  • sloc: php: 96,641; xml: 1,217; makefile: 30; sh: 23
file content (202 lines) | stat: -rw-r--r-- 6,347 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php

namespace Doctrine\Tests\ORM\Tools;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Tools\EntityGenerator;
use Doctrine\ORM\Tools\EntityRepositoryGenerator;
use Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository;
use Doctrine\Tests\Models\DDC3231\DDC3231User1;
use Doctrine\Tests\Models\DDC3231\DDC3231User2;
use Doctrine\Tests\OrmTestCase;
use Doctrine\Tests\VerifyDeprecations;

class EntityRepositoryGeneratorTest extends OrmTestCase
{
    use VerifyDeprecations;

    /**
     * @var EntityGenerator
     */
    private $_generator;

    /**
     * @var EntityRepositoryGenerator
     */
    private $_repositoryGenerator;

    private $_tmpDir;
    private $_namespace;

    /**
     * @inheritdoc
     */
    protected function setUp() : void
    {
        $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);
        $this->_generator->setGenerateStubMethods(true);
        $this->_generator->setRegenerateEntityIfExists(false);
        $this->_generator->setUpdateEntityIfExists(true);
        $this->_generator->setFieldVisibility(EntityGenerator::FIELD_VISIBLE_PROTECTED);

        $this->_repositoryGenerator = new EntityRepositoryGenerator();
    }

    /**
     * @inheritdoc
     */
    public function tearDown() : void
    {
        $dirs = [];

        $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();
            }
        }

        arsort($dirs);

        foreach ($dirs as $dir) {
            \rmdir($dir);
        }
    }

    /** @after */
    public function ensureTestGeneratedDeprecationMessages() : void
    {
        $this->assertHasDeprecationMessages();
    }

    /**
     * @group DDC-3231
     */
    public function testGeneratedEntityRepositoryClass()
    {
        $em = $this->_getTestEntityManager();
        $ns = $this->_namespace;

        $className = $ns . '\DDC3231User1Tmp';
        $this->writeEntityClass(DDC3231User1::class, $className);

        $rpath = $this->writeRepositoryClass($className);

        $this->assertFileExists($rpath);

        require $rpath;

        $repo = new \ReflectionClass($em->getRepository($className));

        $this->assertTrue($repo->inNamespace());
        $this->assertSame($className . 'Repository', $repo->getName());
        $this->assertSame(EntityRepository::class, $repo->getParentClass()->getName());

        require_once __DIR__ . '/../../Models/DDC3231/DDC3231User1NoNamespace.php';

        $className2 = 'DDC3231User1NoNamespaceTmp';
        $this->writeEntityClass(\DDC3231User1NoNamespace::class, $className2);

        $rpath2 = $this->writeRepositoryClass($className2);

        $this->assertFileExists($rpath2);

        require $rpath2;

        $repo2 = new \ReflectionClass($em->getRepository($className2));

        $this->assertFalse($repo2->inNamespace());
        $this->assertSame($className2 . 'Repository', $repo2->getName());
        $this->assertSame(EntityRepository::class, $repo2->getParentClass()->getName());
    }

    /**
     * @group DDC-3231
     */
    public function testGeneratedEntityRepositoryClassCustomDefaultRepository()
    {
        $em = $this->_getTestEntityManager();
        $ns = $this->_namespace;

        $className = $ns . '\DDC3231User2Tmp';
        $this->writeEntityClass(DDC3231User2::class, $className);

        $rpath = $this->writeRepositoryClass($className, DDC3231EntityRepository::class);

        $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(DDC3231EntityRepository::class, $repo->getParentClass()->getName());


        require_once __DIR__ . '/../../Models/DDC3231/DDC3231User2NoNamespace.php';

        $className2 = 'DDC3231User2NoNamespaceTmp';
        $this->writeEntityClass('DDC3231User2NoNamespace', $className2);

        $rpath2 = $this->writeRepositoryClass($className2, DDC3231EntityRepository::class);

        $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(DDC3231EntityRepository::class, $repo2->getParentClass()->getName());
    }

    /**
     * @param string $className
     * @param string $newClassName
     */
    private function writeEntityClass($className, $newClassName)
    {
        $cmf    = new ClassMetadataFactory();
        $em     = $this->_getTestEntityManager();

        $cmf->setEntityManager($em);

        $metadata               = $cmf->getMetadataFor($className);
        $metadata->namespace    = $this->_namespace;
        $metadata->name         = $newClassName;
        $metadata->customRepositoryClassName = $newClassName . "Repository";

        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);

        require $this->_tmpDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $newClassName) . ".php";
    }

    /**
     * @param string $className
     * @param string $defaultRepository
     * @return string
     */
    private function writeRepositoryClass($className, $defaultRepository = null)
    {
        $this->_repositoryGenerator->setDefaultRepositoryName($defaultRepository);

        $this->_repositoryGenerator->writeEntityRepositoryClass($className . 'Repository', $this->_tmpDir);

        return $this->_tmpDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $className) . 'Repository.php';
    }

}