/
ImportMappingDoctrineCommandTest.php
169 lines (141 loc) · 5.57 KB
/
ImportMappingDoctrineCommandTest.php
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
<?php
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
/**
* @group legacy
*/
class ImportMappingDoctrineCommandTest extends TestCase
{
/** @var ImportMappingTestingKernel|null */
private $kernel;
/** @var CommandTester|null */
private $commandTester;
protected function setup()
{
$this->kernel = new ImportMappingTestingKernel();
$this->kernel->boot();
$connection = $this->kernel->getContainer()
->get('doctrine')
->getConnection();
$connection->executeQuery('CREATE TABLE product (id integer primary key, name varchar(20), hint text)');
$application = new Application($this->kernel);
$command = $application->find('doctrine:mapping:import');
$this->commandTester = new CommandTester($command);
}
protected function tearDown()
{
$fs = new Filesystem();
if ($this->kernel !== null) {
$fs->remove($this->kernel->getCacheDir());
}
$fs->remove(sys_get_temp_dir() . '/import_mapping_bundle');
$this->kernel = null;
$this->commandTester = null;
}
public function testExecuteXmlWithBundle()
{
$this->commandTester->execute(['name' => 'ImportMappingTestFooBundle']);
$expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Resources/config/doctrine/Product.orm.xml';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('"Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
}
public function testExecuteAnnotationsWithBundle()
{
$this->commandTester->execute([
'name' => 'ImportMappingTestFooBundle',
'mapping-type' => 'annotation',
]);
$expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Entity/Product.php';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('namespace Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity;', file_get_contents($expectedMetadataPath), 'File contains correct namespace');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The --path option is required/
*/
public function testExecuteThrowsExceptionWithNamespaceAndNoPath()
{
$this->commandTester->execute(['name' => 'Some\Namespace']);
}
public function testExecuteXmlWithNamespace()
{
$this->commandTester->execute([
'name' => 'Some\Namespace\Entity',
'--path' => $this->kernel->getProjectDir() . '/config/doctrine',
]);
$expectedMetadataPath = $this->kernel->getProjectDir() . '/config/doctrine/Product.orm.xml';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('"Some\Namespace\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
}
public function testExecuteAnnotationsWithNamespace()
{
$this->commandTester->execute([
'name' => 'Some\Namespace\Entity',
'--path' => $this->kernel->getProjectDir() . '/src/Entity',
'mapping-type' => 'annotation',
]);
$expectedMetadataPath = $this->kernel->getProjectDir() . '/src/Entity/Product.php';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('namespace Some\Namespace\Entity;', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
}
}
class ImportMappingTestingKernel extends Kernel
{
/** @var string|null */
private $projectDir;
public function __construct()
{
parent::__construct('test', true);
}
public function registerBundles() : iterable
{
return [
new FrameworkBundle(),
new DoctrineBundle(),
new ImportMappingTestFooBundle(),
];
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', ['secret' => 'F00']);
$container->loadFromExtension('doctrine', [
'dbal' => [
'driver' => 'pdo_sqlite',
'path' => $this->getCacheDir() . '/testing.db',
],
'orm' => [],
]);
// Register a NullLogger to avoid getting the stderr default logger of FrameworkBundle
$container->register('logger', NullLogger::class);
});
}
public function getProjectDir() : string
{
if ($this->projectDir === null) {
$this->projectDir = sys_get_temp_dir() . '/sf_kernel_' . md5(mt_rand());
}
return $this->projectDir;
}
public function getRootDir() : string
{
return $this->getProjectDir();
}
}
class ImportMappingTestFooBundle extends Bundle
{
public function getPath() : string
{
return sys_get_temp_dir() . '/import_mapping_bundle';
}
}