diff --git a/tests/integration/DM/Pdo/Connection/CommitInTransactionRollBackCest.php b/tests/integration/DM/Pdo/Connection/CommitInTransactionRollBackCest.php new file mode 100644 index 00000000000..291a7d82f53 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/CommitInTransactionRollBackCest.php @@ -0,0 +1,143 @@ +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); + } +} diff --git a/tests/integration/DM/Pdo/Connection/ConnectDisconnectIsConnectedCest.php b/tests/integration/DM/Pdo/Connection/ConnectDisconnectIsConnectedCest.php new file mode 100644 index 00000000000..0421fdb5ab1 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/ConnectDisconnectIsConnectedCest.php @@ -0,0 +1,74 @@ +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); + } + } +} diff --git a/tests/integration/DM/Pdo/Connection/ConstructCest.php b/tests/integration/DM/Pdo/Connection/ConstructCest.php new file mode 100644 index 00000000000..c842457d9b9 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/ConstructCest.php @@ -0,0 +1,53 @@ +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')); + } + ); + } +} diff --git a/tests/integration/DM/Pdo/Connection/DebugInfoCest.php b/tests/integration/DM/Pdo/Connection/DebugInfoCest.php new file mode 100644 index 00000000000..169df0c7a75 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/DebugInfoCest.php @@ -0,0 +1,45 @@ +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()); + } +} diff --git a/tests/integration/DM/Pdo/Connection/Decorated/ConstructCest.php b/tests/integration/DM/Pdo/Connection/Decorated/ConstructCest.php new file mode 100644 index 00000000000..b3ee369f77f --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/Decorated/ConstructCest.php @@ -0,0 +1,43 @@ +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()); + } +} diff --git a/tests/integration/DM/Pdo/Connection/Decorated/DisconnectCest.php b/tests/integration/DM/Pdo/Connection/Decorated/DisconnectCest.php new file mode 100644 index 00000000000..3a9e88afac5 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/Decorated/DisconnectCest.php @@ -0,0 +1,46 @@ +wantToTest('DM\Pdo\Connection\Decorated - disconnect()'); + + $I->expectThrowable( + new CannotDisconnect( + "Cannot disconnect a Decorated connection instance" + ), + function () use ($I) { + $connection = new PDO( + $I->getDatabaseDsn(), + $I->getDatabaseUsername(), + $I->getDatabasePassword() + ); + + $decorated = new Decorated($connection); + $decorated->disconnect(); + } + ); + } +} diff --git a/tests/integration/DM/Pdo/Connection/ErrorCodeCest.php b/tests/integration/DM/Pdo/Connection/ErrorCodeCest.php new file mode 100644 index 00000000000..be294753d34 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/ErrorCodeCest.php @@ -0,0 +1,34 @@ +wantToTest('DM\Pdo\Connection - errorCode()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $actual = $connection->errorCode(); + $I->assertEquals('', $actual); + } +} diff --git a/tests/integration/DM/Pdo/Connection/ErrorInfoCest.php b/tests/integration/DM/Pdo/Connection/ErrorInfoCest.php new file mode 100644 index 00000000000..aa79fcd605b --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/ErrorInfoCest.php @@ -0,0 +1,36 @@ +wantToTest('DM\Pdo\Connection - errorInfo()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $actual = $connection->errorInfo(); + $expect = ['', null, null]; + + $I->assertEquals($expect, $actual); + } +} diff --git a/tests/integration/DM/Pdo/Connection/ExecCest.php b/tests/integration/DM/Pdo/Connection/ExecCest.php new file mode 100644 index 00000000000..4bc39dd2105 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/ExecCest.php @@ -0,0 +1,48 @@ +wantToTest('DM\Pdo\Connection - exec()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 4); + $I->assertEquals(1, $result); + + $all = $connection->exec( + 'update co_invoices set inv_total = inv_total + 100' + ); + + $I->assertEquals(4, $all); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchAffectedCest.php b/tests/integration/DM/Pdo/Connection/FetchAffectedCest.php new file mode 100644 index 00000000000..cd5fa2dcf9e --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchAffectedCest.php @@ -0,0 +1,47 @@ +wantToTest('DM\Pdo\Connection - fetchAffected()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 4); + $I->assertEquals(1, $result); + + $all = $connection->fetchAffected( + 'delete from co_invoices' + ); + $I->assertEquals(4, $all); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchAllCest.php b/tests/integration/DM/Pdo/Connection/FetchAllCest.php new file mode 100644 index 00000000000..8a70fa8ae05 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchAllCest.php @@ -0,0 +1,52 @@ +wantToTest('DM\Pdo\Connection - fetchAll()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 4); + $I->assertEquals(1, $result); + + $all = $connection->fetchAll( + 'SELECT * from co_invoices' + ); + $I->assertCount(4, $all); + + $I->assertEquals(1, $all[0]['inv_id']); + $I->assertEquals(2, $all[1]['inv_id']); + $I->assertEquals(3, $all[2]['inv_id']); + $I->assertEquals(4, $all[3]['inv_id']); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchAssocCest.php b/tests/integration/DM/Pdo/Connection/FetchAssocCest.php new file mode 100644 index 00000000000..b974695927c --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchAssocCest.php @@ -0,0 +1,52 @@ +wantToTest('DM\Pdo\Connection - fetchAssoc()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 4); + $I->assertEquals(1, $result); + + $all = $connection->fetchAssoc( + 'SELECT * from co_invoices' + ); + $I->assertCount(4, $all); + + $I->assertEquals(1, $all[1]['inv_id']); + $I->assertEquals(2, $all[2]['inv_id']); + $I->assertEquals(3, $all[3]['inv_id']); + $I->assertEquals(4, $all[4]['inv_id']); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchColumnCest.php b/tests/integration/DM/Pdo/Connection/FetchColumnCest.php new file mode 100644 index 00000000000..5e67ad6d976 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchColumnCest.php @@ -0,0 +1,64 @@ +wantToTest('DM\Pdo\Connection - fetchColumn()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + + $all = $connection->fetchColumn( + 'select * from co_invoices' + ); + + $I->assertIsArray($all); + $I->assertEquals(1, $all[0]); + $I->assertEquals(2, $all[1]); + $I->assertEquals(3, $all[2]); + + $all = $connection->fetchColumn( + 'select * from co_invoices', + [], + 4 + ); + + /** + * Intentionally casting things as an array because I don't want + * drivers to fail based on how many decimals they can hold + */ + $I->assertIsArray($all); + $I->assertEquals(101, (int) $all[0]); + $I->assertEquals(102, (int) $all[1]); + $I->assertEquals(103, (int) $all[2]); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchGroupCest.php b/tests/integration/DM/Pdo/Connection/FetchGroupCest.php new file mode 100644 index 00000000000..5b3e345ccba --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchGroupCest.php @@ -0,0 +1,51 @@ +wantToTest('DM\Pdo\Connection - fetchGroup()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 4); + $I->assertEquals(1, $result); + + $all = $connection->fetchGroup( + 'SELECT inv_status_flag, inv_id, inv_total from co_invoices' + ); + + $I->assertEquals(2, $all[0][0]['inv_id']); + $I->assertEquals(4, $all[0][1]['inv_id']); + $I->assertEquals(1, $all[1][0]['inv_id']); + $I->assertEquals(3, $all[1][1]['inv_id']); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchObjectCest.php b/tests/integration/DM/Pdo/Connection/FetchObjectCest.php new file mode 100644 index 00000000000..82aced8f742 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchObjectCest.php @@ -0,0 +1,82 @@ +wantToTest('DM\Pdo\Connection - fetchObject()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + + $all = $connection->fetchObject( + 'select inv_id, inv_total from co_invoices WHERE inv_id = ?', + [ + 0 => 1, + ] + ); + + $I->assertInstanceOf(stdClass::class, $all); + $I->assertEquals(1, $all->inv_id); + $I->assertEquals(101, $all->inv_total); + } + + /** + * Tests Phalcon\DM\Pdo\Connection :: fetchObject() - ctor + * + * @since 2020-01-25 + */ + public function connectionFetchObjectCtor(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\Connection - fetchObject() - ctor'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + + $all = $connection->fetchObject( + 'select inv_id, inv_total from co_invoices WHERE inv_id = ?', + [ + 0 => 1, + ], + Resultset::class, + [ + 'vader', + ] + ); + + $I->assertInstanceOf(Resultset::class, $all); + $I->assertEquals('vader', $all->calculated); + $I->assertEquals(1, $all->inv_id); + $I->assertEquals(101, $all->inv_total); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchObjectsCest.php b/tests/integration/DM/Pdo/Connection/FetchObjectsCest.php new file mode 100644 index 00000000000..0bed73f7323 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchObjectsCest.php @@ -0,0 +1,107 @@ +wantToTest('DM\Pdo\Connection - fetchObjects()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 4); + $I->assertEquals(1, $result); + + $all = $connection->fetchObjects( + 'SELECT * from co_invoices' + ); + $I->assertCount(4, $all); + + $I->assertInstanceOf(stdClass::class, $all[0]); + $I->assertInstanceOf(stdClass::class, $all[1]); + $I->assertInstanceOf(stdClass::class, $all[2]); + $I->assertInstanceOf(stdClass::class, $all[3]); + + $I->assertEquals(1, $all[0]->inv_id); + $I->assertEquals(2, $all[1]->inv_id); + $I->assertEquals(3, $all[2]->inv_id); + $I->assertEquals(4, $all[3]->inv_id); + } + + /** + * Tests Phalcon\DM\Pdo\Connection :: fetchObjects() - ctor + * + * @since 2020-01-25 + */ + public function connectionFetchObjectsCtor(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\Connection - fetchObjects() - ctor'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 4); + $I->assertEquals(1, $result); + + $all = $connection->fetchObjects( + 'SELECT * from co_invoices', + [], + Resultset::class, + [ + 'darth', + ] + ); + $I->assertCount(4, $all); + + $I->assertInstanceOf(Resultset::class, $all[0]); + $I->assertInstanceOf(Resultset::class, $all[1]); + $I->assertInstanceOf(Resultset::class, $all[2]); + $I->assertInstanceOf(Resultset::class, $all[3]); + + $I->assertEquals(1, $all[0]->inv_id); + $I->assertEquals(2, $all[1]->inv_id); + $I->assertEquals(3, $all[2]->inv_id); + $I->assertEquals(4, $all[3]->inv_id); + + $I->assertEquals('darth', $all[0]->calculated); + $I->assertEquals('darth', $all[1]->calculated); + $I->assertEquals('darth', $all[2]->calculated); + $I->assertEquals('darth', $all[3]->calculated); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchOneCest.php b/tests/integration/DM/Pdo/Connection/FetchOneCest.php new file mode 100644 index 00000000000..2bcfe4d5e16 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchOneCest.php @@ -0,0 +1,164 @@ +wantToTest('DM\Pdo\Connection - fetchOne()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + + $all = $connection->fetchOne( + 'select * from co_invoices WHERE inv_id = ?', + [ + 0 => 1, + ] + ); + + $I->assertIsArray($all); + $I->assertEquals(1, $all['inv_id']); + $I->assertArrayHasKey('inv_id', $all); + $I->assertArrayHasKey('inv_cst_id', $all); + $I->assertArrayHasKey('inv_status_flag', $all); + $I->assertArrayHasKey('inv_title', $all); + $I->assertArrayHasKey('inv_total', $all); + $I->assertArrayHasKey('inv_created_at', $all); + } + + /** + * Tests Phalcon\DM\Pdo\Connection :: fetchOne() - no result + * + * @since 2020-01-25 + */ + public function dMPdoConnectionFetchOneNoResult(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\Connection - fetchOne() - no result'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + + $all = $connection->fetchOne( + 'select * from co_invoices WHERE inv_id = ?', + [ + 0 => 7, + ] + ); + + $I->assertIsArray($all); + $I->assertIsEmpty($all); + } + + /** + * Tests Phalcon\DM\Pdo\Connection :: fetchOne() - bind types + * + * @dataProvider getBindTypes + * @since 2020-01-25 + */ + public function dMPdoConnectionFetchOneBindTypes(IntegrationTester $I, Example $example) + { + $I->wantToTest('DM\Pdo\Connection - fetchOne() - bind types - ' . $example[0]); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1, 'test-1'); + $I->assertEquals(1, $result); + + $all = $connection->fetchOne( + 'select * from co_invoices WHERE ' . $example[1], + $example[2] + ); + + $I->assertIsArray($all); + $I->assertEquals(1, $all['inv_id']); + } + + /** + * @return array + */ + private function getBindTypes(): array + { + return [ + [ + 'numeric', + 'inv_id = ?', + [0 => 1], + ], + [ + 'named', + 'inv_id = :id', + ['id' => 1], + ], + [ + 'named boolean', + 'inv_status_flag = :status', + [ + 'status' => true, + ], + ], + [ + 'named boolean with type', + 'inv_status_flag = :status', + [ + 'status' => [true, PDO::PARAM_BOOL], + ], + ], + [ + 'named null', + 'inv_id = :id AND inv_status_flag IS NOT :status', + [ + 'id' => 1, + 'status' => null, + ], + ], + [ + 'named null with type', + 'inv_id = :id AND inv_status_flag IS NOT :status', + [ + 'id' => [1, PDO::PARAM_INT], + 'status' => [null, PDO::PARAM_NULL], + ], + ], + [ + 'named string', + 'inv_title = :title', + [ + 'title' => 'test-1', + ], + ], + + ]; + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchPairsCest.php b/tests/integration/DM/Pdo/Connection/FetchPairsCest.php new file mode 100644 index 00000000000..5f4a74ec61a --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchPairsCest.php @@ -0,0 +1,56 @@ +wantToTest('DM\Pdo\Connection - fetchPairs()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 2); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 3); + $I->assertEquals(1, $result); + $result = $invoice->insert($connection, 4); + $I->assertEquals(1, $result); + + $all = $connection->fetchPairs( + 'SELECT inv_id, inv_total from co_invoices' + ); + $I->assertCount(4, $all); + + $expected = [ + 1 => 101.00, + 2 => 102.00, + 3 => 103.00, + 4 => 104.00, + ]; + + $I->assertEquals($expected, $all); + } +} diff --git a/tests/integration/DM/Pdo/Connection/FetchValueCest.php b/tests/integration/DM/Pdo/Connection/FetchValueCest.php new file mode 100644 index 00000000000..24027eb3ff5 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/FetchValueCest.php @@ -0,0 +1,44 @@ +wantToTest('DM\Pdo\Connection - fetchValue()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + + $all = $connection->fetchValue( + 'select inv_total from co_invoices WHERE inv_cst_id = ?', + [ + 0 => 1, + ] + ); + $I->assertEquals(101, $all); + } +} diff --git a/tests/integration/DM/Pdo/Connection/GetAdapterCest.php b/tests/integration/DM/Pdo/Connection/GetAdapterCest.php new file mode 100644 index 00000000000..b8c417ae3a5 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/GetAdapterCest.php @@ -0,0 +1,46 @@ +wantToTest('DM\Pdo\Connection - getAdapter()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $I->assertFalse($connection->isConnected()); + + $connection->connect(); + + $I->assertTrue($connection->isConnected()); + $I->assertNotEmpty($connection->getAdapter()); + + $connection->disconnect(); + + $I->assertNotEmpty( + $connection->getAdapter(), + 'getPdo() will re-connect if disconnected' + ); + $I->assertTrue($connection->isConnected()); + } +} diff --git a/tests/integration/DM/Pdo/Connection/GetAvailableDriversCest.php b/tests/integration/DM/Pdo/Connection/GetAvailableDriversCest.php new file mode 100644 index 00000000000..c8176ac962f --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/GetAvailableDriversCest.php @@ -0,0 +1,37 @@ +wantToTest('DM\Pdo\Connection - getAvailableDrivers()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $expected = PDO::getAvailableDrivers(); + $actual = $connection::getAvailableDrivers(); + + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/integration/DM/Pdo/Connection/GetQuoteNamesCest.php b/tests/integration/DM/Pdo/Connection/GetQuoteNamesCest.php new file mode 100644 index 00000000000..dd35514ed44 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/GetQuoteNamesCest.php @@ -0,0 +1,59 @@ +wantToTest('DM\Pdo\Connection - getQuoteNames()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $mysql = [ + "prefix" => '`', + "suffix" => '`', + "find" => '`', + "replace" => '``', + ]; + $sqlsrv = [ + "prefix" => '[', + "suffix" => ']', + "find" => ']', + "replace" => '][', + ]; + $default = [ + "prefix" => '"', + "suffix" => '"', + "find" => '"', + "replace" => '""', + ]; + + $actual = $connection->getQuoteNames('unknown'); + $I->assertEquals($default, $actual); + + $actual = $connection->getQuoteNames('mysql'); + $I->assertEquals($mysql, $actual); + + $actual = $connection->getQuoteNames('sqlsrv'); + $I->assertEquals($sqlsrv, $actual); + } +} diff --git a/tests/integration/DM/Pdo/Connection/GetSetAttributeCest.php b/tests/integration/DM/Pdo/Connection/GetSetAttributeCest.php new file mode 100644 index 00000000000..1daeddc8156 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/GetSetAttributeCest.php @@ -0,0 +1,48 @@ +wantToTest('DM\Pdo\Connection - getAttribute()/setAttribute()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $I->assertEquals( + PDO::ERRMODE_EXCEPTION, + $connection->getAttribute(PDO::ATTR_ERRMODE) + ); + + $connection->setAttribute( + PDO::ATTR_ERRMODE, + PDO::ERRMODE_WARNING + ); + + $I->assertEquals( + PDO::ERRMODE_WARNING, + $connection->getAttribute(PDO::ATTR_ERRMODE) + ); + } +} diff --git a/tests/integration/DM/Pdo/Connection/GetSetProfilerCest.php b/tests/integration/DM/Pdo/Connection/GetSetProfilerCest.php new file mode 100644 index 00000000000..14effd64ef7 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/GetSetProfilerCest.php @@ -0,0 +1,42 @@ +wantToTest('DM\Pdo\Connection - getProfiler()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $I->assertInstanceOf( + Profiler::class, + $connection->getProfiler() + ); + + $profiler = new Profiler(); + $connection->setProfiler($profiler); + + $I->assertSame($profiler, $connection->getProfiler()); + } +} diff --git a/tests/integration/DM/Pdo/Connection/LastInsertIdCest.php b/tests/integration/DM/Pdo/Connection/LastInsertIdCest.php new file mode 100644 index 00000000000..e5d9afab54a --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/LastInsertIdCest.php @@ -0,0 +1,59 @@ +wantToTest('DM\Pdo\Connection - lastInsertId()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $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); + $I->assertEquals(2, $connection->lastInsertId()); + } +} diff --git a/tests/integration/DM/Pdo/Connection/QueryCest.php b/tests/integration/DM/Pdo/Connection/QueryCest.php new file mode 100644 index 00000000000..204620c9da8 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/QueryCest.php @@ -0,0 +1,50 @@ +wantToTest('DM\Pdo\Connection - query()'); + + /** @var Connection $connection */ + $connection = $I->getConnection(); + $invoice = new Invoices($connection); + + $result = $invoice->insert($connection, 1); + $I->assertEquals(1, $result); + + $all = $connection + ->query('select * from co_invoices WHERE inv_id = 1') + ->fetch() + ; + + $I->assertIsArray($all); + $I->assertEquals(1, $all['inv_id']); + $I->assertArrayHasKey('inv_id', $all); + $I->assertArrayHasKey('inv_cst_id', $all); + $I->assertArrayHasKey('inv_status_flag', $all); + $I->assertArrayHasKey('inv_title', $all); + $I->assertArrayHasKey('inv_total', $all); + $I->assertArrayHasKey('inv_created_at', $all); + } +} diff --git a/tests/integration/DM/Pdo/Connection/QuoteCest.php b/tests/integration/DM/Pdo/Connection/QuoteCest.php new file mode 100644 index 00000000000..a49889482a3 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/QuoteCest.php @@ -0,0 +1,45 @@ +getConnection(); + $quotes = $connection->getQuoteNames(); + + $source = 'test'; + $expected = $quotes["prefix"] . $source . $quotes["suffix"]; + $actual = $connection->quote($source); + $I->assertEquals($expected, $actual); + + $source = ['test', 1, true, null]; + $expected = $quotes["prefix"] . 'test' . $quotes["suffix"] . ', ' + . $quotes["prefix"] . '1' . $quotes["suffix"] . ', ' + . $quotes["prefix"] . '1' . $quotes["suffix"] . ', ' + . $quotes["prefix"] . '' . $quotes["suffix"]; + $actual = $connection->quote($source); + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/integration/DM/Pdo/Connection/UnderscoreCallCest.php b/tests/integration/DM/Pdo/Connection/UnderscoreCallCest.php new file mode 100644 index 00000000000..de249e2e6f0 --- /dev/null +++ b/tests/integration/DM/Pdo/Connection/UnderscoreCallCest.php @@ -0,0 +1,62 @@ +wantToTest('DM\Pdo\Connection - __call()'); + + /** @var Connection $connection */ + $connection = new ConnectionFixture( + $I->getDatabaseDsn(), + $I->getDatabaseUsername(), + $I->getDatabasePassword() + ); + + $actual = $connection->callMe('blondie'); + $I->assertEquals('blondie', $actual); + } + + /** + * Integration Tests Phalcon\DM\Pdo\Connection :: __call() - exception + * + * @since 2020-01-25 + */ + public function dMPdoConnectionUnderscoreCallException(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\Connection - __call() - exception'); + + $I->expectThrowable( + new BadMethodCallException( + "Class 'Phalcon\DM\Pdo\Connection' does not have a method 'unknown'" + ), + function () use ($I) { + /** @var Connection $connection */ + $connection = $I->getConnection(); + + $connection->unknown(); + } + ); + } +} diff --git a/tests/integration/DM/Pdo/ConnectionLocator/ConstructCest.php b/tests/integration/DM/Pdo/ConnectionLocator/ConstructCest.php new file mode 100644 index 00000000000..741590a702d --- /dev/null +++ b/tests/integration/DM/Pdo/ConnectionLocator/ConstructCest.php @@ -0,0 +1,38 @@ +wantToTest('DM\Pdo\ConnectionLocator - __construct()'); + + $connection1 = $I->getConnection(); + $locator = new ConnectionLocator( + function () use ($connection1) { + return $connection1; + } + ); + $I->assertInstanceOf(ConnectionLocatorInterface::class, $locator); + $I->assertInstanceOf(ConnectionLocator::class, $locator); + } +} diff --git a/tests/integration/DM/Pdo/ConnectionLocator/GetSetMasterCest.php b/tests/integration/DM/Pdo/ConnectionLocator/GetSetMasterCest.php new file mode 100644 index 00000000000..683ee6d1b14 --- /dev/null +++ b/tests/integration/DM/Pdo/ConnectionLocator/GetSetMasterCest.php @@ -0,0 +1,42 @@ +wantToTest('DM\Pdo\ConnectionLocator - getMaster()/setMaster()'); + + $connection1 = $I->getConnection(); + $connection2 = $I->getConnection(); + $locator = new ConnectionLocator($connection1); + + $actual = $locator->getMaster(); + $I->assertEquals(spl_object_hash($connection1), spl_object_hash($actual)); + + $locator->setMaster($connection2); + $actual = $locator->getMaster(); + $I->assertEquals(spl_object_hash($connection2), spl_object_hash($actual)); + } +} diff --git a/tests/integration/DM/Pdo/ConnectionLocator/GetSetReadCest.php b/tests/integration/DM/Pdo/ConnectionLocator/GetSetReadCest.php new file mode 100644 index 00000000000..4f1b98497b8 --- /dev/null +++ b/tests/integration/DM/Pdo/ConnectionLocator/GetSetReadCest.php @@ -0,0 +1,133 @@ +wantToTest('DM\Pdo\ConnectionLocator - getRead()/setRead()'); + + $master = $I->getConnection(); + $read1 = $I->getConnection(); + $read2 = $I->getConnection(); + $locator = new ConnectionLocator( + $master, + [ + "read1" => function () use ($read1) { + return $read1; + }, + "read2" => function () use ($read2) { + return $read2; + }, + ] + ); + + $actual = $locator->getRead("read1"); + $I->assertEquals(spl_object_hash($read1), spl_object_hash($actual)); + + $actual = $locator->getRead("read2"); + $I->assertEquals(spl_object_hash($read2), spl_object_hash($actual)); + } + + /** + * Integration Tests Phalcon\DM\Pdo\ConnectionLocator :: getRead() - random + * + * @since 2020-01-25 + */ + public function dMPdoConnectionLocatorGetReadRandom(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\ConnectionLocator - getRead() - random'); + + $master = $I->getConnection(); + $read1 = $I->getConnection(); + $read2 = $I->getConnection(); + $locator = new ConnectionLocator( + $master, + [ + "read1" => function () use ($read1) { + return $read1; + }, + "read2" => function () use ($read2) { + return $read2; + }, + ] + ); + + $hashes = [ + spl_object_hash($read1), + spl_object_hash($read2), + ]; + + $actual = $locator->getRead(); + $I->assertTrue(in_array(spl_object_hash($actual), $hashes)); + } + + /** + * Integration Tests Phalcon\DM\Pdo\ConnectionLocator :: getRead() - empty + * + * @since 2020-01-25 + */ + public function dMPdoConnectionLocatorGetReadEmpty(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\ConnectionLocator - getRead() - empty'); + + $master = $I->getConnection(); + $locator = new ConnectionLocator($master); + + $actual = $locator->getRead("read1"); + $I->assertEquals(spl_object_hash($master), spl_object_hash($actual)); + } + + /** + * Integration Tests Phalcon\DM\Pdo\ConnectionLocator :: getRead() - + * exception + * + * @since 2020-01-25 + */ + public function dMPdoConnectionLocatorGetReadException(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\ConnectionLocator - getRead() - exception'); + + $I->expectThrowable( + new ConnectionNotFound( + "Connection not found: read:unknown" + ), + function () use ($I) { + $master = $I->getConnection(); + $read1 = $I->getConnection(); + $locator = new ConnectionLocator( + $master, + [ + "read1" => function () use ($read1) { + return $read1; + }, + ] + ); + + $locator->getRead("unknown"); + } + ); + } +} diff --git a/tests/integration/DM/Pdo/ConnectionLocator/GetSetWriteCest.php b/tests/integration/DM/Pdo/ConnectionLocator/GetSetWriteCest.php new file mode 100644 index 00000000000..1c1dd5c771a --- /dev/null +++ b/tests/integration/DM/Pdo/ConnectionLocator/GetSetWriteCest.php @@ -0,0 +1,137 @@ +wantToTest('DM\Pdo\ConnectionLocator - getWrite()/setWrite()'); + + $master = $I->getConnection(); + $write1 = $I->getConnection(); + $write2 = $I->getConnection(); + $locator = new ConnectionLocator( + $master, + [], + [ + "write1" => function () use ($write1) { + return $write1; + }, + "write2" => function () use ($write2) { + return $write2; + }, + ] + ); + + $actual = $locator->getWrite("write1"); + $I->assertEquals(spl_object_hash($write1), spl_object_hash($actual)); + + $actual = $locator->getWrite("write2"); + $I->assertEquals(spl_object_hash($write2), spl_object_hash($actual)); + } + + /** + * Integration Tests Phalcon\DM\Pdo\ConnectionLocator :: getWrite() - random + * + * @since 2020-01-25 + */ + public function dMPdoConnectionLocatorGetWriteRandom(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\ConnectionLocator - getWrite() - random'); + + $master = $I->getConnection(); + $write1 = $I->getConnection(); + $write2 = $I->getConnection(); + $locator = new ConnectionLocator( + $master, + [], + [ + "write1" => function () use ($write1) { + return $write1; + }, + "write2" => function () use ($write2) { + return $write2; + }, + ] + ); + + $hashes = [ + spl_object_hash($write1), + spl_object_hash($write2), + ]; + + $actual = $locator->getWrite(); + $I->assertTrue(in_array(spl_object_hash($actual), $hashes)); + } + + /** + * Integration Tests Phalcon\DM\Pdo\ConnectionLocator :: getWrite() - empty + * + * @since 2020-01-25 + */ + public function dMPdoConnectionLocatorGetWriteEmpty(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\ConnectionLocator - getWrite() - empty'); + + $master = $I->getConnection(); + $locator = new ConnectionLocator($master); + + $actual = $locator->getWrite("write1"); + $I->assertEquals(spl_object_hash($master), spl_object_hash($actual)); + } + + /** + * Integration Tests Phalcon\DM\Pdo\ConnectionLocator :: getWrite() - + * exception + * + * @since 2020-01-25 + */ + public function dMPdoConnectionLocatorGetWriteException(IntegrationTester $I) + { + $I->wantToTest('DM\Pdo\ConnectionLocator - getWrite() - exception'); + + $I->expectThrowable( + new ConnectionNotFound( + "Connection not found: write:unknown" + ), + function () use ($I) { + $master = $I->getConnection(); + $write1 = $I->getConnection(); + $locator = new ConnectionLocator( + $master, + [], + [ + "write1" => function () use ($write1) { + return $write1; + }, + ] + ); + + $locator->getWrite("unknown"); + } + ); + } +} diff --git a/tests/integration/DM/Pdo/Profiler/MemoryLogger/LevelsCest.php b/tests/integration/DM/Pdo/Profiler/MemoryLogger/LevelsCest.php new file mode 100644 index 00000000000..271e4832729 --- /dev/null +++ b/tests/integration/DM/Pdo/Profiler/MemoryLogger/LevelsCest.php @@ -0,0 +1,68 @@ +wantToTest('DM\Pdo\Profiler\MemoryLogger - ' . $example[0]); + + $logger = new MemoryLogger(); + + $logger->{$example[0]}($example[0] . ' message'); + $expected = [$example[0] . ' message']; + $message = $logger->getMessages(); + + $I->assertEquals($expected, $message); + } + + private function getExamples(): array + { + return [ + [ + 'alert', + ], + [ + 'critical', + ], + [ + 'debug', + ], + [ + 'emergency', + ], + [ + 'error', + ], + [ + 'info', + ], + [ + 'notice', + ], + [ + 'warning', + ], + ]; + } +} diff --git a/tests/integration/DM/Pdo/Profiler/MemoryLogger/LogCest.php b/tests/integration/DM/Pdo/Profiler/MemoryLogger/LogCest.php new file mode 100644 index 00000000000..00177bd741e --- /dev/null +++ b/tests/integration/DM/Pdo/Profiler/MemoryLogger/LogCest.php @@ -0,0 +1,47 @@ +wantToTest('DM\Pdo\Profiler\MemoryLogger - log()'); + + $logger = new MemoryLogger(); + + $message = "{method} ({duration} seconds): {statement} {backtrace}"; + $context = [ + "method" => "f1", + "duration" => "123", + "seconds" => "456", + "statement" => "select", + "backtrace" => "backtrace", + ]; + + $logger->log(LogLevel::INFO, $message, $context); + + $expected = ["f1 (123 seconds): select backtrace"]; + $message = $logger->getMessages(); + + $I->assertEquals($expected, $message); + } +} diff --git a/tests/integration/DM/Pdo/Profiler/Profiler/ConstructCest.php b/tests/integration/DM/Pdo/Profiler/Profiler/ConstructCest.php new file mode 100644 index 00000000000..94f41b0c552 --- /dev/null +++ b/tests/integration/DM/Pdo/Profiler/Profiler/ConstructCest.php @@ -0,0 +1,32 @@ +wantToTest('DM\Pdo\Profiler\Profiler - __construct()'); + + $profiler = new Profiler(); + + $I->assertInstanceOf(Profiler::class, $profiler); + } +} diff --git a/tests/integration/DM/Pdo/Profiler/Profiler/GetLoggerCest.php b/tests/integration/DM/Pdo/Profiler/Profiler/GetLoggerCest.php new file mode 100644 index 00000000000..3f68ebc57bc --- /dev/null +++ b/tests/integration/DM/Pdo/Profiler/Profiler/GetLoggerCest.php @@ -0,0 +1,41 @@ +wantToTest('DM\Pdo\Profiler\Profiler - getLogger()'); + + $profile = new Profiler(); + $logger = $profile->getLogger(); + + $I->assertInstanceOf(MemoryLogger::class, $logger); + + $newLogger = new MemoryLogger(); + $profile = new Profiler($newLogger); + + $logger = $profile->getLogger(); + $I->assertInstanceOf(MemoryLogger::class, $logger); + $I->assertEquals($newLogger, $logger); + } +} diff --git a/tests/integration/DM/Pdo/Profiler/Profiler/GetSetLogFormatCest.php b/tests/integration/DM/Pdo/Profiler/Profiler/GetSetLogFormatCest.php new file mode 100644 index 00000000000..67de55e32e1 --- /dev/null +++ b/tests/integration/DM/Pdo/Profiler/Profiler/GetSetLogFormatCest.php @@ -0,0 +1,44 @@ +wantToTest('DM\Pdo\Profiler\Profiler - getLogFormat()/setLogFormat()'); + + + $profiler = new Profiler(); + + $I->assertEquals( + "{method} ({duration} seconds): {statement} {backtrace}", + $profiler->getLogFormat() + ); + + $format = "{method} ({duration} seconds): {statement}"; + $profiler->setLogFormat($format); + $I->assertEquals( + $format, + $profiler->getLogFormat() + ); + } +} diff --git a/tests/integration/DM/Pdo/Profiler/Profiler/GetSetLogLevelCest.php b/tests/integration/DM/Pdo/Profiler/Profiler/GetSetLogLevelCest.php new file mode 100644 index 00000000000..b727a438f53 --- /dev/null +++ b/tests/integration/DM/Pdo/Profiler/Profiler/GetSetLogLevelCest.php @@ -0,0 +1,43 @@ +wantToTest('DM\Pdo\Profiler\Profiler - getLogLevel()/setLogLevel()'); + + $profiler = new Profiler(); + + $I->assertEquals( + LogLevel::DEBUG, + $profiler->getLogLevel() + ); + + $profiler->setLogLevel(LogLevel::INFO); + $I->assertEquals( + LogLevel::INFO, + $profiler->getLogLevel() + ); + } +} diff --git a/tests/integration/DM/Pdo/Profiler/Profiler/IsSetActiveCest.php b/tests/integration/DM/Pdo/Profiler/Profiler/IsSetActiveCest.php new file mode 100644 index 00000000000..1f734c38448 --- /dev/null +++ b/tests/integration/DM/Pdo/Profiler/Profiler/IsSetActiveCest.php @@ -0,0 +1,36 @@ +wantToTest('DM\Pdo\Profiler\Profiler - isActive()/setActive()'); + + $profiler = new Profiler(); + + $I->assertFalse($profiler->isActive()); + + $profiler->setActive(true); + $I->assertTrue($profiler->isActive()); + } +} diff --git a/tests/integration/DM/Pdo/Profiler/Profiler/StartFinishCest.php b/tests/integration/DM/Pdo/Profiler/Profiler/StartFinishCest.php new file mode 100644 index 00000000000..aaa703addd4 --- /dev/null +++ b/tests/integration/DM/Pdo/Profiler/Profiler/StartFinishCest.php @@ -0,0 +1,47 @@ +wantToTest('DM\Pdo\Profiler\Profiler - start()/finish()'); + + $profiler = new Profiler(); + + $profiler + ->setActive(true) + ->start('my-method') + ; + + sleep(1); + $profiler->finish('select from something', [1 => 2]); + + $logger = $profiler->getLogger(); + $message = $logger->getMessages()[0]; + + $I->assertNotFalse(strpos($message, 'my-method (1.')); + $I->assertNotFalse(strpos($message, 'select from something #0')); + } +}