Skip to content

Commit

Permalink
[#14733] - Tests for the DM/Pdo
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Feb 8, 2020
1 parent 9e898e8 commit 91672fa
Show file tree
Hide file tree
Showing 40 changed files with 2,397 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Integration\DM\Pdo\Connection;

use IntegrationTester;
use Phalcon\DM\Pdo\Connection;
use Phalcon\Test\Fixtures\Migrations\Invoices;

use function date;
use function str_replace;
use function uniqid;

class CommitInTransactionRollBackCest
{
/**
* Integration Tests Phalcon\DM\Pdo\Connection :: commit()/inTransaction()
*
* @since 2020-01-25
*/
public function dMPdoConnectionCommitInTransaction(IntegrationTester $I)
{
$I->wantToTest('DM\Pdo\Connection - commit()/inTransaction()');

/** @var Connection $connection */
$connection = $I->getConnection();
(new Invoices($connection));
$connection->beginTransaction();

$I->assertTrue($connection->inTransaction());

$template = 'insert into co_invoices (inv_id, inv_cst_id, inv_status_flag, '
. 'inv_title, inv_total, inv_created_at) values ('
. '%id%, 1, 1, "%title%", %total%, "%now%")';

$sql = str_replace(
[
'%id%',
'%title%',
'%total%',
'%now%',
],
[
2,
uniqid(),
102,
date('Y-m-d H:i:s'),
],
$template
);

$result = $connection->exec($sql);
$I->assertEquals(1, $result);

$connection->commit();

/**
* Committed record
*/
$all = $connection
->fetchOne(
'select * from co_invoices WHERE inv_id = ?',
[
0 => 2,
]
);

$I->assertIsArray($all);
$I->assertEquals(2, $all['inv_id']);
}

/**
* Integration Tests Phalcon\DM\Pdo\Connection :: rollBack()
*
* @since 2020-01-25
*/
public function dMPdoConnectionRollBack(IntegrationTester $I)
{
$I->wantToTest('DM\Pdo\Connection - rollBack()');

/** @var Connection $connection */
$connection = $I->getConnection();
(new Invoices($connection));
$connection->beginTransaction();

$I->assertTrue($connection->inTransaction());

$template = 'insert into co_invoices (inv_id, inv_cst_id, inv_status_flag, '
. 'inv_title, inv_total, inv_created_at) values ('
. '%id%, 1, 1, "%title%", %total%, "%now%")';

$sql = str_replace(
[
'%id%',
'%title%',
'%total%',
'%now%',
],
[
2,
uniqid(),
102,
date('Y-m-d H:i:s'),
],
$template
);

$result = $connection->exec($sql);
$I->assertEquals(1, $result);

/**
* Committed record
*/
$all = $connection
->fetchOne(
'select * from co_invoices WHERE inv_id = ?',
[
0 => 2,
]
);

$connection->rollBack();

$all = $connection
->fetchOne(
'select * from co_invoices WHERE inv_id = ?',
[
0 => 2,
]
);

$I->assertIsArray($all);
$I->assertEmpty($all);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Integration\DM\Pdo\Connection;

use IntegrationTester;
use Phalcon\DM\Pdo\Connection;

class ConnectDisconnectIsConnectedCest
{
/**
* Integration Tests Phalcon\DM\Pdo\Connection ::
* connect()/disconnect()/isConnected()
*
* @since 2020-01-25
*/
public function dMPdoConnectionConnectDisconnectIsConnected(IntegrationTester $I)
{
$I->wantToTest('DM\Pdo\Connection - connect()/disconnect()/isConnected()');

/** @var Connection $connection */
$connection = $I->getConnection();

$I->assertFalse($connection->isConnected());
$connection->connect();
$I->assertTrue($connection->isConnected());
$connection->disconnect();
$I->assertFalse($connection->isConnected());
}

/**
* Integration Tests Phalcon\DM\Pdo\Connection :: connect() - queries
*
* @since 2020-01-25
*/
public function dMPdoConnectionConnectQueries(IntegrationTester $I)
{
$I->wantToTest('DM\Pdo\Connection - connect() - queries');

if ('mysql' === $I->getAdapter()) {
/** @var Connection $connection */
$connection = new Connection(
$I->getDatabaseDsn(),
$I->getDatabaseUsername(),
$I->getDatabasePassword(),
[],
[
'set names big5',
]
);

$I->assertFalse($connection->isConnected());
$result = $connection->fetchOne(
'show variables like "character_set_client"'
);

$I->assertTrue($connection->isConnected());
$expeced = [
'Variable_name' => 'character_set_client',
'Value' => 'big5',
];

$I->assertEquals($expeced, $result);
}
}
}
53 changes: 53 additions & 0 deletions tests/integration/DM/Pdo/Connection/ConstructCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Integration\DM\Pdo\Connection;

use IntegrationTester;
use InvalidArgumentException;
use Phalcon\DM\Pdo\Connection;

class ConstructCest
{
/**
* Integration Tests Phalcon\DM\Pdo\Connection :: __construct()
*
* @since 2020-01-25
*/
public function dMPdoConnectionConstruct(IntegrationTester $I)
{
$I->wantToTest('DM\Pdo\Connection - __construct()');

/** @var Connection $connection */
$connection = $I->getConnection();

$I->assertInstanceOf(Connection::class, $connection);
}

/**
* Integration Tests Phalcon\DM\Pdo\Connection :: __construct() - exception
*
* @since 2020-01-20
*/
public function dMPdoConnectionConstructException(IntegrationTester $I)
{
$I->wantToTest('DM\Pdo\Connection - __construct() - exception');

$I->expectThrowable(
new InvalidArgumentException(
"Driver not supported [random]"
),
function () {
(new Connection('random:some data'));
}
);
}
}
45 changes: 45 additions & 0 deletions tests/integration/DM/Pdo/Connection/DebugInfoCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Integration\DM\Pdo\Connection;

use IntegrationTester;
use PDO;
use Phalcon\DM\Pdo\Connection;

class DebugInfoCest
{
/**
* Integration Tests Phalcon\DM\Pdo\Connection :: __debugInfo()
*
* @since 2020-01-25
*/
public function dMPdoConnectionDebugInfo(IntegrationTester $I)
{
$I->wantToTest('DM\Pdo\Connection - __debugInfo()');

/** @var Connection $connection */
$connection = $I->getConnection();

$expected = [
'arguments' => [
$I->getDatabaseDsn(),
'****',
'****',
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
],
[],
],
];
$I->assertEquals($expected, $connection->__debugInfo());
}
}
43 changes: 43 additions & 0 deletions tests/integration/DM/Pdo/Connection/Decorated/ConstructCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Integration\DM\Pdo\Connection\Decorated;

use IntegrationTester;
use PDO;
use Phalcon\DM\Pdo\Connection\Decorated;
use Phalcon\DM\Pdo\Profiler\Profiler;

class ConstructCest
{
/**
* Integration Tests Phalcon\DM\Pdo\Connection\Decorated :: __construct()
*
* @since 2020-01-25
*/
public function dMPdoConnectionDecoratedConstruct(IntegrationTester $I)
{
$I->wantToTest('DM\Pdo\Connection\Decorated - __construct()');

$connection = new PDO(
$I->getDatabaseDsn(),
$I->getDatabaseUsername(),
$I->getDatabasePassword()
);

$decorated = new Decorated($connection);
$decorated->connect();

$I->assertTrue($decorated->isConnected());
$I->assertInstanceOf(Profiler::class, $decorated->getProfiler());
$I->assertSame($connection, $decorated->getAdapter());
}
}
Loading

0 comments on commit 91672fa

Please sign in to comment.