Skip to content

Commit

Permalink
Remove src\TestSupport from yiisoft\db.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 19, 2022
1 parent 2ae89ee commit 30411a8
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 401 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -23,7 +23,7 @@
"ext-mbstring": "*",
"ext-pdo": "*",
"yiisoft/arrays": "^2.0",
"yiisoft/db": "^3.0@dev",
"yiisoft/db": "dev-remove-test-support as 3.0@dev",
"yiisoft/strings": "^2.0"
},
"require-dev": {
Expand Down
2 changes: 2 additions & 0 deletions tests/BatchQueryResultTest.php
Expand Up @@ -9,6 +9,8 @@

/**
* @group sqlite
*
* @psalm-suppress PropertyNotSetInConstructor
*/
final class BatchQueryResultTest extends CommonBatchQueryResultTest
{
Expand Down
45 changes: 45 additions & 0 deletions tests/Builder/InconditionBuilderTest.php
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Sqlite\Tests\Builder;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
use Yiisoft\Db\Sqlite\Builder\InConditionBuilder;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;

/**
* @group sqlite
*/
final class InconditionBuilderTest extends TestCase
{
use TestTrait;

/**
* @throws Exception
* @throws InvalidArgumentException
* @throws InvalidConfigException
*/
public function testBuildSubqueryInCondition(): void
{
$db = $this->getConnection();
$inCondition = new InCondition(
['id'],
'in',
(new Query($db))->select('id')->from('users')->where(['active' => 1]),
);

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage(
'Yiisoft\Db\Sqlite\Builder\InConditionBuilder::buildSubqueryInCondition is not supported by SQLite.'
);

(new InConditionBuilder($db->getQueryBuilder()))->build($inCondition);
}
}
2 changes: 2 additions & 0 deletions tests/CommandPDOTest.php
Expand Up @@ -9,6 +9,8 @@

/**
* @group sqlite
*
* @psalm-suppress PropertyNotSetInConstructor
*/
final class CommandPDOTest extends CommonCommandPDOTest
{
Expand Down
18 changes: 16 additions & 2 deletions tests/CommandTest.php
Expand Up @@ -17,7 +17,6 @@
/**
* @group sqlite
*
* @psalm-suppress MixedMethodCall
* @psalm-suppress PropertyNotSetInConstructor
*/
final class CommandTest extends CommonCommandTest
Expand Down Expand Up @@ -84,6 +83,9 @@ public function testAddDefaultValue(): void

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::addForeignKey()
*
* @throws Exception
* @throws Throwable
*/
public function testAddForeignKey(
string $name,
Expand All @@ -100,6 +102,9 @@ public function testAddForeignKey(

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::addPrimaryKey()
*
* @throws Exception
* @throws Throwable
*/
public function testAddPrimaryKey(string $name, string $tableName, array|string $column): void
{
Expand All @@ -111,6 +116,9 @@ public function testAddPrimaryKey(string $name, string $tableName, array|string

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::addUnique()
*
* @throws Exception
* @throws Throwable
*/
public function testAddUnique(string $name, string $tableName, array|string $column): void
{
Expand Down Expand Up @@ -139,6 +147,9 @@ public function testAlterColumn(): void

/**
* @dataProvider \Yiisoft\Db\Sqlite\Tests\Provider\CommandProvider::batchInsert()
*
* @throws Exception
* @throws Throwable
*/
public function testBatchInsert(
string $table,
Expand Down Expand Up @@ -443,6 +454,9 @@ public function testTruncateTable(): void

/**
* @dataProvider \Yiisoft\Db\Sqlite\Tests\Provider\CommandProvider::update()
*
* @throws Exception
* @throws Throwable
*/
public function testUpdate(
string $table,
Expand All @@ -458,7 +472,7 @@ public function testUpdate(
* @dataProvider \Yiisoft\Db\Sqlite\Tests\Provider\CommandProvider::upsert()
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testUpsert(array $firstData, array $secondData): void
{
Expand Down
113 changes: 113 additions & 0 deletions tests/ConnectionPDOTest.php
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Sqlite\Tests;

use Throwable;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonConnectionPDOTest;
use Yiisoft\Db\Transaction\TransactionInterface;

/**
* @group pgsql
*
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ConnectionPDOTest extends CommonConnectionPDOTest
{
use TestTrait;

/**
* Ensure database connection is reset on when a connection is cloned.
*
* Make sure each connection element has its own PDO instance i.e. own connection to the DB.
* Also, transaction elements should not be shared between two connections.
*
* @throws Exception
* @throws Throwable
*/
public function testClone(): void
{
$this->setDsn('sqlite:' . __DIR__ . '/Support/Runtime/yiitest.sq3');

$db = $this->getConnection();

$this->assertNull($db->getTransaction());
$this->assertNull($db->getPDO());

$db->open();

$this->assertNull($db->getTransaction());
$this->assertNotNull($db->getPDO());

$conn2 = clone $db;

$this->assertNull($db->getTransaction());
$this->assertNotNull($db->getPDO());

$this->assertNull($conn2->getTransaction());
$this->assertNull($conn2->getPDO());

$db->beginTransaction();

$this->assertNotNull($db->getTransaction());
$this->assertNotNull($db->getPDO());

$this->assertNull($conn2->getTransaction());
$this->assertNull($conn2->getPDO());

$conn3 = clone $db;

$this->assertNotNull($db->getTransaction());
$this->assertNotNull($db->getPDO());
$this->assertNull($conn3->getTransaction());
$this->assertNull($conn3->getPDO());
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws InvalidCallException
* @throws Throwable
*/
public function testGetLastInsertID(): void
{
$db = $this->getConnection(true);

$command = $db->createCommand();
$command->insert(
'customer',
[
'name' => 'Some {{weird}} name',
'email' => 'test@example.com',
'address' => 'Some {{%weird}} address',
]
)->execute();

$this->assertSame('4', $db->getLastInsertID());

$db->close();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testTransactionIsolationException(): void
{
$db = $this->getConnection();

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage(
'Yiisoft\Db\Sqlite\TransactionPDO only supports transaction isolation levels READ UNCOMMITTED and SERIALIZABLE.'
);

$db->beginTransaction(TransactionInterface::READ_COMMITTED);
}
}

0 comments on commit 30411a8

Please sign in to comment.