Skip to content

Commit 0b39d9e

Browse files
committed
Apply Code Style to tests
1 parent 3822f76 commit 0b39d9e

File tree

111 files changed

+61664
-61716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+61664
-61716
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
return \Spiral\CodeStyle\Builder::create()
88
->include(__DIR__ . '/src')
9+
->include(__DIR__ . '/tests')
910
->include(__FILE__)
1011
->cache('./runtime/php-cs-fixer.cache')
1112
->allowRisky()

src/Driver/Jsoner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function toJson(mixed $value, bool $encode = true, bool $validate
3232

3333
$result = (string) $value;
3434

35-
if ($validate && !\json_validate($result)) {
35+
if ($validate && !json_validate($result)) {
3636
throw new BuilderException('Invalid JSON value.');
3737
}
3838

tests/Database/Functional/Driver/Common/BaseTest.php

Lines changed: 53 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -44,70 +44,18 @@ public function tearDown(): void
4444
$this->dropDatabase($this->database);
4545
}
4646

47-
/**
48-
* @return DriverInterface
49-
*/
50-
private function getDriver(array $driverConfig = [], array $connectionConfig = []): DriverInterface
51-
{
52-
$hash = \hash('crc32', static::DRIVER . ':' . \json_encode($driverConfig) . \json_encode($connectionConfig));
53-
54-
if (!isset(self::$memoizedDrivers[$hash])) {
55-
$config = clone self::$config[static::DRIVER];
56-
assert($config instanceof DriverConfig);
57-
58-
$this->applyDriverOptions($config, $driverConfig);
59-
$config->connection = $this->applyConnectionOptions($config->connection, $connectionConfig);
60-
61-
$driver = $config->driver::create($config);
62-
63-
$this->setUpLogger($driver);
64-
65-
self::$memoizedDrivers[$hash] = $driver;
66-
}
67-
68-
return self::$memoizedDrivers[$hash];
69-
}
70-
71-
private function applyConnectionOptions(ConnectionConfig $config, array $options): ConnectionConfig
72-
{
73-
if ($options === []) {
74-
return $config;
75-
}
76-
$config = clone $config;
77-
foreach ($options as $key => $value) {
78-
$config->$key = $value;
79-
}
80-
return $config;
81-
}
82-
83-
private function applyDriverOptions(DriverConfig $config, array $options): void
84-
{
85-
foreach ($options as $key => $value) {
86-
if ($key === 'options') {
87-
$value += $config->options;
88-
}
89-
$config->$key = $value;
90-
}
91-
}
92-
93-
/**
94-
* @param array $connectionConfig
95-
*
96-
* @return Database
97-
*/
9847
protected function db(
9948
string $name = 'default',
10049
string $prefix = '',
10150
array $driverConfig = [],
102-
array $connectionConfig = []
51+
array $connectionConfig = [],
10352
): Database {
10453
return new Database($name, $prefix, $this->getDriver($driverConfig, $connectionConfig));
10554
}
10655

10756
/**
10857
* Send sample query in a form where all quotation symbols replaced with { and }.
10958
*
110-
* @param string $query
11159
* @param string $parameters
11260
* @param FragmentInterface|string $fragment
11361
*/
@@ -120,7 +68,6 @@ protected function assertSameQueryWithParameters(string $query, array $parameter
12068
/**
12169
* Send sample query in a form where all quotation symbols replaced with { and }.
12270
*
123-
* @param string $query
12471
* @param FragmentInterface|string $fragment
12572
*/
12673
protected function assertSameQuery(string $query, $fragment): void
@@ -130,22 +77,19 @@ protected function assertSameQuery(string $query, $fragment): void
13077
}
13178

13279
//Preparing query
133-
$query = str_replace(
80+
$query = \str_replace(
13481
['{', '}'],
135-
explode('\a', $this->db()->getDriver()->identifier('\a')),
136-
$query
82+
\explode('\a', $this->db()->getDriver()->identifier('\a')),
83+
$query,
13784
);
13885

13986
$this->assertSame(
140-
preg_replace('/\s+/', '', $query),
141-
preg_replace('/\s+/', '', (string)$fragment)
87+
\preg_replace('/\s+/', '', $query),
88+
\preg_replace('/\s+/', '', (string) $fragment),
14289
);
14390
}
14491

145-
/**
146-
* @param Database|null $database
147-
*/
148-
protected function dropDatabase(Database $database = null): void
92+
protected function dropDatabase(?Database $database = null): void
14993
{
15094
if ($database === null) {
15195
return;
@@ -168,11 +112,6 @@ protected function dropDatabase(Database $database = null): void
168112
}
169113
}
170114

171-
/**
172-
* @param AbstractTable $table
173-
*
174-
* @return AbstractTable
175-
*/
176115
protected function fetchSchema(AbstractTable $table): AbstractTable
177116
{
178117
return $this->schema($table->getFullName());
@@ -196,12 +135,12 @@ protected function makeMessage(string $table, Comparator $comparator)
196135
$names = [];
197136
foreach ($comparator->alteredColumns() as $pair) {
198137
$names[] = $pair[0]->getName();
199-
print_r($pair);
138+
\print_r($pair);
200139
}
201140

202-
return "Table '{$table}' not synced, column(s) '" . implode(
141+
return "Table '{$table}' not synced, column(s) '" . \implode(
203142
"', '",
204-
$names
143+
$names,
205144
) . "' have been changed.";
206145
}
207146

@@ -248,4 +187,47 @@ protected function getPrivatePropertyValue(object $object, string $property): mi
248187

249188
return $ref->getValue($object);
250189
}
190+
191+
private function getDriver(array $driverConfig = [], array $connectionConfig = []): DriverInterface
192+
{
193+
$hash = \hash('crc32', static::DRIVER . ':' . \json_encode($driverConfig) . \json_encode($connectionConfig));
194+
195+
if (!isset(self::$memoizedDrivers[$hash])) {
196+
$config = clone self::$config[static::DRIVER];
197+
\assert($config instanceof DriverConfig);
198+
199+
$this->applyDriverOptions($config, $driverConfig);
200+
$config->connection = $this->applyConnectionOptions($config->connection, $connectionConfig);
201+
202+
$driver = $config->driver::create($config);
203+
204+
$this->setUpLogger($driver);
205+
206+
self::$memoizedDrivers[$hash] = $driver;
207+
}
208+
209+
return self::$memoizedDrivers[$hash];
210+
}
211+
212+
private function applyConnectionOptions(ConnectionConfig $config, array $options): ConnectionConfig
213+
{
214+
if ($options === []) {
215+
return $config;
216+
}
217+
$config = clone $config;
218+
foreach ($options as $key => $value) {
219+
$config->$key = $value;
220+
}
221+
return $config;
222+
}
223+
224+
private function applyDriverOptions(DriverConfig $config, array $options): void
225+
{
226+
foreach ($options as $key => $value) {
227+
if ($key === 'options') {
228+
$value += $config->options;
229+
}
230+
$config->$key = $value;
231+
}
232+
}
251233
}

tests/Database/Functional/Driver/Common/BuildersAccessTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,31 @@ public function testTableAccess(): void
2424
{
2525
$this->assertInstanceOf(
2626
Table::class,
27-
$this->db()->table('sample')
27+
$this->db()->table('sample'),
2828
);
2929
}
3030

3131
public function testTableSchemaAccess(): void
3232
{
3333
$this->assertInstanceOf(
3434
AbstractTable::class,
35-
$this->db()->table('sample')->getSchema()
35+
$this->db()->table('sample')->getSchema(),
3636
);
3737
}
3838

3939
public function testTableDatabaseAccess(): void
4040
{
4141
$this->assertEquals(
4242
$this->db(),
43-
$this->db()->table('sample')->getDatabase()
43+
$this->db()->table('sample')->getDatabase(),
4444
);
4545
}
4646

4747
public function testCompilerAccess(): void
4848
{
4949
$this->assertInstanceOf(
5050
CompilerInterface::class,
51-
$this->db()->getDriver()->getQueryCompiler('')
51+
$this->db()->getDriver()->getQueryCompiler(''),
5252
);
5353
}
5454

tests/Database/Functional/Driver/Common/Connection/ConnectionExceptionTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,34 @@
1010
use Cycle\Database\Tests\Stub\Driver\MysqlWrapDriver;
1111
use Cycle\Database\Tests\Stub\Driver\PostgresWrapDriver;
1212
use Cycle\Database\Tests\Stub\Driver\SQLiteWrapDriver;
13-
use Exception;
14-
use PDOStatement;
15-
use RuntimeException;
1613

1714
/**
1815
* @runInSeparateProcess
1916
*/
2017
abstract class ConnectionExceptionTest extends BaseConnectionTest
2118
{
2219
/**
23-
* @return iterable<Exception>
20+
* @return iterable<\Exception>
2421
*/
2522
abstract public function reconnectableExceptionsProvider(): iterable;
2623

2724
/**
2825
* @dataProvider reconnectableExceptionsProvider()
2926
*/
30-
public function testConnectionExceptionOutOfTransaction(Exception $exception): void
27+
public function testConnectionExceptionOutOfTransaction(\Exception $exception): void
3128
{
3229
$driver = $this->getDriver();
3330
$this->configureExceptionsQuery($driver, [$exception]);
3431

3532
$result = $driver->query('SELECT 42')->fetchColumn(0);
3633

37-
$this->assertSame('42', (string)$result);
34+
$this->assertSame('42', (string) $result);
3835
}
3936

4037
/**
4138
* @dataProvider reconnectableExceptionsProvider()
4239
*/
43-
public function testConnectionExceptionInTransaction(Exception $exception): void
40+
public function testConnectionExceptionInTransaction(\Exception $exception): void
4441
{
4542
$driver = $this->getDriver();
4643
$driver->beginTransaction();
@@ -58,7 +55,7 @@ public function testConnectionExceptionInTransaction(Exception $exception): void
5855
/**
5956
* @dataProvider reconnectableExceptionsProvider()
6057
*/
61-
public function testConnectionExceptionReconnectsOnce(Exception $exception): void
58+
public function testConnectionExceptionReconnectsOnce(\Exception $exception): void
6259
{
6360
$driver = $this->getDriver();
6461
$this->configureExceptionsQuery($driver, [$exception, $exception]);
@@ -72,7 +69,7 @@ public function testNonConnectionExceptionOutOfTransaction(): void
7269
{
7370
$driver = $this->getDriver();
7471
$this->configureExceptionsQuery($driver, [
75-
new RuntimeException('Test exception 42.'),
72+
new \RuntimeException('Test exception 42.'),
7673
]);
7774

7875
$this->expectException(StatementException::class);
@@ -84,8 +81,8 @@ public function testNonConnectionExceptionOutOfTransaction(): void
8481
private function configureExceptionsQuery(
8582
SQLiteWrapDriver|MysqlWrapDriver|PostgresWrapDriver|MSSQLWrapDriver $driver,
8683
array $exceptions,
87-
) {
88-
$driver->setQueryCallback(static function (PDOStatement $statement, ?array $params) use (&$exceptions) {
84+
): void {
85+
$driver->setQueryCallback(static function (\PDOStatement $statement, ?array $params) use (&$exceptions) {
8986
if ($exceptions !== []) {
9087
throw \array_shift($exceptions);
9188
}

tests/Database/Functional/Driver/Common/Connection/CustomOptionsTest.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,9 @@
88
use Cycle\Database\Schema\AbstractColumn;
99
use Cycle\Database\Schema\AbstractTable;
1010
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
11-
use PDO;
1211

1312
abstract class CustomOptionsTest extends BaseTest
1413
{
15-
public function setUp(): void
16-
{
17-
$this->database = $this->db(connectionConfig: [
18-
'options' => [
19-
/**
20-
* Stringify fetches will return everything as string,
21-
* so e.g. decimal/numeric type will not be converted to float, thus losing the precision
22-
* and letting users handle it differently.
23-
*
24-
* As a result, int is also returned as string, so we need to make sure
25-
* that we're properly casting schema information details.
26-
*/
27-
PDO::ATTR_STRINGIFY_FETCHES => true,
28-
],
29-
]);
30-
31-
parent::setUp();
32-
}
33-
3414
public function testDecimalSizes(): void
3515
{
3616
$schema = $this->sampleSchema('table');
@@ -74,4 +54,23 @@ public function sampleSchema(string $table): AbstractTable
7454

7555
return $schema;
7656
}
57+
58+
public function setUp(): void
59+
{
60+
$this->database = $this->db(connectionConfig: [
61+
'options' => [
62+
/**
63+
* Stringify fetches will return everything as string,
64+
* so e.g. decimal/numeric type will not be converted to float, thus losing the precision
65+
* and letting users handle it differently.
66+
*
67+
* As a result, int is also returned as string, so we need to make sure
68+
* that we're properly casting schema information details.
69+
*/
70+
\PDO::ATTR_STRINGIFY_FETCHES => true,
71+
],
72+
]);
73+
74+
parent::setUp();
75+
}
7776
}

tests/Database/Functional/Driver/Common/DatabaseTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111

1212
abstract class DatabaseTest extends BaseTest
1313
{
14-
public function tearDown(): void
15-
{
16-
$this->dropDatabase($this->db());
17-
}
18-
1914
public function testConnect(): void
2015
{
2116
$this->database->getDriver()->connect();
@@ -36,10 +31,10 @@ public function testGetType(): void
3631
$db = $this->db();
3732
$this->assertSame(
3833
$db->getDriver()->getType(),
39-
$db->getType()
34+
$db->getType(),
4035
);
4136

42-
$this->assertSame(strtolower(static::DRIVER), strtolower($db->getType()));
37+
$this->assertSame(\strtolower(static::DRIVER), \strtolower($db->getType()));
4338
}
4439

4540
public function testReadWrite(): void
@@ -100,4 +95,9 @@ public function testPrefix(): void
10095
$this->assertTrue($db->hasTable('prefix_test'));
10196
$this->assertCount(2, $db->getTables());
10297
}
98+
99+
public function tearDown(): void
100+
{
101+
$this->dropDatabase($this->db());
102+
}
103103
}

0 commit comments

Comments
 (0)