Skip to content

Commit

Permalink
Allow warnings because we use phpdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapio authored and linawolf committed Feb 18, 2024
1 parent 7a65053 commit 403a325
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions resources/schema/guides.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<xsd:attribute name="input-format" type="xsd:string"/>
<xsd:attribute name="log-path" type="xsd:string"/>
<xsd:attribute name="fail-on-log" type="xsd:string"/>
<xsd:attribute name="fail-on-error" type="xsd:string"/>
<xsd:attribute name="show-progress" type="xsd:string"/>
<xsd:attribute name="theme" type="xsd:string"/>
<xsd:attribute name="default-code-language" type="xsd:string"/>
Expand Down
16 changes: 14 additions & 2 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use phpDocumentor\Guides\Settings\SettingsManager;
use phpDocumentor\Guides\Twig\Theme\ThemeManager;
use Psr\Clock\ClockInterface;
use Psr\Log\LogLevel;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
Expand Down Expand Up @@ -117,6 +118,13 @@ public function __construct(
'If set, returns a non-zero exit code as soon as any warnings/errors occur',
);

$this->addOption(
'fail-on-error',
null,
InputOption::VALUE_NONE,
'If set, returns a non-zero exit code as soon as any errors occur',
);

$this->addOption(
'theme',
null,
Expand Down Expand Up @@ -240,8 +248,12 @@ private function getSettingsOverriddenWithInput(InputInterface $input): ProjectS
$settings->setLogPath((string) $input->getOption('log-path'));
}

if ($input->getOption('fail-on-error')) {
$settings->setFailOnError(LogLevel::ERROR);
}

if ($input->getOption('fail-on-log')) {
$settings->setFailOnError(true);
$settings->setFailOnError(LogLevel::WARNING);
}

if (count($input->getOption('output-format')) > 0) {
Expand Down Expand Up @@ -290,7 +302,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($settings->isFailOnError()) {
$spyProcessor = new SpyProcessor();
$spyProcessor = new SpyProcessor($settings->getFailOnError());
$this->logger->pushProcessor($spyProcessor);
}

Expand Down
11 changes: 10 additions & 1 deletion src/Logger/SpyProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Psr\Log\LogLevel;

use function strtolower;

/**
* This decorator has an extra method to check whether anything was logged
Expand All @@ -25,14 +28,20 @@ final class SpyProcessor implements ProcessorInterface
{
private bool $hasBeenCalled = false;

public function __construct(private string|null $level = LogLevel::WARNING)
{
}

public function hasBeenCalled(): bool
{
return $this->hasBeenCalled;
}

public function __invoke(array|LogRecord $record): array|LogRecord
{
$this->hasBeenCalled = true;
if (strtolower($record['level_name']) === $this->level) {
$this->hasBeenCalled = true;
}

return $record;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Logger/SpyProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testHasBeenCalledReturnsFalseByDefault(): void
public function testItKnowsWhenALogIsEmitted(): void
{
$process = new SpyProcessor();
$process(['channel' => 'test']);
$process(['channel' => 'test', 'level_name' => 'warning']);
self::assertTrue($process->hasBeenCalled());
}
}

0 comments on commit 403a325

Please sign in to comment.