Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log messages #5624

Draft
wants to merge 8 commits into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 1 addition & 6 deletions sut/modules/unish/woot/drush.services.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
services:
woot.manager:
class: Drupal\woot\WootManager
arguments: ['@current_user']
tags:
- { name: drush.command }
woot.command:
class: Drupal\woot\Commands\WootCommands
arguments: ['%app.root%']
arguments: ['%app.root%', '@woot.manager']
tags:
- { name: drush.command }
greet.command:
Expand Down
18 changes: 17 additions & 1 deletion sut/modules/unish/woot/src/Commands/WootCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
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
{
public function __construct(protected string $appRoot) {}
public function __construct(protected string $appRoot, protected WootManager $wootManager) {}

/**
* Woot mightily.
Expand Down Expand Up @@ -92,4 +94,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();
}
}
8 changes: 7 additions & 1 deletion sut/modules/unish/woot/src/WootManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Drupal\woot;

use Drupal\Core\Session\AccountProxyInterface;
use Psr\Log\LoggerInterface;

/**
* A simulated service for wooting.
Expand All @@ -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;
}

/**
Expand All @@ -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!';
}
}
9 changes: 9 additions & 0 deletions sut/modules/unish/woot/woot.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
16 changes: 16 additions & 0 deletions tests/functional/ModuleDrushCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@ public function testDrushCommandsInModule()
$this->drush('woot:root');
$this->assertStringContainsString('/sut', $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', [], []);
$this->assertStringContainsString('Message 1', $this->getErrorOutput());
$this->assertStringContainsString('Message 2', $this->getErrorOutput());
$this->assertStringContainsString('Message 3', $this->getErrorOutput());
$this->assertStringContainsString('Message 4', $this->getErrorOutput());
}
}