Skip to content

Commit

Permalink
Adjust last changes yiisoft/db. (#80)
Browse files Browse the repository at this point in the history
* Adjust last changes yiisoft/db.
* Remove duplicate code.
* Add scrutinizer tests.

Co-authored-by: Wilmer Arambula <[email protected]>
  • Loading branch information
terabytesoftw and terabytesoftw authored Oct 2, 2021
1 parent f2e7494 commit f302862
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 362 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_size = 2
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ jobs:
postgres:
image: postgres:${{ matrix.pgsql }}
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: yiitest
POSTGRES_USER: scrutinizer
POSTGRES_PASSWORD: scrutinizer
POSTGRES_DB: scrutinizer
ports:
- 5432:5432
options: --name=postgres --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ jobs:
postgres:
image: postgres:13
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: yiitest
POSTGRES_USER: scrutinizer
POSTGRES_PASSWORD: scrutinizer
POSTGRES_DB: scrutinizer
ports:
- 5432:5432
options: --name=postgres --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3
Expand Down
44 changes: 30 additions & 14 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
checks:
php: true
php: true

filter:
paths:
- "src/*"

tools:
external_code_coverage:
timeout: 660
paths:
- "src/*"

build:
nodes:
analysis:
environment:
php: 7.4.12
environment:
php:
version: 8.0.11
ini:
"xdebug.mode": coverage

nodes:
analysis:
tests:
override:
- php-scrutinizer-run

tests-and-coverage:
services:
postgres: 13

dependencies:
override:
- composer self-update
- composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

tests:
override:
- php-scrutinizer-run
tests:
override:
- command: "./vendor/bin/phpunit --coverage-clover ./coverage.xml"
on_node: 1
coverage:
file: coverage.xml
format: php-clover
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ext-json": "*",
"ext-pdo": "*",
"yiisoft/arrays": "^1.0",
"yiisoft/db": "^3.0@dev",
"yiisoft/db": "^3.0",
"yiisoft/json": "^1.0",
"yiisoft/strings": "^2.0"
},
Expand All @@ -33,7 +33,6 @@
"vimeo/psalm": "^4.4",
"yiisoft/aliases": "^1.1|^2.0",
"yiisoft/cache": "^1.0",
"yiisoft/di": "^3.0@dev",
"yiisoft/log": "^1.0"
},
"autoload": {
Expand Down
25 changes: 23 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@
use PDO;
use Yiisoft\Db\Command\Command;
use Yiisoft\Db\Connection\Connection as AbstractConnection;
use Yiisoft\Db\Cache\QueryCache;
use Yiisoft\Db\Cache\SchemaCache;

/**
* The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
*/
final class Connection extends AbstractConnection
{
private QueryCache $queryCache;
private SchemaCache $schemaCache;

public function __construct(string $dsn, QueryCache $queryCache, SchemaCache $schemaCache)
{
$this->queryCache = $queryCache;
$this->schemaCache = $schemaCache;

parent::__construct($dsn, $queryCache);
}

/**
* Creates a command for execution.
*
Expand All @@ -27,7 +40,15 @@ public function createCommand(string $sql = null, array $params = []): Command
$sql = $this->quoteSql($sql);
}

$command = new Command($this, $sql);
$command = new Command($this, $this->queryCache, $sql);

if ($this->logger !== null) {
$command->setLogger($this->logger);
}

if ($this->profiler !== null) {
$command->setProfiler($this->profiler);
}

return $command->bindValues($params);
}
Expand All @@ -39,7 +60,7 @@ public function createCommand(string $sql = null, array $params = []): Command
*/
public function getSchema(): Schema
{
return new Schema($this);
return new Schema($this, $this->schemaCache);
}

/**
Expand Down
64 changes: 7 additions & 57 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testConstruct(): void
{
$db = $this->getConnection();

$this->assertEquals($this->params()['yiisoft/db-pgsql']['dsn'], $db->getDsn());
$this->assertEquals(self::DB_DSN, $db->getDsn());
}

public function testGetDriverName(): void
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testOpenClose(): void
$this->assertFalse($db->isActive());
$this->assertNull($db->getPDO());

$db = new Connection('unknown::memory:', $this->dependencies);
$db = $this->createConnection('unknown::memory:');

$this->expectException(Exception::class);
$this->expectExceptionMessage('could not find driver');
Expand Down Expand Up @@ -182,17 +182,7 @@ public function testGetPdoAfterClose(): void
{
$db = $this->getConnection();

$db->setSlaves(
'1',
[
'class' => Connection::class,
'__construct()' => [
'dsn' => $this->params()['yiisoft/db-pgsql']['dsn'],
],
'setUsername()' => [$db->getUsername()],
'setPassword()' => [$db->getPassword()],
]
);
$db->setSlave('1', $this->createConnection(self::DB_DSN));

$this->assertNotNull($db->getSlavePdo(false));

Expand All @@ -216,17 +206,7 @@ public function testServerStatusCacheWorks(): void

$db = $this->getConnection(true);

$db->setMasters(
'1',
[
'class' => Connection::class,
'__construct()' => [
'dsn' => $this->params()['yiisoft/db-pgsql']['dsn'],
],
'setUsername()' => [$db->getUsername()],
'setPassword()' => [$db->getPassword()],
]
);
$db->setMaster('1', $this->createConnection(self::DB_DSN));

$db->setShuffleMasters(false);

Expand All @@ -251,17 +231,7 @@ public function testServerStatusCacheWorks(): void
['Yiisoft\Db\Connection\Connection::openFromPoolSequentially', 'host:invalid']
);

$db->setMasters(
'1',
[
'class' => Connection::class,
'__construct()' => [
'dsn' => 'host:invalid',
],
'setUsername()' => [$db->getUsername()],
'setPassword()' => [$db->getPassword()],
]
);
$db->setMaster('1', $this->createConnection('host:invalid'));

$db->setShuffleMasters(true);

Expand All @@ -284,17 +254,7 @@ public function testServerStatusCacheCanBeDisabled(): void

$db = $this->getConnection();

$db->setMasters(
'1',
[
'class' => Connection::class,
'__construct()' => [
'dsn' => $this->params()['yiisoft/db-pgsql']['dsn'],
],
'setUsername()' => [$db->getUsername()],
'setPassword()' => [$db->getPassword()],
]
);
$db->setMaster('1', $this->createConnection(self::DB_DSN));

$this->schemaCache->setEnable(false);

Expand All @@ -316,17 +276,7 @@ public function testServerStatusCacheCanBeDisabled(): void
['Yiisoft\Db\Connection\Connection::openFromPoolSequentially', 'host:invalid']
);

$db->setMasters(
'1',
[
'class' => Connection::class,
'__construct()' => [
'dsn' => 'host:invalid',
],
'setUsername()' => [$db->getUsername()],
'setPassword()' => [$db->getPassword()],
]
);
$db->setMaster('1', $this->createConnection('host:invalid'));

try {
$db->open();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function testResetSequencePostgres12(): void
$this->markTestSkipped('PostgreSQL < 12.0 does not support GENERATED AS IDENTITY columns.');
}

$this->prepareDatabase('@data/postgres12.sql');
$this->prepareDatabase(self::DB_DSN, __DIR__ . '/Fixture/postgres12.sql');

$qb = $this->getQueryBuilder();

Expand Down
Empty file added tests/Runtime/.gitignore
Empty file.
4 changes: 2 additions & 2 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public function testGeneratedValues(): void
$this->markTestSkipped('PostgreSQL < 12.0 does not support GENERATED AS IDENTITY columns.');
}

$this->prepareDatabase('@data/postgres12.sql');
$this->prepareDatabase(self::DB_DSN, __DIR__ . '/Fixture/postgres12.sql');

$table = $this->getConnection()->getSchema()->getTableSchema('generated');

Expand All @@ -408,7 +408,7 @@ public function testPartitionedTable(): void
$this->markTestSkipped('PostgreSQL < 10.0 does not support PARTITION BY clause.');
}

$this->prepareDatabase('@data/postgres10.sql');
$this->prepareDatabase(self::DB_DSN, __DIR__ . '/Fixture/postgres10.sql');

$this->assertNotNull($this->getConnection()->getSchema()->getTableSchema('partitioned'));
}
Expand Down
Loading

0 comments on commit f302862

Please sign in to comment.