generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f18857e
commit d0f3c86
Showing
6 changed files
with
186 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |