Skip to content

Commit

Permalink
Using a fixture instead of manually creating and dropping the table
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 30, 2014
1 parent 77b44d3 commit b83e71a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
42 changes: 42 additions & 0 deletions tests/Fixture/ThingFixture.php
@@ -0,0 +1,42 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class ThingFixture extends TestFixture {

/**
* fields property
*
* @var array
*/
public $fields = array(
'id' => ['type' => 'integer'],
'title' => ['type' => 'string', 'length' => 20],
'body' => ['type' => 'string', 'length' => 50]
);

/**
* records property
*
* @var array
*/
public $records = [
['id' => 1, 'title' => 'a title', 'body' => 'a body'],
['id' => 2, 'title' => 'another title', 'body' => 'another body']
];

}
44 changes: 2 additions & 42 deletions tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -24,13 +24,14 @@
*/
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();
Expand Down Expand Up @@ -255,36 +256,12 @@ public function _testInsertWithPositionalTypes() {
$this->assertEquals($data, $row);
}

/**
* 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']);
Expand All @@ -307,7 +284,6 @@ public function testStatementReusing() {
* @return void
**/
public function testUpdateWithoutConditionsNorTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
$body = 'changed the body!';
$this->connection->update('things', ['title' => $title, 'body' => $body]);
Expand All @@ -322,7 +298,6 @@ public function testUpdateWithoutConditionsNorTypes() {
* @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]);
Expand All @@ -337,7 +312,6 @@ public function testUpdateWithConditionsNoTypes() {
* @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']);
Expand All @@ -352,7 +326,6 @@ public function testUpdateWithConditionsCombinedNoTypes() {
* @return void
**/
public function testUpdateWithTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
$body = new \DateTime('2012-01-01');
$values = compact('title', 'body');
Expand All @@ -372,7 +345,6 @@ public function testUpdateWithTypes() {
* @return void
**/
public function testUpdateWithConditionsAndTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
$body = new \DateTime('2012-01-01');
$values = compact('title', 'body');
Expand All @@ -390,7 +362,6 @@ public function testUpdateWithConditionsAndTypes() {
* @return void
**/
public function testDeleteNoConditions() {
$this->_insertTwoRecords();
$this->connection->delete('things');
$result = $this->connection->execute('SELECT * FROM things');
$this->assertCount(0, $result);
Expand All @@ -401,7 +372,6 @@ public function testDeleteNoConditions() {
* @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);
Expand All @@ -421,7 +391,6 @@ public function testDeleteWithConditions() {
* @return void
**/
public function testSimpleTransactions() {
$this->_insertTwoRecords();
$this->connection->begin();
$this->connection->delete('things', ['id' => 1]);
$this->connection->rollback();
Expand All @@ -442,8 +411,6 @@ public function testSimpleTransactions() {
* @return void
**/
public function testVirtualNestedTrasanction() {
$this->_insertTwoRecords();

//starting 3 virtual transaction
$this->connection->begin();
$this->connection->begin();
Expand All @@ -467,8 +434,6 @@ public function testVirtualNestedTrasanction() {
* @return void
**/
public function testVirtualNestedTrasanction2() {
$this->_insertTwoRecords();

//starting 3 virtual transaction
$this->connection->begin();
$this->connection->begin();
Expand All @@ -491,8 +456,6 @@ public function testVirtualNestedTrasanction2() {
**/

public function testVirtualNestedTrasanction3() {
$this->_insertTwoRecords();

//starting 3 virtual transaction
$this->connection->begin();
$this->connection->begin();
Expand All @@ -516,7 +479,6 @@ public function testVirtualNestedTrasanction3() {
**/
public function testSavePoints() {
$this->skipIf(!$this->connection->useSavePoints(true));
$this->_insertTwoRecords();

$this->connection->begin();
$this->connection->delete('things', ['id' => 1]);
Expand Down Expand Up @@ -546,8 +508,6 @@ public function testSavePoints() {

public function testSavePoints2() {
$this->skipIf(!$this->connection->useSavePoints(true));
$this->_insertTwoRecords();

$this->connection->begin();
$this->connection->delete('things', ['id' => 1]);

Expand Down

0 comments on commit b83e71a

Please sign in to comment.