Skip to content

Commit 5b0c6e0

Browse files
committed
Merge branch '3.9.x' into 4.0.x
* 3.9.x: Remove test logger implementations Remove ConsoleLogger
2 parents 1e374c0 + 261ab35 commit 5b0c6e0

14 files changed

+57
-282
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"doctrine/orm": "^2.13 || ^3",
4141
"doctrine/persistence": "^2 || ^3",
4242
"doctrine/sql-formatter": "^1.0",
43+
"fig/log-test": "^1",
4344
"phpstan/phpstan": "^1.10",
4445
"phpstan/phpstan-deprecation-rules": "^1.1",
4546
"phpstan/phpstan-phpunit": "^1.3",

phpcs.xml.dist

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
</rule>
3939

4040
<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
41-
<exclude-pattern>tests/TestLogger.php</exclude-pattern>
42-
<exclude-pattern>src/Tools/Console/ConsoleLogger.php</exclude-pattern>
41+
<exclude-pattern>tests/LogUtil.php</exclude-pattern>
4342
</rule>
4443

4544
<rule ref="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming">

phpstan.neon.dist

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ parameters:
1717
-
1818
message: '~^Call to function is_bool\(\) with bool will always evaluate to true\.$~'
1919
path: src/InlineParameterFormatter.php
20-
-
21-
message: '~^Call to an undefined method Symfony\\Component\\Console\\Output\\OutputInterface\:\:getErrorOutput\(\)\.$~'
22-
path: src/Tools/Console/ConsoleLogger.php
2320

2421
# https://github.com/phpstan/phpstan/issues/5982
2522
-

src/Tools/Console/Command/DoctrineCommand.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
use Doctrine\Migrations\Configuration\Connection\ConfigurationFile;
88
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback;
99
use Doctrine\Migrations\DependencyFactory;
10-
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
1110
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
1211
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
1312
use Exception;
1413
use Psr\Log\LoggerInterface;
14+
use Psr\Log\LogLevel;
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
18+
use Symfony\Component\Console\Logger\ConsoleLogger;
1819
use Symfony\Component\Console\Output\OutputInterface;
1920
use Symfony\Component\Console\Question\ChoiceQuestion;
2021
use Symfony\Component\Console\Style\StyleInterface;
@@ -104,7 +105,16 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
104105
return;
105106
}
106107

107-
$logger = new ConsoleLogger($output);
108+
$logger = new ConsoleLogger($output, [
109+
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
110+
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
111+
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
112+
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
113+
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
114+
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
115+
LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE,
116+
LogLevel::DEBUG => OutputInterface::VERBOSITY_VERY_VERBOSE,
117+
]);
108118
$dependencyFactory->setService(LoggerInterface::class, $logger);
109119
$dependencyFactory->freeze();
110120
}

src/Tools/Console/ConsoleLogger.php

-133
This file was deleted.

tests/AbstractMigrationTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
use Doctrine\Migrations\Query\Query;
1313
use Doctrine\Migrations\Tests\Stub\AbstractMigrationStub;
1414
use Doctrine\Migrations\Tests\Stub\AbstractMigrationWithoutDownStub;
15+
use Psr\Log\Test\TestLogger;
1516

1617
class AbstractMigrationTest extends MigrationTestCase
1718
{
19+
use LogUtil;
20+
1821
private AbstractMigrationStub $migration;
1922

2023
private TestLogger $logger;
@@ -73,7 +76,7 @@ public function testWarnIfAddDefaultMessage(): void
7376
public function testWarnIfDontOutputMessageIfFalse(): void
7477
{
7578
$this->migration->warnIf(false, 'trallala');
76-
self::assertSame('', $this->getLogOutput($this->logger));
79+
self::assertSame([], $this->logger->records);
7780
}
7881

7982
public function testWriteInvokesOutputWriter(): void

tests/TestLogger.php tests/LogUtil.php

+16-17
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,49 @@
44

55
namespace Doctrine\Migrations\Tests;
66

7-
use DateTime;
87
use DateTimeInterface;
9-
use Psr\Log\AbstractLogger;
8+
use Psr\Log\Test\TestLogger;
109
use Stringable;
1110

11+
use function array_map;
1212
use function gettype;
13+
use function implode;
1314
use function is_object;
1415
use function is_scalar;
1516
use function str_contains;
1617
use function strtr;
1718

18-
class TestLogger extends AbstractLogger
19+
trait LogUtil
1920
{
20-
/** @var string[] */
21-
public array $logs = [];
21+
private function getLogOutput(TestLogger $logger): string
22+
{
23+
return implode("\n", $this->getInterpolatedLogRecords($logger));
24+
}
2225

23-
/**
24-
* {@inheritDoc}
25-
*
26-
* @param mixed[] $context
27-
*/
28-
public function log(mixed $level, string|Stringable $message, array $context = []): void
26+
/** @return list<string> */
27+
private function getInterpolatedLogRecords(TestLogger $logger): array
2928
{
30-
$this->logs[] = $this->interpolate($message, $context);
29+
return array_map($this->interpolate(...), $logger->records);
3130
}
3231

3332
/**
3433
* Interpolates context values into the message placeholders.
3534
*
36-
* @param mixed[] $context
35+
* @param array{level: mixed, message: string|Stringable, context: mixed[]} $record
3736
*/
38-
private function interpolate(string|Stringable $message, array $context): string
37+
private function interpolate(array $record): string
3938
{
40-
$message = (string) $message;
39+
$message = (string) $record['message'];
4140
if (! str_contains($message, '{')) {
4241
return $message;
4342
}
4443

4544
$replacements = [];
46-
foreach ($context as $key => $val) {
45+
foreach ($record['context'] as $key => $val) {
4746
if ($val === null || is_scalar($val) || $val instanceof Stringable) {
4847
$replacements["{{$key}}"] = $val;
4948
} elseif ($val instanceof DateTimeInterface) {
50-
$replacements["{{$key}}"] = $val->format(DateTime::RFC3339);
49+
$replacements["{{$key}}"] = $val->format(DateTimeInterface::RFC3339);
5150
} elseif (is_object($val)) {
5251
$replacements["{{$key}}"] = '[object ' . $val::class . ']';
5352
} else {

tests/Metadata/Storage/DebugLogger.php

-23
This file was deleted.

tests/Metadata/Storage/TableMetadataStorageTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Doctrine\Migrations\Version\ExecutionResult;
2727
use Doctrine\Migrations\Version\Version;
2828
use PHPUnit\Framework\TestCase;
29+
use Psr\Log\Test\TestLogger;
2930

3031
use function sprintf;
3132

@@ -42,7 +43,7 @@ class TableMetadataStorageTest extends TestCase
4243
/** @var AbstractSchemaManager<AbstractPlatform> */
4344
private AbstractSchemaManager $schemaManager;
4445

45-
private DebugLogger $debugLogger;
46+
private TestLogger $testLogger;
4647

4748
private function getSqliteConnection(Configuration|null $configuration = null): Connection
4849
{
@@ -54,8 +55,8 @@ private function getSqliteConnection(Configuration|null $configuration = null):
5455
public function setUp(): void
5556
{
5657
$this->connectionConfig = new Configuration();
57-
$this->debugLogger = new DebugLogger();
58-
$this->connectionConfig->setMiddlewares([new Middleware($this->debugLogger)]);
58+
$this->testLogger = new TestLogger();
59+
$this->connectionConfig->setMiddlewares([new Middleware($this->testLogger)]);
5960
$this->connection = $this->getSqliteConnection($this->connectionConfig);
6061
$this->schemaManager = $this->connection->createSchemaManager();
6162

@@ -67,13 +68,12 @@ public function testSchemaIntrospectionExecutedOnlyOnce(): void
6768
{
6869
$this->storage->ensureInitialized();
6970

70-
$oldQueryCount = $this->debugLogger->count;
71+
$this->testLogger->reset();
7172
$this->storage->ensureInitialized();
72-
self::assertSame(0, $this->debugLogger->count - $oldQueryCount);
73+
self::assertCount(0, $this->testLogger->records);
7374

74-
$oldQueryCount = $this->debugLogger->count;
7575
$this->storage->getExecutedMigrations();
76-
self::assertSame(1, $this->debugLogger->count - $oldQueryCount);
76+
self::assertCount(1, $this->testLogger->records);
7777
}
7878

7979
public function testDifferentTableNotUpdatedOnRead(): void

tests/MigrationTestCase.php

-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use Doctrine\DBAL\DriverManager;
99
use PHPUnit\Framework\TestCase;
1010

11-
use function implode;
12-
1311
abstract class MigrationTestCase extends TestCase
1412
{
1513
public function getSqliteConnection(): Connection
@@ -18,9 +16,4 @@ public function getSqliteConnection(): Connection
1816

1917
return DriverManager::getConnection($params);
2018
}
21-
22-
public function getLogOutput(TestLogger $logger): string
23-
{
24-
return implode("\n", $logger->logs);
25-
}
2619
}

tests/MigratorTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Doctrine\Migrations\Version\Direction;
2424
use Doctrine\Migrations\Version\Version;
2525
use PHPUnit\Framework\MockObject\MockObject;
26+
use Psr\Log\Test\TestLogger;
2627
use Symfony\Component\Console\Output\StreamOutput;
2728
use Symfony\Component\Stopwatch\Stopwatch;
2829
use Throwable;
@@ -79,8 +80,8 @@ public function testEmptyPlanShowsMessage(): void
7980
$planList = new MigrationPlanList([], Direction::UP);
8081
$migrator->migrate($planList, $this->migratorConfiguration);
8182

82-
self::assertCount(1, $this->logger->logs, 'should output the no migrations message');
83-
self::assertStringContainsString('No migrations', $this->logger->logs[0]);
83+
self::assertCount(1, $this->logger->records, 'should output the no migrations message');
84+
self::assertStringContainsString('No migrations', $this->logger->records[0]['message']);
8485
}
8586

8687
protected function createTestMigrator(): DbalMigrator

0 commit comments

Comments
 (0)