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