Skip to content

Commit

Permalink
Add debug logging of files being analysed (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Feb 14, 2023
1 parent 3f98e8f commit 14d6c5e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ protected function scanSources(iterable $sources, Analysis $analysis, Context $r
if (is_dir($resolvedSource)) {
$this->scanSources(Util::finder($resolvedSource), $analysis, $rootContext);
} else {
$rootContext->logger->debug(sprintf('Analysing source: %s', $resolvedSource));
$analysis->addAnalysis($analyser->fromFile($resolvedSource, $rootContext));
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Loggers/ConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public function log($level, $message, array $context = []): void
if (!$this->debug) {
return;
}
$prefix = 'Debug: ';
// no break
case LogLevel::WARNING:
$prefix = $context['prefix'] ?? 'Warning: ';
$prefix = $prefix ?: ($context['prefix'] ?? 'Warning: ');
$color = static::COLOR_WARNING;
break;
case LogLevel::ERROR:
Expand Down
22 changes: 19 additions & 3 deletions tests/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,104 +24,119 @@ public function exampleDetails(): iterable
OA\OpenApi::VERSION_3_0_0,
'example-object',
'example-object.yaml',
false,
[],
];

yield 'misc' => [
OA\OpenApi::VERSION_3_0_0,
'misc',
'misc.yaml',
false,
[],
];

yield 'nesting' => [
OA\OpenApi::VERSION_3_0_0,
'nesting',
'nesting.yaml',
false,
[],
];

yield 'petstore-3.0' => [
OA\OpenApi::VERSION_3_0_0,
'petstore-3.0',
'petstore-3.0.yaml',
false,
[],
];

yield 'petstore.swagger.io' => [
OA\OpenApi::VERSION_3_0_0,
'petstore.swagger.io',
'petstore.swagger.io.yaml',
false,
[],
];

yield 'swagger-spec/petstore' => [
OA\OpenApi::VERSION_3_0_0,
'swagger-spec/petstore',
'petstore.yaml',
false,
[],
];

yield 'swagger-spec/petstore-simple' => [
OA\OpenApi::VERSION_3_0_0,
'swagger-spec/petstore-simple',
'petstore-simple.yaml',
false,
[],
];

yield 'swagger-spec/petstore-simple-3.1.0' => [
OA\OpenApi::VERSION_3_1_0,
'swagger-spec/petstore-simple',
'petstore-simple-3.1.0.yaml',
false,
[],
];

yield 'swagger-spec/petstore-with-external-docs' => [
OA\OpenApi::VERSION_3_0_0,
'swagger-spec/petstore-with-external-docs',
'petstore-with-external-docs.yaml',
false,
[],
];

yield 'polymorphism' => [
OA\OpenApi::VERSION_3_0_0,
'polymorphism',
'polymorphism.yaml',
false,
[],
];

yield 'polymorphism-3.1.0' => [
OA\OpenApi::VERSION_3_1_0,
'polymorphism',
'polymorphism-3.1.0.yaml',
false,
[],
];

yield 'using-interfaces' => [
OA\OpenApi::VERSION_3_0_0,
'using-interfaces',
'using-interfaces.yaml',
false,
[],
];

yield 'using-refs' => [
OA\OpenApi::VERSION_3_0_0,
'using-refs',
'using-refs.yaml',
false,
[],
];

yield 'using-traits' => [
OA\OpenApi::VERSION_3_0_0,
'using-traits',
'using-traits.yaml',
false,
[],
];

yield 'using-links' => [
OA\OpenApi::VERSION_3_0_0,
'using-links',
'using-links.yaml',
false,
[],
];

Expand All @@ -130,6 +145,7 @@ public function exampleDetails(): iterable
OA\OpenApi::VERSION_3_0_0,
'using-links-php81',
'using-links-php81.yaml',
true,
['JetBrains\PhpStorm\ArrayShape'],
];
}
Expand Down Expand Up @@ -163,7 +179,7 @@ public function exampleMappings(): iterable
*
* @dataProvider exampleMappings
*/
public function testExamples(string $version, string $example, string $spec, array $expectedLog, AnalyserInterface $analyser): void
public function testExamples(string $version, string $example, string $spec, bool $debug, array $expectedLog, AnalyserInterface $analyser): void
{
// register autoloader for examples that require autoloading due to inheritance, etc.
$path = $this->example($example);
Expand All @@ -178,7 +194,7 @@ public function testExamples(string $version, string $example, string $spec, arr
}

$path = $this->example($example);
$openapi = (new Generator($this->getTrackingLogger()))
$openapi = (new Generator($this->getTrackingLogger($debug)))
->setVersion($version)
->setAnalyser($analyser)
->generate([$path]);
Expand All @@ -193,7 +209,7 @@ public function testExamples(string $version, string $example, string $spec, arr
/**
* @dataProvider exampleDetails
*/
public function testSerializer(string $version, string $example, string $spec, array $expectedLog): void
public function testSerializer(string $version, string $example, string $spec, bool $debug, array $expectedLog): void
{
$filename = $this->example($example) . '/' . $spec;
$reserialized = (new Serializer())->deserializeFile($filename)->toYaml();
Expand Down
16 changes: 13 additions & 3 deletions tests/OpenApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -51,19 +52,28 @@ protected function tearDown(): void
parent::tearDown();
}

public function getTrackingLogger(): ?LoggerInterface
public function getTrackingLogger(bool $debug = false): ?LoggerInterface
{
return new class($this) extends AbstractLogger {
return new class($this, $debug) extends AbstractLogger {
/** @var OpenApiTestCase */
protected $testCase;

public function __construct(OpenApiTestCase $testCase)
protected $debug;

public function __construct(OpenApiTestCase $testCase, bool $debug = false)
{
$this->testCase = $testCase;
$this->debug = $debug;
}

public function log($level, $message, array $context = []): void
{
if (LogLevel::DEBUG == $level) {
if (!$this->debug || 0 === strpos($message, 'Analysing source:')) {
return;
}
}

if (count($this->testCase->expectedLogMessages)) {
list($assertion, $needle) = array_shift($this->testCase->expectedLogMessages);
$assertion($message, $level);
Expand Down

0 comments on commit 14d6c5e

Please sign in to comment.