|
|
@@ -24,13 +24,14 @@ use Cake\TestSuite\TestCase;
|
|
|
*/
|
|
|
class ConnectionTest extends TestCase {
|
|
|
|
|
|
+ public $fixtures = ['core.thing'];
|
|
|
+
|
|
|
public function setUp() {
|
|
|
parent::setUp();
|
|
|
$this->connection = ConnectionManager::get('test');
|
|
|
}
|
|
|
|
|
|
public function tearDown() {
|
|
|
- $this->connection->execute('DROP TABLE IF EXISTS things');
|
|
|
$this->connection->useSavePoints(false);
|
|
|
unset($this->connection);
|
|
|
parent::tearDown();
|
|
|
@@ -256,35 +257,11 @@ class ConnectionTest extends TestCase {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Auxiliary function to insert a couple rows in a newly created table
|
|
|
- *
|
|
|
- * @return void
|
|
|
- **/
|
|
|
- protected function _insertTwoRecords() {
|
|
|
- $table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
|
|
|
- $this->connection->execute($table);
|
|
|
- $data = ['id' => '1', 'title' => 'a title', 'body' => 'a body'];
|
|
|
- $result = $this->connection->insert(
|
|
|
- 'things',
|
|
|
- $data,
|
|
|
- ['id' => 'integer', 'title' => 'string', 'body' => 'string']
|
|
|
- );
|
|
|
-
|
|
|
- $result->bindValue(1, '2', 'integer');
|
|
|
- $result->bindValue(2, 'another title');
|
|
|
- $result->bindValue(3, 'another body');
|
|
|
- $result->execute();
|
|
|
- $result->closeCursor();
|
|
|
- }
|
|
|
-
|
|
|
-/**
|
|
|
* Tests an statement class can be reused for multiple executions
|
|
|
*
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testStatementReusing() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
-
|
|
|
$total = $this->connection->execute('SELECT COUNT(*) AS total FROM things');
|
|
|
$result = $total->fetch('assoc');
|
|
|
$this->assertEquals(2, $result['total']);
|
|
|
@@ -307,7 +284,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testUpdateWithoutConditionsNorTypes() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
$title = 'changed the title!';
|
|
|
$body = 'changed the body!';
|
|
|
$this->connection->update('things', ['title' => $title, 'body' => $body]);
|
|
|
@@ -322,7 +298,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testUpdateWithConditionsNoTypes() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
$title = 'changed the title!';
|
|
|
$body = 'changed the body!';
|
|
|
$this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2]);
|
|
|
@@ -337,7 +312,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testUpdateWithConditionsCombinedNoTypes() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
$title = 'changed the title!';
|
|
|
$body = 'changed the body!';
|
|
|
$this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2, 'body is not null']);
|
|
|
@@ -352,7 +326,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testUpdateWithTypes() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
$title = 'changed the title!';
|
|
|
$body = new \DateTime('2012-01-01');
|
|
|
$values = compact('title', 'body');
|
|
|
@@ -372,7 +345,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testUpdateWithConditionsAndTypes() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
$title = 'changed the title!';
|
|
|
$body = new \DateTime('2012-01-01');
|
|
|
$values = compact('title', 'body');
|
|
|
@@ -390,7 +362,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testDeleteNoConditions() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
$this->connection->delete('things');
|
|
|
$result = $this->connection->execute('SELECT * FROM things');
|
|
|
$this->assertCount(0, $result);
|
|
|
@@ -401,7 +372,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testDeleteWithConditions() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
$this->connection->delete('things', ['id' => '1-rest-is-ommited'], ['id' => 'integer']);
|
|
|
$result = $this->connection->execute('SELECT * FROM things');
|
|
|
$this->assertCount(1, $result);
|
|
|
@@ -421,7 +391,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testSimpleTransactions() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
$this->connection->begin();
|
|
|
$this->connection->delete('things', ['id' => 1]);
|
|
|
$this->connection->rollback();
|
|
|
@@ -442,8 +411,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testVirtualNestedTrasanction() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
-
|
|
|
//starting 3 virtual transaction
|
|
|
$this->connection->begin();
|
|
|
$this->connection->begin();
|
|
|
@@ -467,8 +434,6 @@ class ConnectionTest extends TestCase {
|
|
|
* @return void
|
|
|
**/
|
|
|
public function testVirtualNestedTrasanction2() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
-
|
|
|
//starting 3 virtual transaction
|
|
|
$this->connection->begin();
|
|
|
$this->connection->begin();
|
|
|
@@ -491,8 +456,6 @@ class ConnectionTest extends TestCase {
|
|
|
**/
|
|
|
|
|
|
public function testVirtualNestedTrasanction3() {
|
|
|
- $this->_insertTwoRecords();
|
|
|
-
|
|
|
//starting 3 virtual transaction
|
|
|
$this->connection->begin();
|
|
|
$this->connection->begin();
|
|
|
@@ -516,7 +479,6 @@ class ConnectionTest extends TestCase {
|
|
|
**/
|
|
|
public function testSavePoints() {
|
|
|
$this->skipIf(!$this->connection->useSavePoints(true));
|
|
|
- $this->_insertTwoRecords();
|
|
|
|
|
|
$this->connection->begin();
|
|
|
$this->connection->delete('things', ['id' => 1]);
|
|
|
@@ -546,8 +508,6 @@ class ConnectionTest extends TestCase {
|
|
|
|
|
|
public function testSavePoints2() {
|
|
|
$this->skipIf(!$this->connection->useSavePoints(true));
|
|
|
- $this->_insertTwoRecords();
|
|
|
-
|
|
|
$this->connection->begin();
|
|
|
$this->connection->delete('things', ['id' => 1]);
|
|
|
|