From 6e787fe86b94a793fffd30160fd63869cdca9084 Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Mon, 5 Jun 2023 11:11:30 +0100 Subject: [PATCH 1/7] Log message test without using drush.services.yml --- sut/modules/unish/woot/drush.services.yml | 6 +--- .../unish/woot/src/Commands/WootCommands.php | 33 +++++++++++++++++++ sut/modules/unish/woot/src/WootManager.php | 8 ++++- sut/modules/unish/woot/woot.services.yml | 9 +++++ tests/functional/ModuleDrushCommandTest.php | 18 ++++++++++ 5 files changed, 68 insertions(+), 6 deletions(-) diff --git a/sut/modules/unish/woot/drush.services.yml b/sut/modules/unish/woot/drush.services.yml index 5359da5d8f..7c06badfac 100644 --- a/sut/modules/unish/woot/drush.services.yml +++ b/sut/modules/unish/woot/drush.services.yml @@ -1,11 +1,7 @@ services: - woot.manager: - class: Drupal\woot\WootManager - arguments: ['@current_user'] - tags: - - { name: drush.command } woot.command: class: Drupal\woot\Commands\WootCommands + arguments: ['@woot.manager'] tags: - { name: drush.command } greet.command: diff --git a/sut/modules/unish/woot/src/Commands/WootCommands.php b/sut/modules/unish/woot/src/Commands/WootCommands.php index 9c871aa586..5ccc2ef463 100644 --- a/sut/modules/unish/woot/src/Commands/WootCommands.php +++ b/sut/modules/unish/woot/src/Commands/WootCommands.php @@ -5,12 +5,31 @@ namespace Drupal\woot\Commands; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; +use Drush\Drush; +use Drupal\woot\WootManager; /** * Commandfiles must be listed in a module's drush.services.yml file. */ class WootCommands { + /** + * The Woot manager service. + * + * @var \Drupal\woot\WootManager + */ + protected $wootManager; + + /** + * WootCommands constructor. + * + * @param \Drupal\woot\WootManager $wootManager + * Woot manager service. + */ + public function __construct(WootManager $wootManager) { + $this->wootManager = $wootManager; + } + /** * Woot mightily. * @@ -82,4 +101,18 @@ public function tryFormatters($options = ['format' => 'table', 'fields' => '']): public function wootAltered() { } + + /** + * Command to demonstrate phpunit message retrieval problem. + * @see https://github.com/drush-ops/drush/issues/5572 + * + * @command woot:messages + * @aliases woot-messages + */ + public function wootMessages() + { + \Drupal::messenger()->addMessage('Message 1 - direct from command using Drupal::messenger()'); + Drush::logger()->notice('Message 2 - direct from command using logger()->notice()'); + $this->wootManager->woof(); + } } diff --git a/sut/modules/unish/woot/src/WootManager.php b/sut/modules/unish/woot/src/WootManager.php index a0b2e67019..212d66624f 100644 --- a/sut/modules/unish/woot/src/WootManager.php +++ b/sut/modules/unish/woot/src/WootManager.php @@ -5,6 +5,7 @@ namespace Drupal\woot; use Drupal\Core\Session\AccountProxyInterface; +use Psr\Log\LoggerInterface; /** * A simulated service for wooting. @@ -13,9 +14,12 @@ class WootManager { protected AccountProxyInterface $currentUser; - public function __construct(AccountProxyInterface $current_user) + protected LoggerInterface $logger; + + public function __construct(AccountProxyInterface $current_user, LoggerInterface $logger) { $this->currentUser = $current_user; + $this->logger = $logger; } /** @@ -27,6 +31,8 @@ public function __construct(AccountProxyInterface $current_user) */ public function woof(): string { + \Drupal::logger('woot')->notice('Message 3 - via wootManager::woof() using \Drupal::logger(woot)->notice'); + $this->logger->notice('Message 4 - via wootManager::woof() using this->logger->notice'); return 'Woof!'; } } diff --git a/sut/modules/unish/woot/woot.services.yml b/sut/modules/unish/woot/woot.services.yml index aafcb46a82..7cfad94004 100644 --- a/sut/modules/unish/woot/woot.services.yml +++ b/sut/modules/unish/woot/woot.services.yml @@ -11,3 +11,12 @@ services: class: Drupal\woot\EventSubscriber\PreRowDeleteTestSubscriber tags: - { name: event_subscriber } + logger.channel.woot: + class: Drupal\Core\Logger\LoggerChannel + factory: logger.factory:get + arguments: ['woot'] + woot.manager: + class: Drupal\woot\WootManager + arguments: + - '@current_user' + - '@logger.channel.woot' diff --git a/tests/functional/ModuleDrushCommandTest.php b/tests/functional/ModuleDrushCommandTest.php index 842da6d3e4..18a4392a28 100644 --- a/tests/functional/ModuleDrushCommandTest.php +++ b/tests/functional/ModuleDrushCommandTest.php @@ -35,4 +35,22 @@ public function testDrushCommandsInModule() $this->drush('woot-factory', [], []); $this->assertStringContainsString('Woot 55', $this->getOutput()); } + + /** + * Tests retrieval of log messages. + */ + public function testLogMessageRetrieval() + { + $this->setUpDrupal(1, true); + // Install the woot module. + $this->drush(PmCommands::INSTALL, ['woot']); + + $this->drush('woot-messages', [], []); + + var_dump($this->getErrorOutput()); + $this->assertStringContainsString('Message 1', $this->getErrorOutput()); + $this->assertStringContainsString('Message 2', $this->getErrorOutput()); + $this->assertStringContainsString('Message 3', $this->getErrorOutput()); + $this->assertStringContainsString('Message 4', $this->getErrorOutput()); + } } From 2256d23caaa8edf20fac3620285012125852b23d Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Mon, 5 Jun 2023 15:42:45 +0100 Subject: [PATCH 2/7] Document file exclusions. Add phpcs summary and source --- CONTRIBUTING.md | 1 + composer.json | 4 ++-- sut/modules/unish/woot/src/WootManager.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7b2e78978d..4a29be4630 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,6 +18,7 @@ Drush is built by people like you! Please [join us](https://github.com/drush-ops * We use [PSR-12](https://www.php-fig.org/psr/psr-12/). * Keep it compatible. Do not introduce changes to the public API, or configurations too casually. Don't make incompatible changes without good reasons! * Run `composer cs` to check the project for coding style issues and run `composer cbf` to fix them automatically where possible. These scripts use [`PHP_CodeSniffer`](https://github.com/squizlabs/PHP_CodeSniffer) in background. +* Files in the `src`, `examples` and `tests` folders are checked. Files in other folders are excluded. ## Documentation * The docs are on our [web site](https://www.drush.org). You may also read these from within Drush, with the `drush topic` command. diff --git a/composer.json b/composer.json index 37c231cca9..b2b4ad9e7c 100644 --- a/composer.json +++ b/composer.json @@ -94,8 +94,8 @@ } }, "scripts": { - "cs": "phpcs", - "cbf": "phpcbf", + "cs": "phpcs --colors --report-full --report-summary --report-source", + "cbf": "phpcbf --colors", "lint": [ "find includes -name '*.inc' -print0 | xargs -0 -n1 php -l", "find src -name '*.php' -and ! -path 'src/Attributes/*' -print0 | xargs -0 -n1 php -l", diff --git a/sut/modules/unish/woot/src/WootManager.php b/sut/modules/unish/woot/src/WootManager.php index 212d66624f..a22edd12b2 100644 --- a/sut/modules/unish/woot/src/WootManager.php +++ b/sut/modules/unish/woot/src/WootManager.php @@ -5,7 +5,7 @@ namespace Drupal\woot; use Drupal\Core\Session\AccountProxyInterface; -use Psr\Log\LoggerInterface; +use Psr\Log\LoggerInterface; /** * A simulated service for wooting. From 867fc6252863b51d226841f290702f2d998d470a Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Mon, 5 Jun 2023 16:07:41 +0100 Subject: [PATCH 3/7] Remove var_dump --- tests/functional/ModuleDrushCommandTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/functional/ModuleDrushCommandTest.php b/tests/functional/ModuleDrushCommandTest.php index 18a4392a28..6c89918771 100644 --- a/tests/functional/ModuleDrushCommandTest.php +++ b/tests/functional/ModuleDrushCommandTest.php @@ -46,8 +46,6 @@ public function testLogMessageRetrieval() $this->drush(PmCommands::INSTALL, ['woot']); $this->drush('woot-messages', [], []); - - var_dump($this->getErrorOutput()); $this->assertStringContainsString('Message 1', $this->getErrorOutput()); $this->assertStringContainsString('Message 2', $this->getErrorOutput()); $this->assertStringContainsString('Message 3', $this->getErrorOutput()); From 98a81b221e5ceafb4e020cbad7c3f60349959588 Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Tue, 6 Jun 2023 10:08:46 +0100 Subject: [PATCH 4/7] Put back wootManager --- sut/modules/unish/woot/src/Commands/WootCommands.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sut/modules/unish/woot/src/Commands/WootCommands.php b/sut/modules/unish/woot/src/Commands/WootCommands.php index 60c86f3382..b8cecd5c0b 100644 --- a/sut/modules/unish/woot/src/Commands/WootCommands.php +++ b/sut/modules/unish/woot/src/Commands/WootCommands.php @@ -26,7 +26,10 @@ class WootCommands * @param \Drupal\woot\WootManager $wootManager * Woot manager service. */ - public function __construct(protected string $appRoot, WootManager $wootManager) {} + public function __construct(protected string $appRoot, WootManager $wootManager) + { + $this->wootManager = $wootManager; + } /** * Woot mightily. From 2f7829a9786ff03b0732c6a4b3d950eede1da365 Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Tue, 6 Jun 2023 13:37:28 +0100 Subject: [PATCH 5/7] Remove definitions, assign directly by default --- .../unish/woot/src/Commands/WootCommands.php | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/sut/modules/unish/woot/src/Commands/WootCommands.php b/sut/modules/unish/woot/src/Commands/WootCommands.php index b8cecd5c0b..524cc38c0a 100644 --- a/sut/modules/unish/woot/src/Commands/WootCommands.php +++ b/sut/modules/unish/woot/src/Commands/WootCommands.php @@ -13,23 +13,8 @@ */ class WootCommands { - /** - * The Woot manager service. - * - * @var \Drupal\woot\WootManager - */ - protected $wootManager; - /** - * WootCommands constructor. - * - * @param \Drupal\woot\WootManager $wootManager - * Woot manager service. - */ - public function __construct(protected string $appRoot, WootManager $wootManager) - { - $this->wootManager = $wootManager; - } + public function __construct(protected string $appRoot, protected WootManager $wootManager) {} /** * Woot mightily. From 2af6337494288b69492e35883ca23281ebbb02b9 Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Wed, 7 Jun 2023 15:40:18 +0100 Subject: [PATCH 6/7] add coding faults to check cs command --- examples/Commands/ArtCommands.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/Commands/ArtCommands.php b/examples/Commands/ArtCommands.php index 6e793cc505..8420703005 100644 --- a/examples/Commands/ArtCommands.php +++ b/examples/Commands/ArtCommands.php @@ -2,7 +2,7 @@ namespace Drush\Commands; -use Consolidation\AnnotatedCommand\AnnotationData; +use Consolidation\AnnotatedCommand\AnnotationData; use Consolidation\AnnotatedCommand\CommandData; use Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface; use Consolidation\AnnotatedCommand\Events\CustomEventAwareTrait; @@ -28,10 +28,10 @@ class ArtCommands extends DrushCommands implements CustomEventAwareInterface { - use CustomEventAwareTrait; + use CustomEventAwareTrait; - /** @var string[] */ - protected ?array $arts; + /** @var string[] */ + protected ?array $arts; /** * Show a fabulous picture. @@ -46,7 +46,7 @@ public function art($art = '') $description = $data[$art]['description']; $path = $data[$art]['path']; $msg = dt( - 'Okay. Here is {art}: {description}', + 'Okay. Here is {art}: {description}', ['art' => $name, 'description' => $description] ); $this->output()->writeln("\n" . $msg . "\n"); From 69e09d785efd4ca4b0dc58ac47f32b08878b9d7a Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Wed, 7 Jun 2023 21:06:32 +0100 Subject: [PATCH 7/7] fix coding standards --- examples/Commands/ArtCommands.php | 10 +++++----- sut/modules/unish/woot/src/Commands/WootCommands.php | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/Commands/ArtCommands.php b/examples/Commands/ArtCommands.php index 8420703005..6e793cc505 100644 --- a/examples/Commands/ArtCommands.php +++ b/examples/Commands/ArtCommands.php @@ -2,7 +2,7 @@ namespace Drush\Commands; -use Consolidation\AnnotatedCommand\AnnotationData; +use Consolidation\AnnotatedCommand\AnnotationData; use Consolidation\AnnotatedCommand\CommandData; use Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface; use Consolidation\AnnotatedCommand\Events\CustomEventAwareTrait; @@ -28,10 +28,10 @@ class ArtCommands extends DrushCommands implements CustomEventAwareInterface { - use CustomEventAwareTrait; + use CustomEventAwareTrait; - /** @var string[] */ - protected ?array $arts; + /** @var string[] */ + protected ?array $arts; /** * Show a fabulous picture. @@ -46,7 +46,7 @@ public function art($art = '') $description = $data[$art]['description']; $path = $data[$art]['path']; $msg = dt( - 'Okay. Here is {art}: {description}', + 'Okay. Here is {art}: {description}', ['art' => $name, 'description' => $description] ); $this->output()->writeln("\n" . $msg . "\n"); diff --git a/sut/modules/unish/woot/src/Commands/WootCommands.php b/sut/modules/unish/woot/src/Commands/WootCommands.php index 524cc38c0a..384ac6d48f 100644 --- a/sut/modules/unish/woot/src/Commands/WootCommands.php +++ b/sut/modules/unish/woot/src/Commands/WootCommands.php @@ -13,7 +13,6 @@ */ class WootCommands { - public function __construct(protected string $appRoot, protected WootManager $wootManager) {} /**