Skip to content

Commit

Permalink
Replace AR with Db.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Oct 27, 2023
1 parent f18857e commit d0f3c86
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 62 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@
"require-dev": {
"codeception/codeception": "^5.0",
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10.4",
"rector/rector": "^0.18.0",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.8",
"yiisoft/active-record": "3.0.x-dev",
"yiisoft/assets": "^4.0",
"yiisoft/csrf": "^2.0",
"yiisoft/db": "^1.0",
"yiisoft/db-sqlite": "^1.0",
"yiisoft/psr-dummy-provider": "^1.0",
"yiisoft/router-fastroute": "^3.0",
"yiisoft/yii-cycle": "dev-master",
Expand Down
4 changes: 2 additions & 2 deletions config/di-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Yii\Debug\Api\Debug\Repository\CollectorRepository;
use Yiisoft\Yii\Debug\Api\Debug\Repository\CollectorRepositoryInterface;
use Yiisoft\Yii\Debug\Api\Inspector\Database\ActiveRecord\ActiveRecordSchemaProvider;
use Yiisoft\Yii\Debug\Api\Inspector\Database\Cycle\CycleSchemaProvider;
use Yiisoft\Yii\Debug\Api\Inspector\Database\Db\DbSchemaProvider;
use Yiisoft\Yii\Debug\Api\Inspector\Database\SchemaProviderInterface;
use Yiisoft\Yii\Debug\Storage\StorageInterface;

Expand All @@ -24,7 +24,7 @@
}

if ($container->has(ConnectionInterface::class)) {
return $container->get(ActiveRecordSchemaProvider::class);
return $container->get(DbSchemaProvider::class);
}

throw new LogicException(
Expand Down
42 changes: 15 additions & 27 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true">
<php>
<ini name="error_reporting" value="-1"/>
</php>

<testsuites>
<testsuite name="Yii Debug API tests">
<directory>./tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" failOnRisky="true" failOnWarning="true" stopOnFailure="false" executionOrder="random" resolveDependencies="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Yii Debug API tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,33 @@

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Api\Inspector\Database\ActiveRecord;
namespace Yiisoft\Yii\Debug\Api\Inspector\Database\Db;

use Yiisoft\ActiveRecord\ActiveRecord;
use Yiisoft\ActiveRecord\ActiveRecordFactory;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\ColumnSchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
use Yiisoft\Yii\Debug\Api\Inspector\Database\SchemaProviderInterface;

class ActiveRecordSchemaProvider implements SchemaProviderInterface
class DbSchemaProvider implements SchemaProviderInterface
{
public function __construct(
private ConnectionInterface $connection,
private ActiveRecordFactory $activeRecordFactory,
) {
public function __construct(private ConnectionInterface $db)
{
}

public function getTables(): array
{
$quoter = $this->db->getQuoter();
/** @var TableSchemaInterface[] $tableSchemas */
$tableSchemas = $this->connection->getSchema()->getTableSchemas();

$tableSchemas = $this->db->getSchema()->getTableSchemas();
$tables = [];
foreach ($tableSchemas as $schema) {
$activeQuery = $this->activeRecordFactory->createQueryTo(ActiveRecord::class, $schema->getName());

/**
* @var ActiveRecord[] $records
*/
$records = $activeQuery->count();

foreach ($tableSchemas as $schema) {
$tables[] = [
'table' => $schema->getName(),
'table' => $quoter->unquoteSimpleTableName($schema->getName()),
'primaryKeys' => $schema->getPrimaryKey(),
'columns' => $this->serializeARColumnsSchemas($schema->getColumns()),
'records' => $records,
'records' => (new Query($this->db))->from($schema->getName())->count(),
];
}
return $tables;
Expand All @@ -46,20 +37,14 @@ public function getTables(): array
public function getTable(string $tableName): array
{
/** @var TableSchemaInterface[] $tableSchemas */
$schema = $this->connection->getSchema()->getTableSchema($tableName);

$activeQuery = $this->activeRecordFactory->createQueryTo(ActiveRecord::class, $tableName);

/**
* @var ActiveRecord[] $records
*/
$records = $activeQuery->all();

$schema = $this->db->getSchema()->getTableSchema($tableName);
$records = (new Query($this->db))->from($schema->getName())->all();
$data = [];

// TODO: add pagination
foreach ($records as $n => $record) {
foreach ($record->attributes() as $attribute) {
$data[$n][$attribute] = $record->{$attribute};
foreach ($records as $r => $record) {
foreach ($record as $n => $attribute) {
$data[$r][$n] = $attribute;
}
}

Expand Down
Empty file removed tests/.gitkeep
Empty file.
151 changes: 151 additions & 0 deletions tests/Inspector/Database/DbSchemaProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Api\Tests\Inspector\Database;

use PHPUnit\Framework\TestCase;
use Yiisoft\Cache\NullCache;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Sqlite\Connection;
use Yiisoft\Db\Sqlite\Driver;
use Yiisoft\Yii\Debug\Api\Inspector\Database\Db\DbSchemaProvider;

final class DbSchemaProviderTest extends TestCase
{
public function testGetTable(): void
{
$db = $this->createConnection();

// generate tables
$this->generateTables($db);

$dbSchemaProvider = new DbSchemaProvider($db);
$table = $dbSchemaProvider->getTable('test3');

// test table
$this->assertSame('test3', $table['table']);
$this->assertSame(['id'], $table['primaryKeys']);
$this->assertCount(4, $table['columns']);
}

public function testGetTables(): void
{
$db = $this->createConnection();

// generate tables
$this->generateTables($db);

$dbSchemaProvider = new DbSchemaProvider($db);
$tables = $dbSchemaProvider->getTables();

// general tables
$this->assertCount(3, $tables);

// test table
$this->assertSame('test', $tables[0]['table']);
$this->assertSame(['id'], $tables[0]['primaryKeys']);
$this->assertCount(2, $tables[0]['columns']);
$this->assertSame(3, $tables[0]['records']);

// test2 table
$this->assertSame('test2', $tables[1]['table']);
$this->assertSame(['id'], $tables[1]['primaryKeys']);
$this->assertCount(3, $tables[1]['columns']);
$this->assertSame(2, $tables[1]['records']);

// test3 table
$this->assertSame('test3', $tables[2]['table']);
$this->assertSame(['id'], $tables[2]['primaryKeys']);
$this->assertCount(4, $tables[2]['columns']);
$this->assertSame(10, $tables[2]['records']);

$db->close();
}

private function createConnection(): ConnectionInterface
{
return new Connection(new Driver('sqlite::memory:'), new SchemaCache(new NullCache()));
}

private function generateTables(ConnectionInterface $db): void
{
// create tables
$db->createCommand()->createTable(
'test',
[
'id' => 'pk',
'email' => 'string',
]
)->execute();

$db->createCommand()->createTable(
'test2',
[
'id' => 'pk',
'name' => 'string',
'flag' => 'integer',
],
)->execute();

$db->createCommand()->createTable(
'test3',
[
'id' => 'pk',
'product' => 'string',
'price' => 'float',
'status' => 'integer',
],
)->execute();

// insert data
$db->createCommand()->batchInsert(
'test',
[
'id',
'email'
],
[
[1, 'test1'],
[2, 'test2'],
[3, 'test3'],
],
)->execute();

$db->createCommand()->batchInsert(
'test2',
[
'id',
'name',
'flag',
],
[
[1, 'test1', 1],
[2, 'test2', 0],
],
)->execute();

$db->createCommand()->batchInsert(
'test3',
[
'id',
'product',
'price',
'status',
],
[
[1, 'test1', 1.1, 1],
[2, 'test2', 2.2, 0],
[3, 'test3', 3.3, 1],
[4, 'test4', 4.4, 0],
[5, 'test5', 5.5, 1],
[6, 'test6', 6.6, 0],
[7, 'test7', 7.7, 1],
[8, 'test8', 8.8, 0],
[9, 'test9', 9.9, 1],
[10, 'test10', 10.10, 0],
],
)->execute();
}
}

0 comments on commit d0f3c86

Please sign in to comment.