Browse Source

Merge pull request #3148 from cakephp/3.0-travis-fixes

3.0 travis fixes
Mark Story 12 years ago
parent
commit
02783004d4

+ 20 - 11
src/TestSuite/Fixture/FixtureInjector.php

@@ -16,11 +16,11 @@ namespace Cake\TestSuite\Fixture;
 
 use Cake\TestSuite\Fixture\FixtureManager;
 use Cake\TestSuite\TestCase;
-use \Exception;
-use \PHPUnit_Framework_AssertionFailedError;
-use \PHPUnit_Framework_Test;
-use \PHPUnit_Framework_TestListener;
-use \PHPUnit_Framework_TestSuite;
+use Exception;
+use PHPUnit_Framework_AssertionFailedError;
+use PHPUnit_Framework_Test;
+use PHPUnit_Framework_TestListener;
+use PHPUnit_Framework_TestSuite;
 
 /**
  * Test listener used to inject a fixture manager in all tests that
@@ -36,12 +36,20 @@ class FixtureInjector implements PHPUnit_Framework_TestListener {
 	protected $_fixtureManager;
 
 /**
+ * Holds a reference to the conainer test suite
+ *
+ * @var \PHPUnit_Framework_TestSuite
+ */
+	protected $_first;
+
+/**
  * Constructor. Save internally the reference to the passed fixture manager
  *
  * @param \Cake\TestSuite\Fixture\FixtureManager $manager
  */
 	public function __construct(FixtureManager $manager) {
 		$this->_fixtureManager = $manager;
+		$this->_fixtureManager->shutdown();
 	}
 
 /**
@@ -52,11 +60,8 @@ class FixtureInjector implements PHPUnit_Framework_TestListener {
  * @return void
  */
 	public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
-		foreach ($suite->getIterator() as $test) {
-			if ($test instanceof TestCase) {
-				$this->_fixtureManager->fixturize($test);
-				$test->fixtureManager = $this->_fixtureManager;
-			}
+		if (empty($this->_first)) {
+			$this->_first = $suite;
 		}
 	}
 
@@ -68,7 +73,9 @@ class FixtureInjector implements PHPUnit_Framework_TestListener {
  * @return void
  */
 	public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
-		$this->_fixtureManager->shutdown();
+		if ($this->_first === $suite) {
+			$this->_fixtureManager->shutdown();
+		}
 	}
 
 /**
@@ -122,6 +129,8 @@ class FixtureInjector implements PHPUnit_Framework_TestListener {
  * @return void
  */
 	public function startTest(PHPUnit_Framework_Test $test) {
+		$test->fixtureManager = $this->_fixtureManager;
+		$this->_fixtureManager->fixturize($test);
 		$this->_fixtureManager->load($test);
 	}
 

+ 9 - 7
src/TestSuite/Fixture/FixtureManager.php

@@ -64,7 +64,6 @@ class FixtureManager {
 		if (empty($test->fixtures) || !empty($this->_processed[get_class($test)])) {
 			return;
 		}
-		$test->db = ConnectionManager::get('test', false);
 		if (!is_array($test->fixtures)) {
 			$test->fixtures = array_map('trim', explode(',', $test->fixtures));
 		}
@@ -235,7 +234,9 @@ class FixtureManager {
 				$db = ConnectionManager::get($fixture->connection, false);
 				$db->transactional(function($db) use ($fixtures, $test) {
 					foreach ($fixtures as $fixture) {
-						$this->_setupTable($fixture, $db, $test->dropTables);
+						if (!in_array($db->configName(), (array)$fixture->created)) {
+							$this->_setupTable($fixture, $db, $test->dropTables);
+						}
 						$fixture->truncate($db);
 						$fixture->insert($db);
 					}
@@ -254,7 +255,7 @@ class FixtureManager {
  * @return void
  */
 	public function unload(TestCase $test) {
-		$fixtures = !empty($test->fixtures) ? $test->fixtures : array();
+		$fixtures = !empty($test->fixtures) ? $test->fixtures : [];
 		foreach (array_reverse($fixtures) as $f) {
 			if (isset($this->_loaded[$f])) {
 				$fixture = $this->_loaded[$f];
@@ -283,7 +284,10 @@ class FixtureManager {
 			if (!$db) {
 				$db = ConnectionManager::get($fixture->connection);
 			}
-			$this->_setupTable($fixture, $db, $dropTables);
+
+			if (!in_array($db->configName(), (array)$fixture->created)) {
+				$this->_setupTable($fixture, $db, $dropTables);
+			}
 			$fixture->truncate($db);
 			$fixture->insert($db);
 		} else {
@@ -294,9 +298,6 @@ class FixtureManager {
 /**
  * Drop all fixture tables loaded by this class
  *
- * This will also close the session, as failing to do so will cause
- * fatal errors with database sessions.
- *
  * @return void
  */
 	public function shutDown() {
@@ -306,6 +307,7 @@ class FixtureManager {
 					$db = ConnectionManager::get($ds);
 					$fixture->drop($db);
 				}
+				$fixture->created = [];
 			}
 		}
 	}

+ 2 - 3
src/TestSuite/TestCase.php

@@ -47,13 +47,12 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
 /**
  * Control table create/drops on each test method.
  *
- * Set this to false to avoid tables to be dropped if they already exist
- * between each test method. Tables will still be dropped at the
+ * If true, tables will still be dropped at the
  * end of each test runner execution.
  *
  * @var boolean
  */
-	public $dropTables = true;
+	public $dropTables = false;
 
 /**
  * Configure values to restore at end of test.

+ 1 - 0
tests/TestCase/Console/Command/Task/ModelTaskTest.php

@@ -97,6 +97,7 @@ class ModelTaskTest extends TestCase {
 	public function tearDown() {
 		parent::tearDown();
 		unset($this->Task);
+		$this->fixtureManager->shutDown();
 	}
 
 /**

+ 2 - 0
tests/TestCase/Controller/Component/PaginatorComponentTest.php

@@ -448,6 +448,7 @@ class PaginatorComponentTest extends TestCase {
  * @return void
  */
 	public function testOutOfVeryBigPageNumberGetsClamped() {
+		$this->loadFixtures('Post');
 		$this->request->query = [
 			'page' => '3000000000000000000000000',
 		];
@@ -615,6 +616,7 @@ class PaginatorComponentTest extends TestCase {
  * @return void
  */
 	public function testPaginateMaxLimit() {
+		$this->loadFixtures('Post');
 		$table = TableRegistry::get('PaginatorPosts');
 
 		$settings = [

+ 15 - 14
tests/TestCase/Database/ConnectionTest.php

@@ -119,18 +119,6 @@ class ConnectionTest extends TestCase {
 	}
 
 /**
- * Tests disconnecting from database
- *
- * @return void
- **/
-	public function testDisconnect() {
-		$this->assertTrue($this->connection->connect());
-		$this->assertTrue($this->connection->isConnected());
-		$this->connection->disconnect();
-		$this->assertFalse($this->connection->isConnected());
-	}
-
-/**
  * Tests creation of prepared statements
  *
  * @return void
@@ -159,17 +147,20 @@ class ConnectionTest extends TestCase {
 		$this->assertCount(1, $statement);
 		$result = $statement->fetch();
 		$this->assertEquals([2], $result);
+		$statement->closeCursor();
 
 		$sql = 'SELECT 1 + ? + ? AS total';
 		$statement = $this->connection->execute($sql, [2, 3], array('integer', 'integer'));
 		$this->assertCount(1, $statement);
 		$result = $statement->fetch('assoc');
 		$this->assertEquals(['total' => 6], $result);
+		$statement->closeCursor();
 
 		$sql = 'SELECT 1 + :one + :two AS total';
 		$statement = $this->connection->execute($sql, ['one' => 2, 'two' => 3], array('one' => 'integer', 'two' => 'integer'));
 		$this->assertCount(1, $statement);
 		$result = $statement->fetch('assoc');
+		$statement->closeCursor();
 		$this->assertEquals(['total' => 6], $result);
 	}
 
@@ -182,12 +173,14 @@ class ConnectionTest extends TestCase {
 		$sql = "SELECT ? = '2012-01-01'";
 		$statement = $this->connection->execute($sql, [new \DateTime('2012-01-01')], ['date']);
 		$result = $statement->fetch();
+		$statement->closeCursor();
 		$this->assertTrue((bool)$result[0]);
 
 		$sql = "SELECT ? = '2012-01-01', ? = '2000-01-01 10:10:10', ? = 2";
 		$params = [new \DateTime('2012-01-01 10:10:10'), '2000-01-01 10:10:10', 2.1];
 		$statement = $this->connection->execute($sql, $params, ['date', 'string', 'integer']);
 		$result = $statement->fetch();
+		$statement->closeCursor();
 		$this->assertEquals($result, array_filter($result));
 	}
 
@@ -211,6 +204,7 @@ class ConnectionTest extends TestCase {
 		$sql = 'SELECT 1';
 		$statement = $this->connection->execute($sql);
 		$result = $statement->fetch();
+		$statement->closeCursor();
 		$this->assertCount(1, $result);
 		$this->assertEquals([1], $result);
 	}
@@ -287,8 +281,9 @@ class ConnectionTest extends TestCase {
 		$this->_insertTwoRecords();
 
 		$total = $this->connection->execute('SELECT COUNT(*) AS total FROM things');
-		$total = $total->fetch('assoc');
-		$this->assertEquals(2, $total['total']);
+		$result = $total->fetch('assoc');
+		$this->assertEquals(2, $result['total']);
+		$total->closeCursor();
 
 		$result = $this->connection->execute('SELECT title, body  FROM things');
 		$row = $result->fetch('assoc');
@@ -296,6 +291,7 @@ class ConnectionTest extends TestCase {
 		$this->assertEquals('a body', $row['body']);
 
 		$row = $result->fetch('assoc');
+		$result->closeCursor();
 		$this->assertEquals('another title', $row['title']);
 		$this->assertEquals('another body', $row['body']);
 	}
@@ -312,6 +308,7 @@ class ConnectionTest extends TestCase {
 		$this->connection->update('things', ['title' => $title, 'body' => $body]);
 		$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
 		$this->assertCount(2, $result);
+		$result->closeCursor();
 	}
 
 /**
@@ -326,6 +323,7 @@ class ConnectionTest extends TestCase {
 		$this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2]);
 		$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
 		$this->assertCount(1, $result);
+		$result->closeCursor();
 	}
 
 /**
@@ -340,6 +338,7 @@ class ConnectionTest extends TestCase {
 		$this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2, 'body is not null']);
 		$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
 		$this->assertCount(1, $result);
+		$result->closeCursor();
 	}
 
 /**
@@ -359,6 +358,7 @@ class ConnectionTest extends TestCase {
 		$this->assertEquals('2012-01-01', $row['body']);
 		$row = $result->fetch('assoc');
 		$this->assertEquals('2012-01-01', $row['body']);
+		$result->closeCursor();
 	}
 
 /**
@@ -376,6 +376,7 @@ class ConnectionTest extends TestCase {
 		$this->assertCount(1, $result);
 		$row = $result->fetch('assoc');
 		$this->assertEquals('2012-01-01', $row['body']);
+		$result->closeCursor();
 	}
 
 /**

+ 1 - 0
tests/TestCase/Database/Driver/MysqlTest.php

@@ -17,6 +17,7 @@ namespace Cake\Test\TestCase\Database\Driver;
 use Cake\Core\Configure;
 use Cake\Database\Connection;
 use Cake\Database\Driver\Mysql;
+use Cake\Datasource\ConnectionManager;
 use Cake\TestSuite\TestCase;
 use \PDO;
 

+ 1 - 0
tests/TestCase/Database/Driver/PostgresTest.php

@@ -18,6 +18,7 @@ use Cake\Core\Configure;
 use Cake\Database\Connection;
 use Cake\Database\Driver\Postgres;
 use Cake\Database\Query;
+use Cake\Datasource\ConnectionManager;
 use \PDO;
 
 /**

+ 1 - 1
tests/TestCase/Database/Log/LoggingStatementTest.php

@@ -37,7 +37,7 @@ class LoggingStatementTest extends \Cake\TestSuite\TestCase {
 			->with($this->logicalAnd(
 				$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
 				$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
-				$this->attributeEqualTo('took', 5, 60),
+				$this->attributeEqualTo('took', 5, 200),
 				$this->attributeEqualTo('numRows', 3),
 				$this->attributeEqualTo('params', [])
 			));

+ 3 - 1
tests/TestCase/ORM/CompositeKeysTest.php

@@ -44,7 +44,9 @@ class CompositeKeyTest extends TestCase {
  * @var array
  */
 	public $fixtures = [
-		'core.site_article', 'core.site_author', 'core.site_tag',
+		'core.site_article',
+		'core.site_author',
+		'core.site_tag',
 		'core.site_articles_tag'
 	];
 

+ 1 - 7
tests/test_app/TestApp/Model/Table/PaginatorPostsTable.php

@@ -24,18 +24,12 @@ use Cake\Utility\Hash;
 class PaginatorPostsTable extends Table {
 
 /**
- * table property
- *
- * @var string
- */
-	protected $_table = 'posts';
-
-/**
  * initialize method
  *
  * @return void
  */
 	public function initialize(array $config) {
+		$this->table('posts');
 		$this->belongsTo('PaginatorAuthor', [
 			'foreignKey' => 'author_id'
 		]);