Skip to content

Commit a425953

Browse files
author
Sergey Rabochiy
committed
Add functional tests for custom root namespace cases
1 parent d4139d4 commit a425953

File tree

11 files changed

+480
-3
lines changed

11 files changed

+480
-3
lines changed

src/Test/MakerTestDetails.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ final class MakerTestDetails
3838

3939
private $commandAllowedToFail = false;
4040

41+
private $snapshotSuffix = '';
42+
4143
private $requiredPhpVersion;
4244

4345
/**
@@ -64,6 +66,46 @@ public function setFixtureFilesPath(string $fixtureFilesPath): self
6466
return $this;
6567
}
6668

69+
public function changeRootNamespace(string $rootNamespace): self
70+
{
71+
$rootNamespace = trim($rootNamespace, '\\');
72+
73+
// to bypass read before flush issue
74+
$this->snapshotSuffix = $rootNamespace;
75+
76+
return $this
77+
->addReplacement(
78+
'composer.json',
79+
'"App\\\\": "src/"',
80+
'"'.$rootNamespace.'\\\\": "src/"'
81+
)
82+
->addReplacement(
83+
'src/Kernel.php',
84+
'namespace App',
85+
'namespace '.$rootNamespace
86+
)
87+
->addReplacement(
88+
'bin/console',
89+
'use App\\Kernel',
90+
'use '.$rootNamespace.'\\Kernel'
91+
)
92+
->addReplacement(
93+
'public/index.php',
94+
'use App\\Kernel',
95+
'use '.$rootNamespace.'\\Kernel'
96+
)
97+
->addReplacement(
98+
'config/services.yaml',
99+
'App\\',
100+
$rootNamespace.'\\'
101+
)
102+
->addReplacement(
103+
'phpunit.xml.dist',
104+
'<env name="KERNEL_CLASS" value="App\\Kernel" />',
105+
'<env name="KERNEL_CLASS" value="'.$rootNamespace.'\\Kernel" />'
106+
);
107+
}
108+
67109
public function addPreMakeCommand(string $preMakeCommand): self
68110
{
69111
$this->preMakeCommands[] = $preMakeCommand;
@@ -202,7 +244,7 @@ public function getUniqueCacheDirectoryName(): string
202244
{
203245
// for cache purposes, only the dependencies are important
204246
// shortened to avoid long paths on Windows
205-
return 'maker_'.substr(md5(serialize($this->getDependencies())), 0, 10);
247+
return 'maker_'.substr(md5(serialize($this->getDependencies()).$this->snapshotSuffix), 0, 10);
206248
}
207249

208250
public function getPreMakeCommands(): array

src/Test/MakerTestEnvironment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ private function preMake()
135135

136136
public function runMaker()
137137
{
138+
$this->preMake();
139+
138140
MakerTestProcess::create('php bin/console cache:clear --no-ansi', $this->path)
139141
->run();
140142

141-
$this->preMake();
142-
143143
// We don't need ansi coloring in tests!
144144
$testProcess = MakerTestProcess::create(
145145
sprintf('php bin/console %s %s --no-ansi', ($this->testDetails->getMaker())::getCommandName(), $this->testDetails->getArgumentsString()),

tests/Maker/FunctionalTest.php

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ public function getCommandTests()
7474
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCommand')
7575
];
7676

77+
yield 'command_in_custom_root_namespace' => [MakerTestDetails::createTest(
78+
$this->getMakerInstance(MakeCommand::class),
79+
[
80+
// command name
81+
'app:foo',
82+
])
83+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCommand')
84+
->changeRootNamespace('Custom')
85+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
86+
->addPreMakeCommand('composer dump-autoload')
87+
];
88+
7789
yield 'controller_basic' => [MakerTestDetails::createTest(
7890
$this->getMakerInstance(MakeController::class),
7991
[
@@ -87,6 +99,22 @@ public function getCommandTests()
8799
})
88100
];
89101

102+
yield 'controller_basic_in_custom_root_namespace' => [MakerTestDetails::createTest(
103+
$this->getMakerInstance(MakeController::class),
104+
[
105+
// controller class name
106+
'FooBar',
107+
])
108+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeController')
109+
->changeRootNamespace('Custom')
110+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
111+
->addPreMakeCommand('composer dump-autoload')
112+
->assert(function(string $output, string $directory) {
113+
// make sure the template was not configured
114+
$this->assertContainsCount('created: ', $output, 1);
115+
})
116+
];
117+
90118
yield 'controller_with_template_and_base' => [MakerTestDetails::createTest(
91119
$this->getMakerInstance(MakeController::class),
92120
[
@@ -156,6 +184,19 @@ public function getCommandTests()
156184
})
157185
];
158186

187+
yield 'fixtures_in_custom_root_namespace' => [MakerTestDetails::createTest(
188+
$this->getMakerInstance(MakeFixtures::class),
189+
[
190+
'AppFixtures'
191+
])
192+
->changeRootNamespace('Custom')
193+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
194+
->addPreMakeCommand('composer dump-autoload')
195+
->assert(function(string $output, string $directory) {
196+
$this->assertContains('created: src/DataFixtures/AppFixtures.php', $output);
197+
})
198+
];
199+
159200
yield 'form_basic' => [MakerTestDetails::createTest(
160201
$this->getMakerInstance(MakeForm::class),
161202
[
@@ -166,6 +207,19 @@ public function getCommandTests()
166207
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeForm')
167208
];
168209

210+
yield 'form_basic_in_custom_root_namespace' => [MakerTestDetails::createTest(
211+
$this->getMakerInstance(MakeForm::class),
212+
[
213+
// form name
214+
'FooBar',
215+
'',
216+
])
217+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFormInCustomRootNamespace')
218+
->changeRootNamespace('Custom')
219+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
220+
->addPreMakeCommand('composer dump-autoload')
221+
];
222+
169223
yield 'form_with_entity' => [MakerTestDetails::createTest(
170224
$this->getMakerInstance(MakeForm::class),
171225
[
@@ -208,6 +262,18 @@ public function getCommandTests()
208262
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFunctional')
209263
];
210264

265+
yield 'functional_in_custom_root_namespace' => [MakerTestDetails::createTest(
266+
$this->getMakerInstance(MakeFunctionalTest::class),
267+
[
268+
// functional test class name
269+
'FooBar',
270+
])
271+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFunctionalInCustomRootNamespace')
272+
->changeRootNamespace('Custom')
273+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
274+
->addPreMakeCommand('composer dump-autoload')
275+
];
276+
211277
yield 'subscriber' => [MakerTestDetails::createTest(
212278
$this->getMakerInstance(MakeSubscriber::class),
213279
[
@@ -218,6 +284,19 @@ public function getCommandTests()
218284
])
219285
];
220286

287+
yield 'subscriber_in_custom_root_namespace' => [MakerTestDetails::createTest(
288+
$this->getMakerInstance(MakeSubscriber::class),
289+
[
290+
// subscriber name
291+
'FooBar',
292+
// event name
293+
'kernel.request',
294+
])
295+
->changeRootNamespace('Custom')
296+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
297+
->addPreMakeCommand('composer dump-autoload')
298+
];
299+
221300
yield 'subscriber_unknown_event_class' => [MakerTestDetails::createTest(
222301
$this->getMakerInstance(MakeSubscriber::class),
223302
[
@@ -238,6 +317,19 @@ public function getCommandTests()
238317
])
239318
];
240319

320+
yield 'serializer_encoder_in_custom_root_namespace' => [MakerTestDetails::createTest(
321+
$this->getMakerInstance(MakeSerializerEncoder::class),
322+
[
323+
// encoder class name
324+
'FooBarEncoder',
325+
// encoder format
326+
'foobar',
327+
])
328+
->changeRootNamespace('Custom')
329+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
330+
->addPreMakeCommand('composer dump-autoload')
331+
];
332+
241333
yield 'twig_extension' => [MakerTestDetails::createTest(
242334
$this->getMakerInstance(MakeTwigExtension::class),
243335
[
@@ -246,6 +338,17 @@ public function getCommandTests()
246338
])
247339
];
248340

341+
yield 'twig_extension_in_custom_root_namespace' => [MakerTestDetails::createTest(
342+
$this->getMakerInstance(MakeTwigExtension::class),
343+
[
344+
// extension class name
345+
'FooBar',
346+
])
347+
->changeRootNamespace('Custom')
348+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
349+
->addPreMakeCommand('composer dump-autoload')
350+
];
351+
249352
yield 'unit_test' => [MakerTestDetails::createTest(
250353
$this->getMakerInstance(MakeUnitTest::class),
251354
[
@@ -254,6 +357,17 @@ public function getCommandTests()
254357
])
255358
];
256359

360+
yield 'unit_test_in_custom_root_namespace' => [MakerTestDetails::createTest(
361+
$this->getMakerInstance(MakeUnitTest::class),
362+
[
363+
// class name
364+
'FooBar',
365+
])
366+
->changeRootNamespace('Custom')
367+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
368+
->addPreMakeCommand('composer dump-autoload')
369+
];
370+
257371
yield 'validator' => [MakerTestDetails::createTest(
258372
$this->getMakerInstance(MakeValidator::class),
259373
[
@@ -262,6 +376,17 @@ public function getCommandTests()
262376
])
263377
];
264378

379+
yield 'validator_in_custom_root_namespace' => [MakerTestDetails::createTest(
380+
$this->getMakerInstance(MakeValidator::class),
381+
[
382+
// validator name
383+
'FooBar',
384+
])
385+
->changeRootNamespace('Custom')
386+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
387+
->addPreMakeCommand('composer dump-autoload')
388+
];
389+
265390
yield 'voter' => [MakerTestDetails::createTest(
266391
$this->getMakerInstance(MakeVoter::class),
267392
[
@@ -270,6 +395,17 @@ public function getCommandTests()
270395
])
271396
];
272397

398+
yield 'voter_in_custom_root_namespace' => [MakerTestDetails::createTest(
399+
$this->getMakerInstance(MakeVoter::class),
400+
[
401+
// voter class name
402+
'FooBar',
403+
])
404+
->changeRootNamespace('Custom')
405+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
406+
->addPreMakeCommand('composer dump-autoload')
407+
];
408+
273409
yield 'auth_empty' => [MakerTestDetails::createTest(
274410
$this->getMakerInstance(MakeAuthenticator::class),
275411
[
@@ -278,6 +414,17 @@ public function getCommandTests()
278414
])
279415
];
280416

417+
yield 'auth_empty_in_custom_root_namespace' => [MakerTestDetails::createTest(
418+
$this->getMakerInstance(MakeAuthenticator::class),
419+
[
420+
// class name
421+
'AppCustomAuthenticator',
422+
])
423+
->changeRootNamespace('Custom')
424+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
425+
->addPreMakeCommand('composer dump-autoload')
426+
];
427+
281428
yield 'migration_with_changes' => [MakerTestDetails::createTest(
282429
$this->getMakerInstance(MakeMigration::class),
283430
[/* no input */])
@@ -302,6 +449,33 @@ public function getCommandTests()
302449
})
303450
];
304451

452+
yield 'migration_with_changes_in_custom_root_namespace' => [MakerTestDetails::createTest(
453+
$this->getMakerInstance(MakeMigration::class),
454+
[/* no input */])
455+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeMigrationInCustomRootNamespace')
456+
->changeRootNamespace('Custom')
457+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
458+
->addPreMakeCommand('composer dump-autoload')
459+
->configureDatabase(false)
460+
// doctrine-migrations-bundle only requires doctrine-bundle, which
461+
// only requires doctrine/dbal. But we're testing with the ORM,
462+
// so let's install it
463+
->addExtraDependencies('doctrine/orm')
464+
->assert(function(string $output, string $directory) {
465+
$this->assertContains('Success', $output);
466+
467+
$finder = new Finder();
468+
$finder->in($directory.'/src/Migrations')
469+
->name('*.php');
470+
$this->assertCount(1, $finder);
471+
472+
// see that the exact filename is in the output
473+
$iterator = $finder->getIterator();
474+
$iterator->rewind();
475+
$this->assertContains(sprintf('"src/Migrations/%s"', $iterator->current()->getFilename()), $output);
476+
})
477+
];
478+
305479
yield 'migration_no_changes' => [MakerTestDetails::createTest(
306480
$this->getMakerInstance(MakeMigration::class),
307481
[/* no input */])
@@ -331,6 +505,24 @@ public function getCommandTests()
331505
})
332506
];
333507

508+
yield 'crud_basic_in_custom_root_namespace' => [MakerTestDetails::createTest(
509+
$this->getMakerInstance(MakeCrud::class),
510+
[
511+
// entity class name
512+
'SweetFood',
513+
])
514+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCrudInCustomRootNamespace')
515+
->changeRootNamespace('Custom')
516+
->addPreMakeCommand("echo 'maker:\n root_namespace: Custom\n' > config/packages/dev/maker.yaml")
517+
->addPreMakeCommand('composer dump-autoload')
518+
// need for crud web tests
519+
->configureDatabase()
520+
->assert(function(string $output, string $directory) {
521+
$this->assertContains('created: src/Controller/SweetFoodController.php', $output);
522+
$this->assertContains('created: src/Form/SweetFoodType.php', $output);
523+
})
524+
];
525+
334526
yield 'crud_repository' => [MakerTestDetails::createTest(
335527
$this->getMakerInstance(MakeCrud::class),
336528
[
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
parameters:
2+
# Adds a fallback DATABASE_URL if the env var is not set.
3+
# This allows you to run cache:warmup even if your
4+
# environment variables are not available yet.
5+
# You should not need to change this value.
6+
env(DATABASE_URL): ''
7+
8+
doctrine:
9+
dbal:
10+
# configure these for your database server
11+
driver: 'pdo_mysql'
12+
server_version: '5.7'
13+
charset: utf8mb4
14+
default_table_options:
15+
charset: utf8mb4
16+
collate: utf8mb4_unicode_ci
17+
18+
url: '%env(resolve:DATABASE_URL)%'
19+
orm:
20+
auto_generate_proxy_classes: '%kernel.debug%'
21+
naming_strategy: doctrine.orm.naming_strategy.underscore
22+
auto_mapping: true
23+
mappings:
24+
Custom:
25+
is_bundle: false
26+
type: annotation
27+
dir: '%kernel.project_dir%/src/Entity'
28+
prefix: 'Custom\Entity'
29+
alias: Custom

0 commit comments

Comments
 (0)