Skip to content

Commit c167d3a

Browse files
authored
Drush functions autoloading (#65)
* Add integration testing with Drush * Load Drush legacy includes * Return checking
1 parent 3dba0b9 commit c167d3a

File tree

8 files changed

+61
-4
lines changed

8 files changed

+61
-4
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ composer.phar
22
composer.lock
33
/vendor/
44
/clover.xml
5-
/tests/fixtures/drupal/core
65
.circleci/config_local.yml

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"phpunit/phpunit": "^7.5",
2121
"phpstan/phpstan-deprecation-rules": "^0.11.0",
2222
"composer/installers": "^1.6",
23-
"drupal/core": "^8.6"
23+
"drupal/core": "^8.6",
24+
"drush/drush": "^9.6"
2425
},
2526
"conflict": {
2627
"nette/di": ">=3.0"

src/Drupal/Bootstrap.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public function register(): void
6666
throw new \RuntimeException('Cannot determine the Drupal root from ' . $drupalRoot);
6767
}
6868
$this->drupalRoot = $drupalRoot;
69-
$this->autoloader = include realpath($GLOBALS['drupalVendorDir']) . '/autoload.php';
69+
70+
$drupalVendorRoot = realpath($GLOBALS['drupalVendorDir']);
71+
$this->autoloader = include $drupalVendorRoot . '/autoload.php';
7072

7173
$this->extensionDiscovery = new ExtensionDiscovery($this->drupalRoot);
7274
$this->extensionDiscovery->setProfileDirectories([]);
@@ -121,6 +123,17 @@ public function register(): void
121123
foreach ($this->themeData as $extension) {
122124
$this->loadExtension($extension);
123125
}
126+
127+
if (class_exists(\Drush\Drush::class)) {
128+
$reflect = new \ReflectionClass(\Drush\Drush::class);
129+
if ($reflect->getFileName() !== false) {
130+
$drushDir = dirname($reflect->getFileName(), 2);
131+
/** @var \SplFileInfo $file */
132+
foreach (Finder::findFiles('*.inc')->in($drushDir . '/includes') as $file) {
133+
require_once $file->getPathname();
134+
}
135+
}
136+
}
124137
}
125138

126139
protected function loadLegacyIncludes(): void

tests/DrupalIntegrationTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ public function testDrupalTestInChildSiteContant() {
5050
$this->assertCount(0, $errors);
5151
}
5252

53+
public function testDrupalModuleWithDrushCommands() {
54+
$path = __DIR__ . '/fixtures/drupal/modules/drush_command/src/Commands/TestDrushCommands.php';
55+
$errors = $this->runAnalyze($path);
56+
$this->assertCount(0, $errors, print_r($errors, true));
57+
}
58+
5359
private function runAnalyze(string $path) {
5460
$rootDir = __DIR__ . '/fixtures/drupal';
61+
$tmpDir = sys_get_temp_dir() . '/' . time() . 'phpstan';
5562
$containerFactory = new ContainerFactory($rootDir);
5663
$container = $containerFactory->create(
57-
sys_get_temp_dir() . '/' . time() . 'phpstan',
64+
$tmpDir,
5865
[__DIR__ . '/fixtures/config/phpunit-drupal-phpstan.neon'],
5966
[]
6067
);

tests/fixtures/drupal/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/core
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
drush_command.commands:
3+
class: Drupal\drush_command\Commands\TestDrushCommands
4+
tags:
5+
- { name: drush.command }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: drush_command
2+
type: module
3+
core: 8.x
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Drupal\drush_command\Commands;
4+
5+
use Drush\Commands\DrushCommands;
6+
7+
class TestDrushCommands extends DrushCommands {
8+
9+
/**
10+
* Test
11+
*
12+
* @command phpstan:example
13+
*/
14+
public function example() {
15+
if (drush_is_osx()) {
16+
$this->io()->writeln('macOS');
17+
} elseif (drush_is_cygwin() || drush_is_mingw()) {
18+
$this->io()->writeln('Windows');
19+
} else {
20+
$this->io()->writeln('Linux ¯\_(ツ)_/¯');
21+
}
22+
}
23+
24+
public function batchProcess() {
25+
drush_backend_batch_process();
26+
}
27+
28+
}

0 commit comments

Comments
 (0)