Skip to content

Commit

Permalink
Apply Code Style to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 10, 2024
1 parent 3822f76 commit 0b39d9e
Show file tree
Hide file tree
Showing 111 changed files with 61,664 additions and 61,716 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

return \Spiral\CodeStyle\Builder::create()
->include(__DIR__ . '/src')
->include(__DIR__ . '/tests')
->include(__FILE__)
->cache('./runtime/php-cs-fixer.cache')
->allowRisky()
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Jsoner.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function toJson(mixed $value, bool $encode = true, bool $validate

$result = (string) $value;

if ($validate && !\json_validate($result)) {
if ($validate && !json_validate($result)) {
throw new BuilderException('Invalid JSON value.');
}

Expand Down
124 changes: 53 additions & 71 deletions tests/Database/Functional/Driver/Common/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,70 +44,18 @@ public function tearDown(): void
$this->dropDatabase($this->database);
}

/**
* @return DriverInterface
*/
private function getDriver(array $driverConfig = [], array $connectionConfig = []): DriverInterface
{
$hash = \hash('crc32', static::DRIVER . ':' . \json_encode($driverConfig) . \json_encode($connectionConfig));

if (!isset(self::$memoizedDrivers[$hash])) {
$config = clone self::$config[static::DRIVER];
assert($config instanceof DriverConfig);

$this->applyDriverOptions($config, $driverConfig);
$config->connection = $this->applyConnectionOptions($config->connection, $connectionConfig);

$driver = $config->driver::create($config);

$this->setUpLogger($driver);

self::$memoizedDrivers[$hash] = $driver;
}

return self::$memoizedDrivers[$hash];
}

private function applyConnectionOptions(ConnectionConfig $config, array $options): ConnectionConfig
{
if ($options === []) {
return $config;
}
$config = clone $config;
foreach ($options as $key => $value) {
$config->$key = $value;
}
return $config;
}

private function applyDriverOptions(DriverConfig $config, array $options): void
{
foreach ($options as $key => $value) {
if ($key === 'options') {
$value += $config->options;
}
$config->$key = $value;
}
}

/**
* @param array $connectionConfig
*
* @return Database
*/
protected function db(
string $name = 'default',
string $prefix = '',
array $driverConfig = [],
array $connectionConfig = []
array $connectionConfig = [],
): Database {
return new Database($name, $prefix, $this->getDriver($driverConfig, $connectionConfig));
}

/**
* Send sample query in a form where all quotation symbols replaced with { and }.
*
* @param string $query
* @param string $parameters
* @param FragmentInterface|string $fragment
*/
Expand All @@ -120,7 +68,6 @@ protected function assertSameQueryWithParameters(string $query, array $parameter
/**
* Send sample query in a form where all quotation symbols replaced with { and }.
*
* @param string $query
* @param FragmentInterface|string $fragment
*/
protected function assertSameQuery(string $query, $fragment): void
Expand All @@ -130,22 +77,19 @@ protected function assertSameQuery(string $query, $fragment): void
}

//Preparing query
$query = str_replace(
$query = \str_replace(
['{', '}'],
explode('\a', $this->db()->getDriver()->identifier('\a')),
$query
\explode('\a', $this->db()->getDriver()->identifier('\a')),
$query,
);

$this->assertSame(
preg_replace('/\s+/', '', $query),
preg_replace('/\s+/', '', (string)$fragment)
\preg_replace('/\s+/', '', $query),
\preg_replace('/\s+/', '', (string) $fragment),
);
}

/**
* @param Database|null $database
*/
protected function dropDatabase(Database $database = null): void
protected function dropDatabase(?Database $database = null): void
{
if ($database === null) {
return;
Expand All @@ -168,11 +112,6 @@ protected function dropDatabase(Database $database = null): void
}
}

/**
* @param AbstractTable $table
*
* @return AbstractTable
*/
protected function fetchSchema(AbstractTable $table): AbstractTable
{
return $this->schema($table->getFullName());
Expand All @@ -196,12 +135,12 @@ protected function makeMessage(string $table, Comparator $comparator)
$names = [];
foreach ($comparator->alteredColumns() as $pair) {
$names[] = $pair[0]->getName();
print_r($pair);
\print_r($pair);
}

return "Table '{$table}' not synced, column(s) '" . implode(
return "Table '{$table}' not synced, column(s) '" . \implode(
"', '",
$names
$names,
) . "' have been changed.";
}

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

return $ref->getValue($object);
}

private function getDriver(array $driverConfig = [], array $connectionConfig = []): DriverInterface
{
$hash = \hash('crc32', static::DRIVER . ':' . \json_encode($driverConfig) . \json_encode($connectionConfig));

if (!isset(self::$memoizedDrivers[$hash])) {
$config = clone self::$config[static::DRIVER];
\assert($config instanceof DriverConfig);

$this->applyDriverOptions($config, $driverConfig);
$config->connection = $this->applyConnectionOptions($config->connection, $connectionConfig);

$driver = $config->driver::create($config);

$this->setUpLogger($driver);

self::$memoizedDrivers[$hash] = $driver;
}

return self::$memoizedDrivers[$hash];
}

private function applyConnectionOptions(ConnectionConfig $config, array $options): ConnectionConfig
{
if ($options === []) {
return $config;
}
$config = clone $config;
foreach ($options as $key => $value) {
$config->$key = $value;
}
return $config;
}

private function applyDriverOptions(DriverConfig $config, array $options): void
{
foreach ($options as $key => $value) {
if ($key === 'options') {
$value += $config->options;
}
$config->$key = $value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ public function testTableAccess(): void
{
$this->assertInstanceOf(
Table::class,
$this->db()->table('sample')
$this->db()->table('sample'),
);
}

public function testTableSchemaAccess(): void
{
$this->assertInstanceOf(
AbstractTable::class,
$this->db()->table('sample')->getSchema()
$this->db()->table('sample')->getSchema(),
);
}

public function testTableDatabaseAccess(): void
{
$this->assertEquals(
$this->db(),
$this->db()->table('sample')->getDatabase()
$this->db()->table('sample')->getDatabase(),
);
}

public function testCompilerAccess(): void
{
$this->assertInstanceOf(
CompilerInterface::class,
$this->db()->getDriver()->getQueryCompiler('')
$this->db()->getDriver()->getQueryCompiler(''),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,34 @@
use Cycle\Database\Tests\Stub\Driver\MysqlWrapDriver;
use Cycle\Database\Tests\Stub\Driver\PostgresWrapDriver;
use Cycle\Database\Tests\Stub\Driver\SQLiteWrapDriver;
use Exception;
use PDOStatement;
use RuntimeException;

/**
* @runInSeparateProcess
*/
abstract class ConnectionExceptionTest extends BaseConnectionTest
{
/**
* @return iterable<Exception>
* @return iterable<\Exception>
*/
abstract public function reconnectableExceptionsProvider(): iterable;

/**
* @dataProvider reconnectableExceptionsProvider()
*/
public function testConnectionExceptionOutOfTransaction(Exception $exception): void
public function testConnectionExceptionOutOfTransaction(\Exception $exception): void
{
$driver = $this->getDriver();
$this->configureExceptionsQuery($driver, [$exception]);

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

$this->assertSame('42', (string)$result);
$this->assertSame('42', (string) $result);
}

/**
* @dataProvider reconnectableExceptionsProvider()
*/
public function testConnectionExceptionInTransaction(Exception $exception): void
public function testConnectionExceptionInTransaction(\Exception $exception): void
{
$driver = $this->getDriver();
$driver->beginTransaction();
Expand All @@ -58,7 +55,7 @@ public function testConnectionExceptionInTransaction(Exception $exception): void
/**
* @dataProvider reconnectableExceptionsProvider()
*/
public function testConnectionExceptionReconnectsOnce(Exception $exception): void
public function testConnectionExceptionReconnectsOnce(\Exception $exception): void
{
$driver = $this->getDriver();
$this->configureExceptionsQuery($driver, [$exception, $exception]);
Expand All @@ -72,7 +69,7 @@ public function testNonConnectionExceptionOutOfTransaction(): void
{
$driver = $this->getDriver();
$this->configureExceptionsQuery($driver, [
new RuntimeException('Test exception 42.'),
new \RuntimeException('Test exception 42.'),
]);

$this->expectException(StatementException::class);
Expand All @@ -84,8 +81,8 @@ public function testNonConnectionExceptionOutOfTransaction(): void
private function configureExceptionsQuery(
SQLiteWrapDriver|MysqlWrapDriver|PostgresWrapDriver|MSSQLWrapDriver $driver,
array $exceptions,
) {
$driver->setQueryCallback(static function (PDOStatement $statement, ?array $params) use (&$exceptions) {
): void {
$driver->setQueryCallback(static function (\PDOStatement $statement, ?array $params) use (&$exceptions) {
if ($exceptions !== []) {
throw \array_shift($exceptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,9 @@
use Cycle\Database\Schema\AbstractColumn;
use Cycle\Database\Schema\AbstractTable;
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
use PDO;

abstract class CustomOptionsTest extends BaseTest
{
public function setUp(): void
{
$this->database = $this->db(connectionConfig: [
'options' => [
/**
* Stringify fetches will return everything as string,
* so e.g. decimal/numeric type will not be converted to float, thus losing the precision
* and letting users handle it differently.
*
* As a result, int is also returned as string, so we need to make sure
* that we're properly casting schema information details.
*/
PDO::ATTR_STRINGIFY_FETCHES => true,
],
]);

parent::setUp();
}

public function testDecimalSizes(): void
{
$schema = $this->sampleSchema('table');
Expand Down Expand Up @@ -74,4 +54,23 @@ public function sampleSchema(string $table): AbstractTable

return $schema;
}

public function setUp(): void
{
$this->database = $this->db(connectionConfig: [
'options' => [
/**
* Stringify fetches will return everything as string,
* so e.g. decimal/numeric type will not be converted to float, thus losing the precision
* and letting users handle it differently.
*
* As a result, int is also returned as string, so we need to make sure
* that we're properly casting schema information details.
*/
\PDO::ATTR_STRINGIFY_FETCHES => true,
],
]);

parent::setUp();
}
}
14 changes: 7 additions & 7 deletions tests/Database/Functional/Driver/Common/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

abstract class DatabaseTest extends BaseTest
{
public function tearDown(): void
{
$this->dropDatabase($this->db());
}

public function testConnect(): void
{
$this->database->getDriver()->connect();
Expand All @@ -36,10 +31,10 @@ public function testGetType(): void
$db = $this->db();
$this->assertSame(
$db->getDriver()->getType(),
$db->getType()
$db->getType(),
);

$this->assertSame(strtolower(static::DRIVER), strtolower($db->getType()));
$this->assertSame(\strtolower(static::DRIVER), \strtolower($db->getType()));
}

public function testReadWrite(): void
Expand Down Expand Up @@ -100,4 +95,9 @@ public function testPrefix(): void
$this->assertTrue($db->hasTable('prefix_test'));
$this->assertCount(2, $db->getTables());
}

public function tearDown(): void
{
$this->dropDatabase($this->db());
}
}
Loading

0 comments on commit 0b39d9e

Please sign in to comment.