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

Merge 3.9.x into 4.0.x #1451

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"doctrine/orm": "^2.13 || ^3",
"doctrine/persistence": "^2 || ^3",
"doctrine/sql-formatter": "^1.0",
"fig/log-test": "^1",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
Expand Down
3 changes: 1 addition & 2 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
</rule>

<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
<exclude-pattern>tests/TestLogger.php</exclude-pattern>
<exclude-pattern>src/Tools/Console/ConsoleLogger.php</exclude-pattern>
<exclude-pattern>tests/LogUtil.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming">
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ parameters:
-
message: '~^Call to function is_bool\(\) with bool will always evaluate to true\.$~'
path: src/InlineParameterFormatter.php
-
message: '~^Call to an undefined method Symfony\\Component\\Console\\Output\\OutputInterface\:\:getErrorOutput\(\)\.$~'
path: src/Tools/Console/ConsoleLogger.php

# https://github.com/phpstan/phpstan/issues/5982
-
Expand Down
14 changes: 12 additions & 2 deletions src/Tools/Console/Command/DoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Doctrine\Migrations\Configuration\Connection\ConfigurationFile;
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\StyleInterface;
Expand Down Expand Up @@ -104,7 +105,16 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
return;
}

$logger = new ConsoleLogger($output);
$logger = new ConsoleLogger($output, [
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE,
LogLevel::DEBUG => OutputInterface::VERBOSITY_VERY_VERBOSE,
]);
$dependencyFactory->setService(LoggerInterface::class, $logger);
$dependencyFactory->freeze();
}
Expand Down
133 changes: 0 additions & 133 deletions src/Tools/Console/ConsoleLogger.php

This file was deleted.

5 changes: 4 additions & 1 deletion tests/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
use Doctrine\Migrations\Query\Query;
use Doctrine\Migrations\Tests\Stub\AbstractMigrationStub;
use Doctrine\Migrations\Tests\Stub\AbstractMigrationWithoutDownStub;
use Psr\Log\Test\TestLogger;

class AbstractMigrationTest extends MigrationTestCase
{
use LogUtil;

private AbstractMigrationStub $migration;

private TestLogger $logger;
Expand Down Expand Up @@ -73,7 +76,7 @@ public function testWarnIfAddDefaultMessage(): void
public function testWarnIfDontOutputMessageIfFalse(): void
{
$this->migration->warnIf(false, 'trallala');
self::assertSame('', $this->getLogOutput($this->logger));
self::assertSame([], $this->logger->records);
}

public function testWriteInvokesOutputWriter(): void
Expand Down
33 changes: 16 additions & 17 deletions tests/TestLogger.php → tests/LogUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,49 @@

namespace Doctrine\Migrations\Tests;

use DateTime;
use DateTimeInterface;
use Psr\Log\AbstractLogger;
use Psr\Log\Test\TestLogger;
use Stringable;

use function array_map;
use function gettype;
use function implode;
use function is_object;
use function is_scalar;
use function str_contains;
use function strtr;

class TestLogger extends AbstractLogger
trait LogUtil
{
/** @var string[] */
public array $logs = [];
private function getLogOutput(TestLogger $logger): string
{
return implode("\n", $this->getInterpolatedLogRecords($logger));
}

/**
* {@inheritDoc}
*
* @param mixed[] $context
*/
public function log(mixed $level, string|Stringable $message, array $context = []): void
/** @return list<string> */
private function getInterpolatedLogRecords(TestLogger $logger): array
{
$this->logs[] = $this->interpolate($message, $context);
return array_map($this->interpolate(...), $logger->records);
}

/**
* Interpolates context values into the message placeholders.
*
* @param mixed[] $context
* @param array{level: mixed, message: string|Stringable, context: mixed[]} $record
*/
private function interpolate(string|Stringable $message, array $context): string
private function interpolate(array $record): string
{
$message = (string) $message;
$message = (string) $record['message'];
if (! str_contains($message, '{')) {
return $message;
}

$replacements = [];
foreach ($context as $key => $val) {
foreach ($record['context'] as $key => $val) {
if ($val === null || is_scalar($val) || $val instanceof Stringable) {
$replacements["{{$key}}"] = $val;
} elseif ($val instanceof DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(DateTime::RFC3339);
$replacements["{{$key}}"] = $val->format(DateTimeInterface::RFC3339);
} elseif (is_object($val)) {
$replacements["{{$key}}"] = '[object ' . $val::class . ']';
} else {
Expand Down
23 changes: 0 additions & 23 deletions tests/Metadata/Storage/DebugLogger.php

This file was deleted.

14 changes: 7 additions & 7 deletions tests/Metadata/Storage/TableMetadataStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Doctrine\Migrations\Version\ExecutionResult;
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\TestCase;
use Psr\Log\Test\TestLogger;

use function sprintf;

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

private DebugLogger $debugLogger;
private TestLogger $testLogger;

private function getSqliteConnection(Configuration|null $configuration = null): Connection
{
Expand All @@ -54,8 +55,8 @@ private function getSqliteConnection(Configuration|null $configuration = null):
public function setUp(): void
{
$this->connectionConfig = new Configuration();
$this->debugLogger = new DebugLogger();
$this->connectionConfig->setMiddlewares([new Middleware($this->debugLogger)]);
$this->testLogger = new TestLogger();
$this->connectionConfig->setMiddlewares([new Middleware($this->testLogger)]);
$this->connection = $this->getSqliteConnection($this->connectionConfig);
$this->schemaManager = $this->connection->createSchemaManager();

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

$oldQueryCount = $this->debugLogger->count;
$this->testLogger->reset();
$this->storage->ensureInitialized();
self::assertSame(0, $this->debugLogger->count - $oldQueryCount);
self::assertCount(0, $this->testLogger->records);

$oldQueryCount = $this->debugLogger->count;
$this->storage->getExecutedMigrations();
self::assertSame(1, $this->debugLogger->count - $oldQueryCount);
self::assertCount(1, $this->testLogger->records);
}

public function testDifferentTableNotUpdatedOnRead(): void
Expand Down
7 changes: 0 additions & 7 deletions tests/MigrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\TestCase;

use function implode;

abstract class MigrationTestCase extends TestCase
{
public function getSqliteConnection(): Connection
Expand All @@ -18,9 +16,4 @@ public function getSqliteConnection(): Connection

return DriverManager::getConnection($params);
}

public function getLogOutput(TestLogger $logger): string
{
return implode("\n", $logger->logs);
}
}
5 changes: 3 additions & 2 deletions tests/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\Migrations\Version\Direction;
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\Test\TestLogger;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Stopwatch\Stopwatch;
use Throwable;
Expand Down Expand Up @@ -79,8 +80,8 @@ public function testEmptyPlanShowsMessage(): void
$planList = new MigrationPlanList([], Direction::UP);
$migrator->migrate($planList, $this->migratorConfiguration);

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

protected function createTestMigrator(): DbalMigrator
Expand Down
Loading