Skip to content

Commit

Permalink
Separate chain calls (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sankaest committed May 31, 2022
1 parent c27bcae commit b2dbe25
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 38 deletions.
18 changes: 11 additions & 7 deletions src/DbTarget.php
Expand Up @@ -70,7 +70,9 @@ protected function export(): void
{
$defaultLogTime = microtime(true);
$formattedMessages = $this->getFormattedMessages();
$table = $this->db->getSchema()->quoteTableName($this->table);
$table = $this->db
->getSchema()
->quoteTableName($this->table);

$sql = "INSERT INTO {$table} ([[level]], [[category]], [[log_time]], [[message]])"
. ' VALUES (:level, :category, :log_time, :message)';
Expand All @@ -79,12 +81,14 @@ protected function export(): void
$command = $this->db->createCommand($sql);

foreach ($this->getMessages() as $key => $message) {
if ($command->bindValues([
':level' => $message->level(),
':category' => $message->context('category', ''),
':log_time' => $message->context('time', $defaultLogTime),
':message' => $formattedMessages[$key],
])->execute() > 0) {
if ($command
->bindValues([
':level' => $message->level(),
':category' => $message->context('category', ''),
':log_time' => $message->context('time', $defaultLogTime),
':message' => $formattedMessages[$key],
])
->execute() > 0) {
continue;
}
throw new RuntimeException(sprintf(
Expand Down
4 changes: 3 additions & 1 deletion src/Migration/M202101052207CreateLog.php
Expand Up @@ -36,7 +36,9 @@ public function __construct(LoggerInterface $logger, MigrationInformerInterface

foreach ($logger->getTargets() as $target) {
if ($target instanceof DbTarget) {
$this->targets[md5($target->getDb()->getDsn() . ':' . $target->getTable())] = $target;
$this->targets[md5($target
->getDb()
->getDsn() . ':' . $target->getTable())] = $target;
}
}

Expand Down
63 changes: 45 additions & 18 deletions tests/DbTargetTest.php
Expand Up @@ -25,33 +25,45 @@ public function setUp(): void
parent::setUp();

$migration = new M202101052207CreateLog(
$this->getContainer()->get(LoggerInterface::class),
$this->getContainer()->get(MigrationInformerInterface::class),
$this
->getContainer()
->get(LoggerInterface::class),
$this
->getContainer()
->get(MigrationInformerInterface::class),
);

$migration->up($this->getContainer()->get(MigrationBuilder::class));
$migration->up($this
->getContainer()
->get(MigrationBuilder::class));
}

public function testGetters(): void
{
$target = $this->createDbTarget();

$this->assertSame('log', $target->getTable());
$this->assertSame($this->getContainer()->get(ConnectionInterface::class), $target->getDb());
$this->assertSame($this
->getContainer()
->get(ConnectionInterface::class), $target->getDb());
}

public function testExport(): void
{
$time = microtime(true);

$this->createDbTarget(null, 'test-table-1')->collect([
new Message(LogLevel::INFO, 'Message', ['time' => $time, 'category' => 'application']),
], true);
$this
->createDbTarget(null, 'test-table-1')
->collect([
new Message(LogLevel::INFO, 'Message', ['time' => $time, 'category' => 'application']),
], true);

$this->createDbTarget(null, 'test-table-2')->collect([
new Message(LogLevel::ALERT, 'Message-1', ['time' => $time, 'category' => 'app']),
new Message(LogLevel::ERROR, 'Message-2', ['time' => $time, 'foo' => 'bar']),
], true);
$this
->createDbTarget(null, 'test-table-2')
->collect([
new Message(LogLevel::ALERT, 'Message-1', ['time' => $time, 'category' => 'app']),
new Message(LogLevel::ERROR, 'Message-2', ['time' => $time, 'foo' => 'bar']),
], true);

$this->assertSame(
[
Expand Down Expand Up @@ -89,36 +101,51 @@ public function testExport(): void

public function testExportWithEmptyMessages(): void
{
$this->createDbTarget(null, 'test-table-1')->collect([], true);
$this
->createDbTarget(null, 'test-table-1')
->collect([], true);

$this->assertSame([], $this->findData('test-table-1'));
}

public function testExportWithStoreFailure(): void
{
$command = $this->getMockBuilder(Command::class)
$command = $this
->getMockBuilder(Command::class)
->onlyMethods(['execute'])
->disableOriginalConstructor()
->getMockForAbstractClass()
;
$command->method('execute')->willReturn(0);
$command
->method('execute')
->willReturn(0);

$db = $this->createMock(ConnectionInterface::class);
$db->method('createCommand')->willReturn($command);
$db
->method('createCommand')
->willReturn($command);

$this->expectException(RuntimeException::class);
$this->createDbTarget($db)->collect([new Message(LogLevel::INFO, 'Message')], true);
$this
->createDbTarget($db)
->collect([new Message(LogLevel::INFO, 'Message')], true);
}

private function createDbTarget(ConnectionInterface $db = null, string $table = 'log'): DbTarget
{
$target = new DbTarget($db ?? $this->getContainer()->get(ConnectionInterface::class), $table);
$target = new DbTarget($db ?? $this
->getContainer()
->get(ConnectionInterface::class), $table);
$target->setFormat(fn (Message $message) => "[{$message->level()}] {$message->message()}");
return $target;
}

private function findData(string $table): array
{
return (new Query($this->getContainer()->get(ConnectionInterface::class)))->from($table)->all();
return (new Query($this
->getContainer()
->get(ConnectionInterface::class)))
->from($table)
->all();
}
}
30 changes: 22 additions & 8 deletions tests/MigrationTest.php
Expand Up @@ -23,23 +23,31 @@ public function setUp(): void
{
parent::setUp();

$this->migrationInformer = $this->getContainer()->get(MigrationInformerInterface::class);
$this->migrationInformer = $this
->getContainer()
->get(MigrationInformerInterface::class);
}

public function testUpAndDown(): void
{
$migration = new M202101052207CreateLog(
$this->getContainer()->get(LoggerInterface::class),
$this
->getContainer()
->get(LoggerInterface::class),
$this->migrationInformer,
);

$migration->up($this->getContainer()->get(MigrationBuilder::class));
$migration->up($this
->getContainer()
->get(MigrationBuilder::class));

$this->assertTrue($this->tableExists('test-table-1'));
$this->assertTrue($this->tableExists('test-table-2'));
$this->assertFalse($this->tableExists('table-not-exist'));

$migration->down($this->getContainer()->get(MigrationBuilder::class));
$migration->down($this
->getContainer()
->get(MigrationBuilder::class));

$this->assertFalse($this->tableExists('test-table-1'));
$this->assertFalse($this->tableExists('test-table-2'));
Expand All @@ -49,10 +57,14 @@ public function testUpAndDown(): void
public function testUpWithCheckingEquivalenceOfConnectionInstance(): void
{
/** @var Logger $logger */
$logger = $this->getContainer()->get(LoggerInterface::class);
$logger = $this
->getContainer()
->get(LoggerInterface::class);

$migration = new M202101052207CreateLog($logger, $this->migrationInformer);
$migration->up($this->getContainer()->get(MigrationBuilder::class));
$migration->up($this
->getContainer()
->get(MigrationBuilder::class));

/** @var DbTarget[] $targets */
$targets = $logger->getTargets();
Expand All @@ -78,9 +90,11 @@ public function log($level, $message, array $context = []): void

private function tableExists(string $table): bool
{
return (bool) $this->getContainer()->get(ConnectionInterface::class)
return (bool) $this
->getContainer()
->get(ConnectionInterface::class)
->createCommand("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='{$table}'")
->queryScalar()
;
;
}
}
15 changes: 11 additions & 4 deletions tests/TestCase.php
Expand Up @@ -37,10 +37,17 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase

protected function tearDown(): void
{
$db = $this->getContainer()->get(ConnectionInterface::class);

foreach ($db->getSchema()->getTableNames() as $tableName) {
$db->createCommand()->dropTable($tableName)->execute();
$db = $this
->getContainer()
->get(ConnectionInterface::class);

foreach ($db
->getSchema()
->getTableNames() as $tableName) {
$db
->createCommand()
->dropTable($tableName)
->execute();
}

unset($this->container);
Expand Down

0 comments on commit b2dbe25

Please sign in to comment.