1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Log\NullLogger; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
10
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
11
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
14
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle; |
15
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
16
|
|
|
use function method_exists; |
17
|
|
|
|
18
|
|
|
class ImportMappingDoctrineCommandTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** @var Kernel|null */ |
21
|
|
|
private $kernel; |
22
|
|
|
|
23
|
|
|
/** @var CommandTester|null */ |
24
|
|
|
private $commandTester; |
25
|
|
|
|
26
|
|
|
protected function setup() |
27
|
|
|
{ |
28
|
|
|
$this->kernel = new ImportMappingTestingKernel(); |
29
|
|
|
$this->kernel->boot(); |
30
|
|
|
|
31
|
|
|
$connection = $this->kernel->getContainer() |
32
|
|
|
->get('doctrine') |
33
|
|
|
->getConnection(); |
34
|
|
|
$connection->executeQuery('CREATE TABLE product (id integer primary key, name varchar(20), hint text)'); |
35
|
|
|
|
36
|
|
|
$application = new Application($this->kernel); |
37
|
|
|
$command = $application->find('doctrine:mapping:import'); |
38
|
|
|
$this->commandTester = new CommandTester($command); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function tearDown() |
42
|
|
|
{ |
43
|
|
|
$fs = new Filesystem(); |
44
|
|
|
if ($this->kernel !== null) { |
45
|
|
|
$fs->remove($this->kernel->getCacheDir()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$fs->remove(sys_get_temp_dir() . '/import_mapping_bundle'); |
49
|
|
|
$this->kernel = null; |
50
|
|
|
$this->commandTester = null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testExecuteXmlWithBundle() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->commandTester->execute(['name' => 'ImportMappingTestFooBundle']); |
56
|
|
|
|
57
|
|
|
$expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Resources/config/doctrine/Product.orm.xml'; |
58
|
|
|
$this->assertFileExists($expectedMetadataPath); |
59
|
|
|
$this->assertContains('"Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function testExecuteAnnotationsWithBundle() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->commandTester->execute([ |
65
|
|
|
'name' => 'ImportMappingTestFooBundle', |
66
|
|
|
'mapping-type' => 'annotation', |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
$expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Entity/Product.php'; |
70
|
|
|
$this->assertFileExists($expectedMetadataPath); |
71
|
|
|
$this->assertContains('namespace Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity;', file_get_contents($expectedMetadataPath), 'File contains correct namespace'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @expectedException \InvalidArgumentException |
76
|
|
|
* @expectedExceptionMessageRegExp /The --path option is required/ |
77
|
|
|
*/ |
78
|
|
|
public function testExecuteThrowsExceptionWithNamespaceAndNoPath() |
79
|
|
|
{ |
80
|
|
|
$this->commandTester->execute(['name' => 'Some\Namespace']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testExecuteXmlWithNamespace() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$this->commandTester->execute([ |
86
|
|
|
'name' => 'Some\Namespace\Entity', |
87
|
|
|
'--path' => $this->getProjectDir($this->kernel) . '/config/doctrine', |
|
|
|
|
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
$expectedMetadataPath = $this->getProjectDir($this->kernel) . '/config/doctrine/Product.orm.xml'; |
|
|
|
|
91
|
|
|
$this->assertFileExists($expectedMetadataPath); |
92
|
|
|
$this->assertContains('"Some\Namespace\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testExecuteAnnotationsWithNamespace() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$this->commandTester->execute([ |
98
|
|
|
'name' => 'Some\Namespace\Entity', |
99
|
|
|
'--path' => $this->getProjectDir($this->kernel) . '/src/Entity', |
|
|
|
|
100
|
|
|
'mapping-type' => 'annotation', |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
$expectedMetadataPath = $this->getProjectDir($this->kernel) . '/src/Entity/Product.php'; |
|
|
|
|
104
|
|
|
$this->assertFileExists($expectedMetadataPath); |
105
|
|
|
$this->assertContains('namespace Some\Namespace\Entity;', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* BC layer to support Symfony < 4.2 and Symfony >= 5.0. Once support for Symfony < 4.2 has been removed, this method can be dropped. |
110
|
|
|
*/ |
111
|
|
|
private function getProjectDir(Kernel $kernel) : string |
112
|
|
|
{ |
113
|
|
|
return method_exists($kernel, 'getProjectDir') ? $kernel->getProjectDir() : $kernel->getRootDir(); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
class ImportMappingTestingKernel extends Kernel |
118
|
|
|
{ |
119
|
|
|
public function __construct() |
120
|
|
|
{ |
121
|
|
|
parent::__construct('test', true); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function registerBundles() |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
new FrameworkBundle(), |
128
|
|
|
new DoctrineBundle(), |
129
|
|
|
new ImportMappingTestFooBundle(), |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function registerContainerConfiguration(LoaderInterface $loader) |
134
|
|
|
{ |
135
|
|
|
$loader->load(function (ContainerBuilder $container) { |
136
|
|
|
// @todo Setting the kernel.name parameter can be removed once the dependency on DoctrineCacheBundle has been dropped |
137
|
|
|
$container->setParameter('kernel.name', 'foo'); |
138
|
|
|
$container->loadFromExtension('framework', ['secret' => 'F00']); |
139
|
|
|
|
140
|
|
|
$container->loadFromExtension('doctrine', [ |
141
|
|
|
'dbal' => [ |
142
|
|
|
'driver' => 'pdo_sqlite', |
143
|
|
|
'path' => $this->getCacheDir() . '/testing.db', |
144
|
|
|
], |
145
|
|
|
'orm' => [], |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
|
// Register a NullLogger to avoid getting the stderr default logger of FrameworkBundle |
149
|
|
|
$container->register('logger', NullLogger::class); |
150
|
|
|
}); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getRootDir() |
154
|
|
|
{ |
155
|
|
|
if ($this->rootDir === null) { |
|
|
|
|
156
|
|
|
$this->rootDir = sys_get_temp_dir() . '/sf_kernel_' . md5(mt_rand()); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->rootDir; |
|
|
|
|
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
class ImportMappingTestFooBundle extends Bundle |
164
|
|
|
{ |
165
|
|
|
public function getPath() |
166
|
|
|
{ |
167
|
|
|
return sys_get_temp_dir() . '/import_mapping_bundle'; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.