Skip to content

Commit

Permalink
minor #891 [tests] housekeeping (jrushlow)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 1.0-dev branch.

Discussion
----------

[tests] housekeeping

- removes `override_url` config logic (feature has was introduced && deprecated in doctrine-bundle 2.3)
- remove duplicate directory getter
- run generated projects `bin/phpunit` instead of "makers" `simple-phpunit`
- add return types to touched classes

Commits
-------

5681a5f [tests] housekeeping
  • Loading branch information
weaverryan committed Jun 6, 2021
2 parents 5da4340 + 5681a5f commit baec3cb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 65 deletions.
26 changes: 13 additions & 13 deletions src/Test/MakerTestEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function readFile(string $path): string
return file_get_contents($this->path.'/'.$path);
}

private function changeRootNamespaceIfNeeded()
private function changeRootNamespaceIfNeeded(): void
{
if ('App' === ($rootNamespace = $this->testDetails->getRootNamespace())) {
return;
Expand Down Expand Up @@ -128,7 +128,7 @@ private function changeRootNamespaceIfNeeded()
$this->processReplacements($replacements, $this->path);
}

public function prepare()
public function prepare(): void
{
if (!$this->fs->exists($this->flexPath)) {
$this->buildFlexSkeleton();
Expand Down Expand Up @@ -198,15 +198,15 @@ public function prepare()
->run();
}

private function preMake()
private function preMake(): void
{
foreach ($this->testDetails->getPreMakeCommands() as $preCommand) {
MakerTestProcess::create($preCommand, $this->path)
->run();
}
}

public function runMaker()
public function runMaker(): MakerTestProcess
{
// Lets remove cache
$this->fs->remove($this->path.'/var/cache');
Expand Down Expand Up @@ -248,7 +248,7 @@ public function runMaker()
return $this->runnedMakerProcess;
}

public function getGeneratedFilesFromOutputText()
public function getGeneratedFilesFromOutputText(): array
{
$output = $this->runnedMakerProcess->getOutput();

Expand All @@ -259,37 +259,37 @@ public function getGeneratedFilesFromOutputText()
return array_map('trim', $matches[3]);
}

public function fileExists(string $file)
public function fileExists(string $file): bool
{
return $this->fs->exists($this->path.'/'.$file);
}

public function runPhpCSFixer(string $file)
public function runPhpCSFixer(string $file): MakerTestProcess
{
return MakerTestProcess::create(sprintf('php vendor/bin/php-cs-fixer --config=%s fix --dry-run --diff %s', __DIR__.'/../Resources/test/.php_cs.test', $this->path.'/'.$file), $this->rootPath)
->run(true);
}

public function runTwigCSLint(string $file)
public function runTwigCSLint(string $file): MakerTestProcess
{
return MakerTestProcess::create(sprintf('php vendor/bin/twigcs --config ./.twig_cs.dist %s', $this->path.'/'.$file), $this->rootPath)
->run(true);
}

public function runInternalTests()
public function runInternalTests(): ?MakerTestProcess
{
$finder = new Finder();
$finder->in($this->path.'/tests')->files();
if ($finder->count() > 0) {
// execute the tests that were moved into the project!
return MakerTestProcess::create(sprintf('php %s', $this->rootPath.'/vendor/bin/simple-phpunit'), $this->path)
return MakerTestProcess::create(sprintf('php %s', $this->path.'/bin/phpunit'), $this->path)
->run(true);
}

return null;
}

private function postMake()
private function postMake(): void
{
$this->processReplacements($this->testDetails->getPostMakeReplacements(), $this->path);

Expand Down Expand Up @@ -318,7 +318,7 @@ private function postMake()
}
}

private function buildFlexSkeleton()
private function buildFlexSkeleton(): void
{
$targetVersion = $this->getTargetSkeletonVersion();
$versionString = $targetVersion ? sprintf(':%s', $targetVersion) : '';
Expand Down Expand Up @@ -397,7 +397,7 @@ private function buildFlexSkeleton()
)->run();
}

private function processReplacements(array $replacements, $rootDir)
private function processReplacements(array $replacements, $rootDir): void
{
foreach ($replacements as $replacement) {
$path = realpath($rootDir.'/'.$replacement['filename']);
Expand Down
65 changes: 13 additions & 52 deletions tests/Doctrine/EntityRegeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bundle\MakerBundle\Tests\Doctrine;

use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -41,7 +40,7 @@ class EntityRegeneratorTest extends TestCase
/**
* @dataProvider getRegenerateEntitiesTests
*/
public function testRegenerateEntities(string $expectedDirName, bool $overwrite)
public function testRegenerateEntities(string $expectedDirName, bool $overwrite): void
{
/*
* Prior to symfony/doctrine-bridge 5.0 (which require
Expand All @@ -65,7 +64,7 @@ public function testRegenerateEntities(string $expectedDirName, bool $overwrite)
);
}

public function getRegenerateEntitiesTests()
public function getRegenerateEntitiesTests(): \Generator
{
yield 'regenerate_no_overwrite' => [
'expected_no_overwrite',
Expand All @@ -78,7 +77,7 @@ public function getRegenerateEntitiesTests()
];
}

public function testXmlRegeneration()
public function testXmlRegeneration(): void
{
$kernel = new TestXmlEntityRegeneratorKernel('dev', true);
$this->doTestRegeneration(
Expand All @@ -91,7 +90,7 @@ public function testXmlRegeneration()
);
}

private function doTestRegeneration(string $sourceDir, Kernel $kernel, string $namespace, string $expectedDirName, bool $overwrite, string $targetDirName)
private function doTestRegeneration(string $sourceDir, Kernel $kernel, string $namespace, string $expectedDirName, bool $overwrite, string $targetDirName): void
{
$fs = new Filesystem();
$tmpDir = __DIR__.'/../tmp/'.$targetDirName;
Expand Down Expand Up @@ -157,21 +156,20 @@ private function createAllButTraitsIterator(string $sourceDir): \Iterator
class TestEntityRegeneratorKernel extends Kernel
{
use MicroKernelTrait;
use OverrideUrlTraitFixture;

public function registerBundles()
public function registerBundles(): array
{
return [
new FrameworkBundle(),
new DoctrineBundle(),
];
}

protected function configureRoutes(RouteCollectionBuilder $routes)
protected function configureRoutes(RouteCollectionBuilder $routes): void
{
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
{
$c->loadFromExtension('framework', [
'secret' => 123,
Expand All @@ -185,10 +183,6 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'url' => 'sqlite:///fake',
];

if ($this->canOverrideUrl($c)) {
$dbal['override_url'] = true;
}

$c->prependExtensionConfig('doctrine', [
'dbal' => $dbal,
'orm' => [
Expand All @@ -205,12 +199,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
]);
}

public function getProjectDir()
{
return $this->getRootDir();
}

public function getRootDir()
public function getProjectDir(): string
{
return __DIR__.'/../tmp/current_project';
}
Expand All @@ -219,21 +208,20 @@ public function getRootDir()
class TestXmlEntityRegeneratorKernel extends Kernel
{
use MicroKernelTrait;
use OverrideUrlTraitFixture;

public function registerBundles()
public function registerBundles(): array
{
return [
new FrameworkBundle(),
new DoctrineBundle(),
];
}

protected function configureRoutes(RouteCollectionBuilder $routes)
protected function configureRoutes(RouteCollectionBuilder $routes): void
{
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
{
$c->loadFromExtension('framework', [
'secret' => 123,
Expand All @@ -247,10 +235,6 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'url' => 'sqlite:///fake',
];

if ($this->canOverrideUrl($c)) {
$dbal['override_url'] = true;
}

$c->prependExtensionConfig('doctrine', [
'dbal' => $dbal,
'orm' => [
Expand All @@ -268,39 +252,16 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
]);
}

public function getProjectDir()
{
return $this->getRootDir();
}

public function getRootDir()
public function getProjectDir(): string
{
return __DIR__.'/../tmp/current_project_xml';
}
}

class AllButTraitsIterator extends \RecursiveFilterIterator
{
public function accept()
public function accept(): bool
{
return !\in_array($this->current()->getFilename(), []);
}
}

trait OverrideUrlTraitFixture
{
/**
* Quick and dirty way to check if override_url is required since doctrine-bundle 2.3.
*/
public function canOverrideUrl(ContainerBuilder $builder): bool
{
/** @var DoctrineExtension $ext */
$ext = $builder->getExtension('doctrine');
$method = new \ReflectionMethod(DoctrineExtension::class, 'getConnectionOptions');
$method->setAccessible(true);

$configOptions = $method->invoke($ext, ['override_url' => 'string', 'shards' => [], 'replicas' => [], 'slaves' => []]);

return \array_key_exists('connection_override_options', $configOptions);
}
}

0 comments on commit baec3cb

Please sign in to comment.