Skip to content

Commit 25c6902

Browse files
committed
skipping tests before DoctrineMigrationsBundle 3
1 parent bfda162 commit 25c6902

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"symfony/http-kernel": "^3.4|^4.0|^5.0"
2626
},
2727
"require-dev": {
28+
"composer/semver": "^3.0@dev",
2829
"doctrine/doctrine-bundle": "^1.8|^2.0",
2930
"doctrine/orm": "^2.3",
3031
"friendsofphp/php-cs-fixer": "^2.8",

src/Test/MakerTestCase.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Test;
1313

14+
use Composer\Semver\Semver;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Bundle\MakerBundle\MakerInterface;
1617
use Symfony\Bundle\MakerBundle\Str;
@@ -40,6 +41,10 @@ protected function executeMakerCommand(MakerTestDetails $testDetails)
4041
// prepare environment to test
4142
$testEnv->prepare();
4243

44+
if (!$this->hasRequiredDependencyVersions($testDetails, $testEnv)) {
45+
$this->markTestSkipped('Some dependencies versions are too low');
46+
}
47+
4348
// run tests
4449
$makerTestProcess = $testEnv->runMaker();
4550
$files = $testEnv->getGeneratedFilesFromOutputText();
@@ -95,4 +100,32 @@ protected function getMakerInstance(string $makerClass): MakerInterface
95100

96101
return $this->kernel->getContainer()->get($serviceId);
97102
}
103+
104+
private function hasRequiredDependencyVersions(MakerTestDetails $testDetails, MakerTestEnvironment $testEnv): bool
105+
{
106+
if (empty($testDetails->getRequiredPackageVersions())) {
107+
return true;
108+
}
109+
110+
$installedPackages = json_decode($testEnv->readFile('vendor/composer/installed.json'), true);
111+
$packageVersions = [];
112+
foreach ($installedPackages as $installedPackage) {
113+
$packageVersions[$installedPackage['name']] = $installedPackage['version_normalized'];
114+
}
115+
116+
foreach ($testDetails->getRequiredPackageVersions() as $requiredPackageData) {
117+
$name = $requiredPackageData['name'];
118+
$versionConstraint = $requiredPackageData['version_constraint'];
119+
120+
if (!isset($packageVersions[$name])) {
121+
throw new \Exception(sprintf('Package "%s" is required in the test project at version "%s" but it is not installed?', $name, $versionConstraint));
122+
}
123+
124+
if (!Semver::satisfies($packageVersions[$name], $versionConstraint)) {
125+
return false;
126+
}
127+
}
128+
129+
return true;
130+
}
98131
}

src/Test/MakerTestDetails.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ final class MakerTestDetails
4444

4545
private $requiredPhpVersion;
4646

47+
private $requiredPackageVersions = [];
48+
4749
private $guardAuthenticators = [];
4850

4951
/**
@@ -216,6 +218,13 @@ public function setRequiredPhpVersion(int $version): self
216218
return $this;
217219
}
218220

221+
public function addRequiredPackageVersion(string $packageName, string $versionConstraint): self
222+
{
223+
$this->requiredPackageVersions[] = ['name' => $packageName, 'version_constraint' => $versionConstraint];
224+
225+
return $this;
226+
}
227+
219228
public function setGuardAuthenticator(string $firewallName, string $id): self
220229
{
221230
$this->guardAuthenticators[$firewallName] = $id;
@@ -316,4 +325,9 @@ public function getGuardAuthenticators(): array
316325
{
317326
return $this->guardAuthenticators;
318327
}
328+
329+
public function getRequiredPackageVersions(): array
330+
{
331+
return $this->requiredPackageVersions;
332+
}
319333
}

src/Test/MakerTestEnvironment.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public function getPath(): string
6969
return $this->path;
7070
}
7171

72+
public function readFile(string $path): string
73+
{
74+
if (!file_exists($this->path.'/'.$path)) {
75+
throw new \InvalidArgumentException(sprintf('Cannot find file "%s"', $path));
76+
}
77+
78+
return file_get_contents($this->path.'/'.$path);
79+
}
80+
7281
private function changeRootNamespaceIfNeeded()
7382
{
7483
if ('App' === ($rootNamespace = $this->testDetails->getRootNamespace())) {

tests/Maker/MakeMigrationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ public function getTestDetails()
6969
])
7070
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeMigration')
7171
->configureDatabase(false)
72+
->addRequiredPackageVersion('doctrine/doctrine-migrations-bundle', '>=3')
7273
->addExtraDependencies('doctrine/orm:@stable')
7374
// generate a migration first
7475
->addPreMakeCommand('php bin/console make:migration')
7576
->assert(function (string $output, string $directory) {
7677
$this->assertStringContainsString('You have 1 available migrations to execute', $output);
7778
$this->assertStringContainsString('Success', $output);
7879
$this->assertCount(14, explode("\n", $output), 'Asserting that very specific output is shown - some should be hidden');
79-
var_dump($output);
8080
}),
8181
];
8282

@@ -88,6 +88,7 @@ public function getTestDetails()
8888
])
8989
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeMigration')
9090
->configureDatabase(false)
91+
->addRequiredPackageVersion('doctrine/doctrine-migrations-bundle', '>=3')
9192
->addExtraDependencies('doctrine/orm:@stable')
9293
// generate a migration first
9394
->addPreMakeCommand('php bin/console make:migration')

0 commit comments

Comments
 (0)