Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #71 from roxblnfk/feature/refactoring
Browse files Browse the repository at this point in the history
DBAL::setLogger for all drivers; remove varchar limit; refactoring
  • Loading branch information
wolfy-j authored Mar 29, 2021
2 parents 626b7fc + 97bfa1f commit 04f94c9
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 148 deletions.
19 changes: 18 additions & 1 deletion src/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Container\ContainerExceptionInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Spiral\Core\Container;
use Spiral\Core\FactoryInterface;
Expand Down Expand Up @@ -89,7 +90,9 @@ final class DatabaseManager implements
Container\SingletonInterface,
Container\InjectorInterface
{
use LoggerTrait;
use LoggerTrait {
setLogger as protected internalSetLogger;
}

/** @var DatabaseConfig */
private $config;
Expand All @@ -103,6 +106,20 @@ final class DatabaseManager implements
/** @var DriverInterface[] */
private $drivers = [];

/**
* Set logger for all drivers
*/
public function setLogger(LoggerInterface $logger): void
{
$this->internalSetLogger($logger);
// Assign the logger to all initialized drivers
foreach ($this->drivers as $driver) {
if ($driver instanceof LoggerAwareInterface) {
$driver->setLogger($this->logger);
}
}
}

/**
* @param DatabaseConfig $config
* @param FactoryInterface $factory
Expand Down
6 changes: 0 additions & 6 deletions src/Schema/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,12 +613,6 @@ public function string(int $size = 255): AbstractColumn
{
$this->type('string');

if ($size > 255) {
throw new SchemaException(
'String size can\'t exceed 255 characters. Use text instead'
);
}

if ($size < 0) {
throw new SchemaException(
'Invalid string length value'
Expand Down
19 changes: 7 additions & 12 deletions tests/Database/AlterColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testAddColumnWithDefaultValue(): void
$schema->string('new_column')->defaultValue('some_value');
$schema->save();

$this->assertInternalType('array', $schema->string('new_column')->__debugInfo());
$this->assertIsArray($schema->string('new_column')->__debugInfo());

$this->assertSameAsInDB($schema);
}
Expand Down Expand Up @@ -137,28 +137,25 @@ public function testMakeNullable(): void
$this->assertTrue($this->fetchSchema($schema)->column('first_name')->isNullable());
}

/**
* @expectedException \Spiral\Database\Exception\SchemaException
*/
public function testColumnSizeException(): void
{
$this->expectException(\Spiral\Database\Exception\SchemaException::class);
$schema = $this->sampleSchema('table');
$this->assertTrue($schema->exists());

$schema->string('first_name', -1);
$schema->save();
}

/**
* @expectedException \Spiral\Database\Exception\SchemaException
*/
public function testColumnSize2Exception(): void
{
$schema = $this->sampleSchema('table');
$this->assertTrue($schema->exists());

$schema->string('first_name', 256);
$schema->save();

// No limit error
$this->assertTrue(true);
}

public function testChangeSize(): void
Expand Down Expand Up @@ -187,14 +184,12 @@ public function testDecimalSizes(): void
$this->assertSame(10, $this->fetchSchema($schema)->column('double_2')->getPrecision());
$this->assertSame(1, $this->fetchSchema($schema)->column('double_2')->getScale());

$this->assertInternalType('array', $schema->decimal('double_2', 10, 1)->__debugInfo());
$this->assertIsArray($schema->decimal('double_2', 10, 1)->__debugInfo());
}

/**
* @expectedException \Spiral\Database\Exception\SchemaException
*/
public function testDecimalSizesException(): void
{
$this->expectException(\Spiral\Database\Exception\SchemaException::class);
$schema = $this->sampleSchema('table');
$this->assertTrue($schema->exists());

Expand Down
16 changes: 8 additions & 8 deletions tests/Database/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

declare(strict_types=1);

namespace Spiral\tests\Cases\Database;
namespace Spiral\Database\tests;

use PHPUnit\Framework\TestCase;
use Spiral\Core\Container\Autowire;
use Spiral\Database\Config\DatabaseConfig;
use Spiral\Database\Exception\ConfigException;

class ConfigTest extends TestCase
{
Expand Down Expand Up @@ -44,9 +45,6 @@ public function testHasDatabase(): void
$this->assertFalse($config->hasDatabase('database-1'));
}

/**
* @expectedException \Spiral\Database\Exception\ConfigException
*/
public function testDatabaseException(): void
{
$config = new DatabaseConfig(
Expand All @@ -58,7 +56,10 @@ public function testDatabaseException(): void
]
]
);
$this->assertSame('test3', $config->getDatabase('test3'));

$this->expectException(ConfigException::class);

$config->getDatabase('test3');
}

public function testDatabaseDriver(): void
Expand Down Expand Up @@ -215,9 +216,6 @@ public function testHasDriver(): void
$this->assertFalse($config->hasDriver('database-1'));
}

/**
* @expectedException \Spiral\Database\Exception\ConfigException
*/
public function testDriverException(): void
{
$config = new DatabaseConfig(
Expand All @@ -226,6 +224,8 @@ public function testDriverException(): void
]
);

$this->expectException(ConfigException::class);

$config->getDriver('test3');
}

Expand Down
9 changes: 5 additions & 4 deletions tests/Database/CreateTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Spiral\Database\Tests;

use Spiral\Database\Database;
use Spiral\Database\Exception\SchemaException;
use Spiral\Database\Schema\AbstractTable;

abstract class CreateTableTest extends BaseTest
Expand Down Expand Up @@ -58,7 +59,7 @@ public function testSimpleCreation(): void
$this->assertTrue($schema->exists());
$this->assertSameAsInDB($schema);

$this->assertInternalType('array', $schema->__debugInfo());
$this->assertIsArray($schema->__debugInfo());
}

public function testMultipleColumns(): void
Expand Down Expand Up @@ -133,13 +134,13 @@ public function testCreateWithPrimary(): void
$this->assertSame(['id'], $this->fetchSchema($schema)->getPrimaryKeys());
}

/**
* @expectedException \Spiral\Database\Exception\SchemaException
*/
public function testDeleteNonExisted(): void
{
$schema = $this->schema('table');
$this->assertFalse($schema->exists());

$this->expectException(SchemaException::class);

$schema->declareDropped();
}
}
11 changes: 6 additions & 5 deletions tests/Database/Driver/MySQL/DatetimeColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Spiral\Database\Tests\Driver\MySQL;

use Spiral\Database\Exception\HandlerException;

/**
* @group driver
* @group driver-mysql
Expand All @@ -19,13 +21,12 @@ class DatetimeColumnTest extends \Spiral\Database\Tests\DatetimeColumnTest
{
public const DRIVER = 'mysql';

/**
* @expectedException \Spiral\Database\Exception\HandlerException
* @expectedExceptionMessage SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid
* default value for 'target'
*/
public function testTimestampDatetimeZero(): void
{
$this->expectExceptionMessage(
"SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'target'"
);
$this->expectException(HandlerException::class);
parent::testTimestampDatetimeZero();
}
}
14 changes: 4 additions & 10 deletions tests/Database/Driver/MySQL/DefaultValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@ class DefaultValueTest extends \Spiral\Database\Tests\DefaultValueTest
{
public const DRIVER = 'mysql';

/**
* @expectedException \Spiral\Database\Driver\MySQL\Exception\MySQLException
* @expectedExceptionMessage Column table.target of type text/blob can not have non empty
* default value
*/
public function testTextDefaultValueString(): void
{
$this->expectException(\Spiral\Database\Driver\MySQL\Exception\MySQLException::class);
$this->expectExceptionMessage("Column table.target of type text/blob can not have non empty default value");
parent::testTextDefaultValueString();
}

/**
* @expectedException \Spiral\Database\Driver\MySQL\Exception\MySQLException
* @expectedExceptionMessage Column table.target of type text/blob can not have non empty
* default value
*/
public function testTextDefaultValueEmpty(): void
{
$this->expectException(\Spiral\Database\Driver\MySQL\Exception\MySQLException::class);
$this->expectExceptionMessage("Column table.target of type text/blob can not have non empty default value");
parent::testTextDefaultValueEmpty();
}
}
9 changes: 3 additions & 6 deletions tests/Database/Driver/Postgres/ConsistencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,13 @@ public function testPrimary(): void
$this->assertSame('target', $d->getPrimaryKey('', 'table'));
}

/**
* @expectedException \Spiral\Database\Exception\DriverException
*/
public function testPrimaryException(): void
{
/**
* @var PostgresDriver $d
*/
/** @var PostgresDriver $d */
$d = $this->getDriver();

$this->expectException(\Spiral\Database\Exception\DriverException::class);

$this->assertSame('target', $d->getPrimaryKey('', 'table'));
}

Expand Down
Loading

0 comments on commit 04f94c9

Please sign in to comment.