From b6e2d28b90b4ab9369cd6dd300e16172fb2a2b55 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 26 Mar 2021 18:14:47 +0300 Subject: [PATCH 1/4] Fix deprecation warnings in all test files --- tests/Database/AlterColumnTest.php | 12 +-- tests/Database/ConfigTest.php | 16 ++-- tests/Database/CreateTableTest.php | 9 ++- .../Driver/MySQL/DatetimeColumnTest.php | 11 +-- .../Driver/MySQL/DefaultValueTest.php | 14 +--- .../Driver/Postgres/ConsistencyTest.php | 9 +-- tests/Database/ManagerTest.php | 54 +++++-------- tests/Database/SelectQueryTest.php | 75 ++++++------------- tests/Database/StatementTest.php | 11 +-- tests/Database/TableTest.php | 3 +- 10 files changed, 77 insertions(+), 137 deletions(-) diff --git a/tests/Database/AlterColumnTest.php b/tests/Database/AlterColumnTest.php index 0572948d..0d90cfa3 100644 --- a/tests/Database/AlterColumnTest.php +++ b/tests/Database/AlterColumnTest.php @@ -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); } @@ -137,11 +137,9 @@ 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()); @@ -187,14 +185,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()); diff --git a/tests/Database/ConfigTest.php b/tests/Database/ConfigTest.php index 7336be04..e4b5f676 100644 --- a/tests/Database/ConfigTest.php +++ b/tests/Database/ConfigTest.php @@ -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 { @@ -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( @@ -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 @@ -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( @@ -226,6 +224,8 @@ public function testDriverException(): void ] ); + $this->expectException(ConfigException::class); + $config->getDriver('test3'); } diff --git a/tests/Database/CreateTableTest.php b/tests/Database/CreateTableTest.php index 8a42e473..8a1c0996 100644 --- a/tests/Database/CreateTableTest.php +++ b/tests/Database/CreateTableTest.php @@ -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 @@ -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 @@ -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(); } } diff --git a/tests/Database/Driver/MySQL/DatetimeColumnTest.php b/tests/Database/Driver/MySQL/DatetimeColumnTest.php index 1e5088bc..023e28cf 100644 --- a/tests/Database/Driver/MySQL/DatetimeColumnTest.php +++ b/tests/Database/Driver/MySQL/DatetimeColumnTest.php @@ -11,6 +11,8 @@ namespace Spiral\Database\Tests\Driver\MySQL; +use Spiral\Database\Exception\HandlerException; + /** * @group driver * @group driver-mysql @@ -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(); } } diff --git a/tests/Database/Driver/MySQL/DefaultValueTest.php b/tests/Database/Driver/MySQL/DefaultValueTest.php index 510b4f1e..0f9fcf3d 100644 --- a/tests/Database/Driver/MySQL/DefaultValueTest.php +++ b/tests/Database/Driver/MySQL/DefaultValueTest.php @@ -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(); } } diff --git a/tests/Database/Driver/Postgres/ConsistencyTest.php b/tests/Database/Driver/Postgres/ConsistencyTest.php index 5e148fa6..e70a6aa4 100644 --- a/tests/Database/Driver/Postgres/ConsistencyTest.php +++ b/tests/Database/Driver/Postgres/ConsistencyTest.php @@ -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')); } diff --git a/tests/Database/ManagerTest.php b/tests/Database/ManagerTest.php index 7f5c2975..f0defaf7 100644 --- a/tests/Database/ManagerTest.php +++ b/tests/Database/ManagerTest.php @@ -18,6 +18,7 @@ use Spiral\Database\DatabaseManager; use Spiral\Database\Driver\DriverInterface; use Spiral\Database\Driver\SQLite\SQLiteDriver; +use Spiral\Database\Exception\DBALException; class ManagerTest extends TestCase { @@ -45,17 +46,15 @@ public function testAddDatabase(): void $this->assertSame($db, $dbal->database('default')); } - /** - * @expectedException \Spiral\Database\Exception\DBALException - */ public function testAddDatabaseException(): void { $driver = m::mock(DriverInterface::class); $db = new Database('default', '', $driver); - - $dbal = new DatabaseManager(new DatabaseConfig(self::DEFAULT_OPTIONS)); $dbal->addDatabase($db); + + $this->expectException(DBALException::class); + $dbal->addDatabase($db); } @@ -69,24 +68,21 @@ public function testAddDriver(): void $this->assertSame($driver, $dbal->driver('default')); } - /** - * @expectedException \Spiral\Database\Exception\DBALException - */ public function testAddDriverException(): void { $driver = m::mock(DriverInterface::class); - $dbal = new DatabaseManager(new DatabaseConfig(self::DEFAULT_OPTIONS)); $dbal->addDriver('default', $driver); + + $this->expectException(DBALException::class); + $dbal->addDriver('default', $driver); } - /** - * @expectedException \Spiral\Database\Exception\DBALException - */ public function testDatabaseException(): void { $dbal = new DatabaseManager(new DatabaseConfig(self::DEFAULT_OPTIONS)); + $this->expectException(DBALException::class); $dbal->database('default'); } @@ -147,27 +143,21 @@ public function testGetDatabases(): void $this->assertCount(1, $dbal->getDatabases()); } - /** - * @expectedException \Spiral\Database\Exception\DBALException - */ public function testGetDatabaseException(): void { - $read = m::mock(DriverInterface::class); - $write = m::mock(DriverInterface::class); - $dbal = new DatabaseManager(new DatabaseConfig(self::DEFAULT_OPTIONS)); + + $this->expectException(DBALException::class); + $dbal->database('other'); } - /** - * @expectedException \Spiral\Database\Exception\DBALException - */ public function testGetDriverException(): void { - $read = m::mock(DriverInterface::class); - $write = m::mock(DriverInterface::class); - $dbal = new DatabaseManager(new DatabaseConfig(self::DEFAULT_OPTIONS)); + + $this->expectException(DBALException::class); + $dbal->driver('other'); } @@ -269,22 +259,18 @@ public function testCountDatabase(): void $this->assertCount(3, $dbal->getDatabases()); } - /** - * @expectedException \Spiral\Database\Exception\DBALException - */ public function testBadDriver(): void { $dbal = new DatabaseManager( - new DatabaseConfig( - [ - - 'connections' => [ - 'default' => new Container\Autowire('unknown') - ] + new DatabaseConfig([ + 'connections' => [ + 'default' => new Container\Autowire('unknown') ] - ) + ]) ); + $this->expectException(DBALException::class); + $dbal->driver('default'); } } diff --git a/tests/Database/SelectQueryTest.php b/tests/Database/SelectQueryTest.php index a9df70b6..7a100983 100644 --- a/tests/Database/SelectQueryTest.php +++ b/tests/Database/SelectQueryTest.php @@ -11,6 +11,7 @@ namespace Spiral\Database\Tests; +use Spiral\Database\Exception\BuilderException; use Spiral\Database\Injection\Expression; use Spiral\Database\Injection\Fragment; use Spiral\Database\Injection\Parameter; @@ -294,19 +295,13 @@ public function testSelectWithWhereWithNotBetween(): void ); } - /** - * @expectedException \Spiral\Database\Exception\BuilderException - * @expectedExceptionMessage Between statements expects exactly 2 values - */ public function testSelectWithWhereBetweenBadValue(): void { + $this->expectExceptionMessage("Between statements expects exactly 2 values"); + $this->expectException(BuilderException::class); + $select = $this->database->select()->distinct()->from(['users']) ->where('balance', 'BETWEEN', 0); - - $this->assertSameQuery( - 'SELECT DISTINCT * FROM {users} WHERE {balance} NOT BETWEEN ? AND ?', - $select - ); } public function testSelectWithFullySpecificColumnNameInWhere(): void @@ -386,20 +381,14 @@ public function testSelectWithWhereOrWhere(): void ); } - /** - * @expectedException \Spiral\Database\Exception\BuilderException - */ public function testSelectInvalidArrayArgument(): void { + $this->expectException(BuilderException::class); + $select = $this->database->select()->distinct() ->from(['users']) ->where('name', 'Anton') ->orWhere('id', 'in', [1, 2, 3]); - - $this->assertSameQuery( - 'SELECT DISTINCT * FROM {users} WHERE {name} = ? OR {balance} > ?', - $select - ); } public function testSelectWithWhereOrWhereAndWhere(): void @@ -572,12 +561,11 @@ public function testShortWhereWithNotBetweenCondition(): void ); } - /** - * @expectedException \Spiral\Database\Exception\BuilderException - * @expectedExceptionMessage Exactly 2 array values are required for between statement - */ public function testShortWhereWithBetweenConditionBadArguments(): void { + $this->expectException(BuilderException::class); + $this->expectExceptionMessage("Exactly 2 array values are required for between statement"); + $select = $this->database ->select() ->from(['users']) @@ -590,7 +578,6 @@ public function testShortWhereWithBetweenConditionBadArguments(): void ); } - public function testShortWhereMultiple(): void { $select = $this->database @@ -722,12 +709,11 @@ public function testOrShortWhereAND(): void ); } - /** - * @expectedException \Spiral\Database\Exception\BuilderException - * @expectedExceptionMessage Nested conditions should have defined operator - */ public function testBadShortExpression(): void { + $this->expectException(BuilderException::class); + $this->expectExceptionMessage("Nested conditions should have defined operator"); + $select = $this->database ->select() ->from(['users']) @@ -736,11 +722,6 @@ public function testBadShortExpression(): void 'status' => ['active', 'blocked'] ] ); - - $this->assertSameQuery( - 'SELECT * FROM {users} WHERE {balance} = ?', - $select - ); } //Order By @@ -1236,19 +1217,12 @@ public function testHavingSelectWithHavingWithNotBetween(): void ); } - /** - * @expectedException \Spiral\Database\Exception\BuilderException - * @expectedExceptionMessage Between statements expects exactly 2 values - */ public function testHavingSelectWithHavingBetweenBadValue(): void { + $this->expectException(BuilderException::class); + $this->expectExceptionMessage("Between statements expects exactly 2 values"); $select = $this->database->select()->distinct()->from(['users']) ->having('balance', 'BETWEEN', 0); - - $this->assertSameQuery( - 'SELECT DISTINCT * FROM {users} HAVING {balance} NOT BETWEEN ? AND ?', - $select - ); } public function testHavingSelectWithFullySpecificColumnNameInHaving(): void @@ -1484,12 +1458,11 @@ public function testHavingShortHavingWithNotBetweenCondition(): void ); } - /** - * @expectedException \Spiral\Database\Exception\BuilderException - * @expectedExceptionMessage Exactly 2 array values are required for between statement - */ public function testHavingShortHavingWithBetweenConditionBadArguments(): void { + $this->expectException(BuilderException::class); + $this->expectExceptionMessage("Exactly 2 array values are required for between statement"); + $select = $this->database->select() ->from(['users']) ->having( @@ -1949,23 +1922,21 @@ public function testShortWhereValueAsParameter(): void ); } - /** - * @expectedException \Spiral\Database\Exception\BuilderException - * @expectedExceptionMessage Arrays must be wrapped with Parameter instance - */ public function testBadArrayParameter(): void { + $this->expectException(BuilderException::class); + $this->expectExceptionMessage("Arrays must be wrapped with Parameter instance"); + $this->database->select() ->from(['users']) ->where('status', 'IN', ['active', 'blocked']); } - /** - * @expectedException \Spiral\Database\Exception\BuilderException - * @expectedExceptionMessage Arrays must be wrapped with Parameter instance - */ public function testBadArrayParameterInShortWhere(): void { + $this->expectException(BuilderException::class); + $this->expectExceptionMessage("Arrays must be wrapped with Parameter instance"); + $this->database->select() ->from(['users']) ->where( diff --git a/tests/Database/StatementTest.php b/tests/Database/StatementTest.php index 4a8188e8..e9e65fa8 100644 --- a/tests/Database/StatementTest.php +++ b/tests/Database/StatementTest.php @@ -356,21 +356,16 @@ public function testDatetimeInQuery(): void ); } - /** - * @expectedException \Spiral\Database\Exception\StatementException - */ public function testNativeParametersError(): void { $this->fillData(); - $row = $this->database->query( + $this->expectException(\Spiral\Database\Exception\StatementException::class); + + $this->database->query( 'SELECT * FROM sample_table WHERE id = :id', [':id' => [1, 2]] )->fetch(); - - $i = 4; - $this->assertEquals(md5((string)$i), $row['name']); - $this->assertEquals($i * 10, $row['value']); } public function testUnpackArrayFromParameter(): void diff --git a/tests/Database/TableTest.php b/tests/Database/TableTest.php index 791d16f7..ef528732 100644 --- a/tests/Database/TableTest.php +++ b/tests/Database/TableTest.php @@ -46,7 +46,7 @@ public function schema(string $table): AbstractTable public function testGetSchema(): void { - $this->assertInternalType('array', $this->database->getDriver()->__debugInfo()); + $this->assertIsArray($this->database->getDriver()->__debugInfo()); $this->assertInstanceOf(Table::class, $this->database->table('table')); $this->assertInstanceOf(AbstractTable::class, $this->database->table('table')->getSchema()); } @@ -267,7 +267,6 @@ public function testInsertMultiple(): void ); } - public function testAggregationByPass(): void { $table = $this->database->table('table'); From f9cdcc977de6ad304efa4fc004336034d10d5d9b Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 26 Mar 2021 18:15:27 +0300 Subject: [PATCH 2/4] No limit for `varchar` field. #70 --- src/Schema/AbstractColumn.php | 10 +++++----- tests/Database/AlterColumnTest.php | 7 +++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Schema/AbstractColumn.php b/src/Schema/AbstractColumn.php index b621bc68..d73d33ce 100644 --- a/src/Schema/AbstractColumn.php +++ b/src/Schema/AbstractColumn.php @@ -613,11 +613,11 @@ 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 > 255) { + // throw new SchemaException( + // 'String size can\'t exceed 255 characters. Use text instead' + // ); + // } if ($size < 0) { throw new SchemaException( diff --git a/tests/Database/AlterColumnTest.php b/tests/Database/AlterColumnTest.php index 0d90cfa3..087e13c7 100644 --- a/tests/Database/AlterColumnTest.php +++ b/tests/Database/AlterColumnTest.php @@ -147,16 +147,15 @@ public function testColumnSizeException(): void $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 From 7f86f484a8364017fca66bfe9a4cd900a2476227 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 26 Mar 2021 19:00:25 +0300 Subject: [PATCH 3/4] Fix cycle/orm#60 --- src/DatabaseManager.php | 19 ++++++++++++++++++- tests/Database/ManagerTest.php | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 996dc355..c7fc23a3 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -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; @@ -89,7 +90,9 @@ final class DatabaseManager implements Container\SingletonInterface, Container\InjectorInterface { - use LoggerTrait; + use LoggerTrait { + setLogger as protected internalSetLogger; + } /** @var DatabaseConfig */ private $config; @@ -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 diff --git a/tests/Database/ManagerTest.php b/tests/Database/ManagerTest.php index f0defaf7..9b64116a 100644 --- a/tests/Database/ManagerTest.php +++ b/tests/Database/ManagerTest.php @@ -12,6 +12,8 @@ use Mockery as m; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; use Spiral\Core\Container; use Spiral\Database\Config\DatabaseConfig; use Spiral\Database\Database; @@ -130,6 +132,27 @@ public function testGetDrivers(): void $this->assertCount(2, $dbal->getDrivers()); } + public function testSetLogger(): void + { + $logger = m::mock(LoggerInterface::class); + + $driverWithoutLogger = m::mock(DriverInterface::class); + + $driverWithLogger = m::mock(DriverInterface::class, LoggerAwareInterface::class) + ->shouldReceive('setLogger')->with($logger)->once()->andReturnNull() + ->getMock(); + self::assertInstanceOf(LoggerAwareInterface::class, $driverWithLogger); + + $dbal = new DatabaseManager(new DatabaseConfig(self::DEFAULT_OPTIONS)); + + $dbal->addDriver('read', $driverWithoutLogger); + $dbal->addDriver('write', $driverWithLogger); + + $dbal->setLogger($logger); + + m::close(); + } + public function testGetDatabases(): void { $read = m::mock(DriverInterface::class); From 97bfa1fac7070d677f7f518442465c7b0ccd39fc Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 26 Mar 2021 23:03:18 +0300 Subject: [PATCH 4/4] Cleanup --- src/Schema/AbstractColumn.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Schema/AbstractColumn.php b/src/Schema/AbstractColumn.php index d73d33ce..bfc553d9 100644 --- a/src/Schema/AbstractColumn.php +++ b/src/Schema/AbstractColumn.php @@ -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'