Skip to content

Commit

Permalink
Add test for drupal-finder in composer scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
webflo committed Jun 18, 2024
1 parent 3403675 commit c6cf59a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"require-dev": {
"phpunit/phpunit": "^10.4",
"mikey179/vfsstream": "^1.6"
"mikey179/vfsstream": "^1.6",
"symfony/process": "^6.4"
},
"config": {
"platform": {
Expand Down
2 changes: 2 additions & 0 deletions tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[composer.json]
indent_size = 2
32 changes: 28 additions & 4 deletions tests/DrupalFinderComposerRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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');
Expand All @@ -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');
Expand Down
19 changes: 19 additions & 0 deletions tests/fixtures/default/TestAsComposerScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace DrupalFinder\Tests\Fixtures\Default;

use Composer\Script\Event;
use DrupalFinder\DrupalFinderComposerRuntime;

class TestAsComposerScript {

public static function dumpDrupalFinder(Event $event) {
$finder = new DrupalFinderComposerRuntime();
$event->getIO()->writeRaw(json_encode([
'getComposerRoot' => $finder->getComposerRoot(),
'getVendorDir' => $finder->getVendorDir(),
'getDrupalRoot' => $finder->getDrupalRoot(),
]));
}

}
10 changes: 9 additions & 1 deletion tests/fixtures/default/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit c6cf59a

Please sign in to comment.