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

Commit 04f94c9

Browse files
authored
Merge pull request #71 from roxblnfk/feature/refactoring
DBAL::setLogger for all drivers; remove varchar limit; refactoring
2 parents 626b7fc + 97bfa1f commit 04f94c9

12 files changed

+121
-148
lines changed

src/DatabaseManager.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerExceptionInterface;
1515
use Psr\Log\LoggerAwareInterface;
16+
use Psr\Log\LoggerInterface;
1617
use Psr\Log\NullLogger;
1718
use Spiral\Core\Container;
1819
use Spiral\Core\FactoryInterface;
@@ -89,7 +90,9 @@ final class DatabaseManager implements
8990
Container\SingletonInterface,
9091
Container\InjectorInterface
9192
{
92-
use LoggerTrait;
93+
use LoggerTrait {
94+
setLogger as protected internalSetLogger;
95+
}
9396

9497
/** @var DatabaseConfig */
9598
private $config;
@@ -103,6 +106,20 @@ final class DatabaseManager implements
103106
/** @var DriverInterface[] */
104107
private $drivers = [];
105108

109+
/**
110+
* Set logger for all drivers
111+
*/
112+
public function setLogger(LoggerInterface $logger): void
113+
{
114+
$this->internalSetLogger($logger);
115+
// Assign the logger to all initialized drivers
116+
foreach ($this->drivers as $driver) {
117+
if ($driver instanceof LoggerAwareInterface) {
118+
$driver->setLogger($this->logger);
119+
}
120+
}
121+
}
122+
106123
/**
107124
* @param DatabaseConfig $config
108125
* @param FactoryInterface $factory

src/Schema/AbstractColumn.php

-6
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,6 @@ public function string(int $size = 255): AbstractColumn
613613
{
614614
$this->type('string');
615615

616-
if ($size > 255) {
617-
throw new SchemaException(
618-
'String size can\'t exceed 255 characters. Use text instead'
619-
);
620-
}
621-
622616
if ($size < 0) {
623617
throw new SchemaException(
624618
'Invalid string length value'

tests/Database/AlterColumnTest.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function testAddColumnWithDefaultValue(): void
106106
$schema->string('new_column')->defaultValue('some_value');
107107
$schema->save();
108108

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

111111
$this->assertSameAsInDB($schema);
112112
}
@@ -137,28 +137,25 @@ public function testMakeNullable(): void
137137
$this->assertTrue($this->fetchSchema($schema)->column('first_name')->isNullable());
138138
}
139139

140-
/**
141-
* @expectedException \Spiral\Database\Exception\SchemaException
142-
*/
143140
public function testColumnSizeException(): void
144141
{
142+
$this->expectException(\Spiral\Database\Exception\SchemaException::class);
145143
$schema = $this->sampleSchema('table');
146144
$this->assertTrue($schema->exists());
147145

148146
$schema->string('first_name', -1);
149147
$schema->save();
150148
}
151149

152-
/**
153-
* @expectedException \Spiral\Database\Exception\SchemaException
154-
*/
155150
public function testColumnSize2Exception(): void
156151
{
157152
$schema = $this->sampleSchema('table');
158153
$this->assertTrue($schema->exists());
159154

160155
$schema->string('first_name', 256);
161-
$schema->save();
156+
157+
// No limit error
158+
$this->assertTrue(true);
162159
}
163160

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

190-
$this->assertInternalType('array', $schema->decimal('double_2', 10, 1)->__debugInfo());
187+
$this->assertIsArray($schema->decimal('double_2', 10, 1)->__debugInfo());
191188
}
192189

193-
/**
194-
* @expectedException \Spiral\Database\Exception\SchemaException
195-
*/
196190
public function testDecimalSizesException(): void
197191
{
192+
$this->expectException(\Spiral\Database\Exception\SchemaException::class);
198193
$schema = $this->sampleSchema('table');
199194
$this->assertTrue($schema->exists());
200195

tests/Database/ConfigTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
declare(strict_types=1);
1010

11-
namespace Spiral\tests\Cases\Database;
11+
namespace Spiral\Database\tests;
1212

1313
use PHPUnit\Framework\TestCase;
1414
use Spiral\Core\Container\Autowire;
1515
use Spiral\Database\Config\DatabaseConfig;
16+
use Spiral\Database\Exception\ConfigException;
1617

1718
class ConfigTest extends TestCase
1819
{
@@ -44,9 +45,6 @@ public function testHasDatabase(): void
4445
$this->assertFalse($config->hasDatabase('database-1'));
4546
}
4647

47-
/**
48-
* @expectedException \Spiral\Database\Exception\ConfigException
49-
*/
5048
public function testDatabaseException(): void
5149
{
5250
$config = new DatabaseConfig(
@@ -58,7 +56,10 @@ public function testDatabaseException(): void
5856
]
5957
]
6058
);
61-
$this->assertSame('test3', $config->getDatabase('test3'));
59+
60+
$this->expectException(ConfigException::class);
61+
62+
$config->getDatabase('test3');
6263
}
6364

6465
public function testDatabaseDriver(): void
@@ -215,9 +216,6 @@ public function testHasDriver(): void
215216
$this->assertFalse($config->hasDriver('database-1'));
216217
}
217218

218-
/**
219-
* @expectedException \Spiral\Database\Exception\ConfigException
220-
*/
221219
public function testDriverException(): void
222220
{
223221
$config = new DatabaseConfig(
@@ -226,6 +224,8 @@ public function testDriverException(): void
226224
]
227225
);
228226

227+
$this->expectException(ConfigException::class);
228+
229229
$config->getDriver('test3');
230230
}
231231

tests/Database/CreateTableTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Spiral\Database\Tests;
1212

1313
use Spiral\Database\Database;
14+
use Spiral\Database\Exception\SchemaException;
1415
use Spiral\Database\Schema\AbstractTable;
1516

1617
abstract class CreateTableTest extends BaseTest
@@ -58,7 +59,7 @@ public function testSimpleCreation(): void
5859
$this->assertTrue($schema->exists());
5960
$this->assertSameAsInDB($schema);
6061

61-
$this->assertInternalType('array', $schema->__debugInfo());
62+
$this->assertIsArray($schema->__debugInfo());
6263
}
6364

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

136-
/**
137-
* @expectedException \Spiral\Database\Exception\SchemaException
138-
*/
139137
public function testDeleteNonExisted(): void
140138
{
141139
$schema = $this->schema('table');
142140
$this->assertFalse($schema->exists());
141+
142+
$this->expectException(SchemaException::class);
143+
143144
$schema->declareDropped();
144145
}
145146
}

tests/Database/Driver/MySQL/DatetimeColumnTest.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Spiral\Database\Tests\Driver\MySQL;
1313

14+
use Spiral\Database\Exception\HandlerException;
15+
1416
/**
1517
* @group driver
1618
* @group driver-mysql
@@ -19,13 +21,12 @@ class DatetimeColumnTest extends \Spiral\Database\Tests\DatetimeColumnTest
1921
{
2022
public const DRIVER = 'mysql';
2123

22-
/**
23-
* @expectedException \Spiral\Database\Exception\HandlerException
24-
* @expectedExceptionMessage SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid
25-
* default value for 'target'
26-
*/
2724
public function testTimestampDatetimeZero(): void
2825
{
26+
$this->expectExceptionMessage(
27+
"SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'target'"
28+
);
29+
$this->expectException(HandlerException::class);
2930
parent::testTimestampDatetimeZero();
3031
}
3132
}

tests/Database/Driver/MySQL/DefaultValueTest.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,17 @@ class DefaultValueTest extends \Spiral\Database\Tests\DefaultValueTest
1919
{
2020
public const DRIVER = 'mysql';
2121

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

32-
/**
33-
* @expectedException \Spiral\Database\Driver\MySQL\Exception\MySQLException
34-
* @expectedExceptionMessage Column table.target of type text/blob can not have non empty
35-
* default value
36-
*/
3729
public function testTextDefaultValueEmpty(): void
3830
{
31+
$this->expectException(\Spiral\Database\Driver\MySQL\Exception\MySQLException::class);
32+
$this->expectExceptionMessage("Column table.target of type text/blob can not have non empty default value");
3933
parent::testTextDefaultValueEmpty();
4034
}
4135
}

tests/Database/Driver/Postgres/ConsistencyTest.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,13 @@ public function testPrimary(): void
5757
$this->assertSame('target', $d->getPrimaryKey('', 'table'));
5858
}
5959

60-
/**
61-
* @expectedException \Spiral\Database\Exception\DriverException
62-
*/
6360
public function testPrimaryException(): void
6461
{
65-
/**
66-
* @var PostgresDriver $d
67-
*/
62+
/** @var PostgresDriver $d */
6863
$d = $this->getDriver();
6964

65+
$this->expectException(\Spiral\Database\Exception\DriverException::class);
66+
7067
$this->assertSame('target', $d->getPrimaryKey('', 'table'));
7168
}
7269

0 commit comments

Comments
 (0)