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