/** * Test replace mode. */ public function testReplace() { $last_insert_id = $this->connection->insert('writers', ['id' => 1, 'name' => 'Anton Chekhov', 'birthday' => new DateTime('1860-01-29')], ConnectionInterface::REPLACE); $this->assertEquals(1, $last_insert_id); $this->assertEquals(3, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`')); $this->assertEquals(['id' => 1, 'name' => 'Anton Chekhov', 'birthday' => '1860-01-29'], $this->connection->executeFirstRow('SELECT * FROM `writers` WHERE `id` = ?', $last_insert_id)); }
/** * Test if callback query log callback is working properly. */ public function testQueryLogCallback() { $log = []; $this->connection->onLogQuery(function ($sql, $execution_time) use(&$log) { $log[] = ['sql' => $sql, 'exec_time' => $execution_time]; }); $this->connection->execute('SHOW TABLES LIKE ?', 'my_awesome_table_prefix_%'); $this->assertCount(1, $log); $this->assertNotEmpty($log[0]['exec_time']); $this->assertEquals("SHOW TABLES LIKE 'my_awesome_table_prefix_%'", $log[0]['sql']); }
/** * @throws \ActiveCollab\DatabaseConnection\Exception\QueryException */ public function testExceptionWhenLoadingByObjectClassAndClassNameIsEmpty() { /** @var WriterWithContainer[] $result */ $result = $this->connection->advancedExecute('SELECT * FROM `writers` ORDER BY `id`', null, ConnectionInterface::LOAD_ALL_ROWS, ConnectionInterface::RETURN_OBJECT_BY_CLASS, WriterWithContainer::class, null, $this->container); $this->assertCount(3, $result); foreach ($result as $writer) { $this->assertInstanceOf(WriterWithContainer::class, $writer); $this->assertInstanceOf(ContainerInterface::class, $writer->getContainer()); $this->assertEquals('it works!', $writer->dependency); } }
/** * Test if objects are properly created by value stored in a field. */ public function testExecuteLoadObjectFromField() { // --------------------------------------------------- // Add type field // --------------------------------------------------- $this->connection->execute('ALTER TABLE `writers` ADD `type` VARCHAR(255) NULL DEFAULT ? AFTER `id`;', '\\ActiveCollab\\DatabaseConnection\\Test\\Fixture\\Writer'); $this->assertEquals(['id' => 1, 'type' => '\\ActiveCollab\\DatabaseConnection\\Test\\Fixture\\Writer', 'name' => 'Leo Tolstoy', 'birthday' => '1828-09-09'], $this->connection->executeFirstRow('SELECT * FROM `writers` WHERE `id` = ?', 1)); // --------------------------------------------------- // Use type field to know which objects to create // --------------------------------------------------- $result = $this->connection->advancedExecute('SELECT * FROM `writers` ORDER BY `id`', null, ConnectionInterface::LOAD_ALL_ROWS, ConnectionInterface::RETURN_OBJECT_BY_FIELD, 'type'); $this->assertInstanceOf('\\ActiveCollab\\DatabaseConnection\\Result\\Result', $result); $this->assertCount(3, $result); /** @var \ActiveCollab\DatabaseConnection\Test\Fixture\Writer[] $writers */ $writers = []; foreach ($result as $row) { $writers[] = $row; } $this->assertCount(3, $writers); $this->assertInstanceOf('ActiveCollab\\DatabaseConnection\\Test\\Fixture\\Writer', $writers[0]); $this->assertEquals(1, $writers[0]->getId()); $this->assertEquals('Leo Tolstoy', $writers[0]->getName()); $this->assertEquals('1828-09-09', $writers[0]->getBirthday()->format('Y-m-d')); $this->assertInstanceOf('ActiveCollab\\DatabaseConnection\\Test\\Fixture\\Writer', $writers[1]); $this->assertEquals(2, $writers[1]->getId()); $this->assertEquals('Alexander Pushkin', $writers[1]->getName()); $this->assertEquals('1799-06-06', $writers[1]->getBirthday()->format('Y-m-d')); $this->assertInstanceOf('ActiveCollab\\DatabaseConnection\\Test\\Fixture\\Writer', $writers[2]); $this->assertEquals(3, $writers[2]->getId()); $this->assertEquals('Fyodor Dostoyevsky', $writers[2]->getName()); $this->assertEquals('1821-11-11', $writers[2]->getBirthday()->format('Y-m-d')); }
/** * Test if rows can't be inserted escaped once batch is done. * * @expectedException \RuntimeException */ public function testRowsCantBeInsertedEscapedOnceDone() { $batch_insert = $this->connection->batchInsert('writers', ['name', 'birthday'], 350); $batch_insert->insert('Leo Tolstoy', new DateTime('1828-09-09')); $batch_insert->insert('Alexander Pushkin', new DateTime('1799-06-06')); $this->assertEquals(0, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`')); $batch_insert->done(); $this->assertEquals(2, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`')); $batch_insert->insertEscaped($this->connection->escapeValue('Fyodor Dostoyevsky'), $this->connection->escapeValue(new DateTime('1821-11-11'))); }
/** * Test error in transcation callback. */ public function testErrorInTransactionCallback() { $this->assertEquals(3, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`')); /** @var Exception $exception_caught */ $exception_caught = false; $this->connection->transact(function () { $this->connection->execute('INSERT INTO `writers` (`name`, `birthday`) VALUES (?, ?)', 'Anton Chekhov', new DateTime('1860-01-29')); throw new RuntimeException('Throwing an exception here'); }, null, function (Exception $e) use(&$exception_caught) { $exception_caught = $e; }); $this->assertInstanceOf('\\RuntimeException', $exception_caught); $this->assertEquals('Throwing an exception here', $exception_caught->getMessage()); $this->assertEquals(3, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`')); }
/** * @expectedException \InvalidArgumentException */ public function testExceptionDueToInvalidConditions() { $this->connection->delete('writers', 123); }
/** * Test prepare partial. */ public function testPreparePartial() { $this->assertEquals("WHERE id IN ('1','2','3') AND NOT id = '12'", $this->connection->prepare('WHERE id IN ? AND NOT id = ?', [1, 2, 3], 12)); }
/** * Test escape table name. */ public function testEscapeTableName() { $this->assertEquals('`users`', $this->connection->escapeFieldName('users')); }
/** * Test execute first column. */ public function testExecuteFirstColumn() { $this->assertEquals(['Alexander Pushkin', 'Fyodor Dostoyevsky', 'Leo Tolstoy'], $this->connection->executeFirstColumn('SELECT `name` FROM `writers` ORDER BY `name`')); $this->assertEquals(['1799-06-06', '1821-11-11', '1828-09-09'], $this->connection->executeFirstColumn('SELECT `birthday` FROM `writers` ORDER BY `name`')); }
/** * Test count by field. */ public function testCountField() { $this->assertSame(3, $this->connection->count('writers', null, 'name')); }