Browse Source

Using a fixture instead of manually creating and dropping the table

Jose Lorenzo Rodriguez 12 years ago
parent
commit
b83e71aa86
2 changed files with 44 additions and 42 deletions
  1. 42 0
      tests/Fixture/ThingFixture.php
  2. 2 42
      tests/TestCase/Database/ConnectionTest.php

+ 42 - 0
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']
+	];
+
+}

+ 2 - 42
tests/TestCase/Database/ConnectionTest.php

@@ -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]);