From c6cf59aff15348da816c3b76f3396af1921a52c7 Mon Sep 17 00:00:00 2001 From: Florian Weber Date: Wed, 19 Jun 2024 01:00:53 +0200 Subject: [PATCH] Add test for drupal-finder in composer scripts --- composer.json | 3 +- tests/.editorconfig | 2 ++ tests/DrupalFinderComposerRuntimeTest.php | 32 ++++++++++++++++--- .../fixtures/default/TestAsComposerScript.php | 19 +++++++++++ tests/fixtures/default/composer.json | 10 +++++- 5 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 tests/.editorconfig create mode 100644 tests/fixtures/default/TestAsComposerScript.php diff --git a/composer.json b/composer.json index f013761..b6a2015 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ }, "require-dev": { "phpunit/phpunit": "^10.4", - "mikey179/vfsstream": "^1.6" + "mikey179/vfsstream": "^1.6", + "symfony/process": "^6.4" }, "config": { "platform": { diff --git a/tests/.editorconfig b/tests/.editorconfig new file mode 100644 index 0000000..dcb31cb --- /dev/null +++ b/tests/.editorconfig @@ -0,0 +1,2 @@ +[composer.json] +indent_size = 2 diff --git a/tests/DrupalFinderComposerRuntimeTest.php b/tests/DrupalFinderComposerRuntimeTest.php index 1f03a7f..59d0599 100644 --- a/tests/DrupalFinderComposerRuntimeTest.php +++ b/tests/DrupalFinderComposerRuntimeTest.php @@ -3,6 +3,8 @@ namespace DrupalFinder\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\Process; class DrupalFinderComposerRuntimeTest extends TestCase { @@ -12,11 +14,33 @@ class DrupalFinderComposerRuntimeTest extends TestCase { * @runInSeparateProcess */ public function testDefault() { - $basePath = realpath( __DIR__ . '/fixtures/default'); + $basePath = realpath(__DIR__ . '/fixtures/default'); $this->assertDirectoryExists($basePath . '/vendor', static::installFixtures); $this->assertDirectoryExists($basePath . '/web', static::installFixtures); - $result = json_decode(require $basePath . '/drupal-finder.php', TRUE); + $result = json_decode(require $basePath . '/drupal-finder.php', TRUE); + $this->assertSame($result['getComposerRoot'], $basePath); + $this->assertSame($result['getVendorDir'], $basePath . '/vendor'); + $this->assertSame($result['getDrupalRoot'], $basePath . '/web'); + } + + /** + * @runInSeparateProcess + */ + public function testDefaultComposerScript() { + $basePath = realpath(__DIR__ . '/fixtures/default'); + $this->assertDirectoryExists($basePath . '/vendor', static::installFixtures); + $this->assertDirectoryExists($basePath . '/web', static::installFixtures); + + $process = new Process(['composer', 'run-script', 'dump-drupal-finder'], $basePath); + $process->run(); + + // executes after the command finishes + if (!$process->isSuccessful()) { + throw new ProcessFailedException($process); + } + + $result = json_decode($process->getOutput(), TRUE); $this->assertSame($result['getComposerRoot'], $basePath); $this->assertSame($result['getVendorDir'], $basePath . '/vendor'); $this->assertSame($result['getDrupalRoot'], $basePath . '/web'); @@ -26,11 +50,11 @@ public function testDefault() { * @runInSeparateProcess */ public function testCustomVendor() { - $basePath = realpath( __DIR__ . '/fixtures/custom-vendor'); + $basePath = realpath(__DIR__ . '/fixtures/custom-vendor'); $this->assertDirectoryExists($basePath . '/foo/bar', static::installFixtures); $this->assertDirectoryExists($basePath . '/foo/bar/drupal', static::installFixtures); - $result = json_decode(require $basePath . '/drupal-finder.php', TRUE); + $result = json_decode(require $basePath . '/drupal-finder.php', TRUE); $this->assertSame($result['getComposerRoot'], $basePath); $this->assertSame($result['getVendorDir'], $basePath . '/foo/bar'); $this->assertSame($result['getDrupalRoot'], $basePath . '/foo/bar/drupal'); diff --git a/tests/fixtures/default/TestAsComposerScript.php b/tests/fixtures/default/TestAsComposerScript.php new file mode 100644 index 0000000..7a275e3 --- /dev/null +++ b/tests/fixtures/default/TestAsComposerScript.php @@ -0,0 +1,19 @@ +getIO()->writeRaw(json_encode([ + 'getComposerRoot' => $finder->getComposerRoot(), + 'getVendorDir' => $finder->getVendorDir(), + 'getDrupalRoot' => $finder->getDrupalRoot(), + ])); + } + +} diff --git a/tests/fixtures/default/composer.json b/tests/fixtures/default/composer.json index 7741bd3..4fa7b69 100644 --- a/tests/fixtures/default/composer.json +++ b/tests/fixtures/default/composer.json @@ -23,5 +23,13 @@ } }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "autoload": { + "classmap": [ + "TestAsComposerScript.php" + ] + }, + "scripts": { + "dump-drupal-finder": "\\DrupalFinder\\Tests\\Fixtures\\Default\\TestAsComposerScript::dumpDrupalFinder" + } }