Skip to content

Commit

Permalink
Merge pull request #3148 from cakephp/3.0-travis-fixes
Browse files Browse the repository at this point in the history
3.0 travis fixes
  • Loading branch information
markstory committed Mar 28, 2014
2 parents 68e9c21 + 1f00a9a commit 0278300
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 44 deletions.
31 changes: 20 additions & 11 deletions src/TestSuite/Fixture/FixtureInjector.php
Expand Up @@ -16,11 +16,11 @@

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
Expand All @@ -35,13 +35,21 @@ 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();
}

/**
Expand All @@ -52,11 +60,8 @@ public function __construct(FixtureManager $manager) {
* @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;
}
}

Expand All @@ -68,7 +73,9 @@ public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
* @return void
*/
public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
$this->_fixtureManager->shutdown();
if ($this->_first === $suite) {
$this->_fixtureManager->shutdown();
}
}

/**
Expand Down Expand Up @@ -122,6 +129,8 @@ public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time
* @return void
*/
public function startTest(PHPUnit_Framework_Test $test) {
$test->fixtureManager = $this->_fixtureManager;
$this->_fixtureManager->fixturize($test);
$this->_fixtureManager->load($test);
}

Expand Down
16 changes: 9 additions & 7 deletions src/TestSuite/Fixture/FixtureManager.php
Expand Up @@ -64,7 +64,6 @@ public function fixturize($test) {
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));
}
Expand Down Expand Up @@ -235,7 +234,9 @@ public function load(TestCase $test) {
$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);
}
Expand All @@ -254,7 +255,7 @@ public function load(TestCase $test) {
* @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];
Expand Down Expand Up @@ -283,7 +284,10 @@ public function loadSingle($name, $db = null, $dropTables = true) {
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 {
Expand All @@ -294,9 +298,6 @@ public function loadSingle($name, $db = null, $dropTables = true) {
/**
* 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() {
Expand All @@ -306,6 +307,7 @@ public function shutDown() {
$db = ConnectionManager::get($ds);
$fixture->drop($db);
}
$fixture->created = [];
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/TestSuite/TestCase.php
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Console/Command/Task/ModelTaskTest.php
Expand Up @@ -97,6 +97,7 @@ protected function _setupOtherMocks() {
public function tearDown() {
parent::tearDown();
unset($this->Task);
$this->fixtureManager->shutDown();
}

/**
Expand Down
Expand Up @@ -448,6 +448,7 @@ public function testOutOfRangePageNumberGetsClamped() {
* @return void
*/
public function testOutOfVeryBigPageNumberGetsClamped() {
$this->loadFixtures('Post');
$this->request->query = [
'page' => '3000000000000000000000000',
];
Expand Down Expand Up @@ -615,6 +616,7 @@ public function testCheckLimit() {
* @return void
*/
public function testPaginateMaxLimit() {
$this->loadFixtures('Post');
$table = TableRegistry::get('PaginatorPosts');

$settings = [
Expand Down
29 changes: 15 additions & 14 deletions tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -118,18 +118,6 @@ public function testWrongCredentials() {
$connection->connect();
}

/**
* 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
*
Expand Down Expand Up @@ -159,17 +147,20 @@ public function testExecuteWithArguments() {
$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);
}

Expand All @@ -182,12 +173,14 @@ public function testExecuteWithArgumentsAndTypes() {
$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));
}

Expand All @@ -211,6 +204,7 @@ public function testExecuteWithNoParams() {
$sql = 'SELECT 1';
$statement = $this->connection->execute($sql);
$result = $statement->fetch();
$statement->closeCursor();
$this->assertCount(1, $result);
$this->assertEquals([1], $result);
}
Expand Down Expand Up @@ -287,15 +281,17 @@ public function testStatementReusing() {
$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');
$this->assertEquals('a title', $row['title']);
$this->assertEquals('a body', $row['body']);

$row = $result->fetch('assoc');
$result->closeCursor();
$this->assertEquals('another title', $row['title']);
$this->assertEquals('another body', $row['body']);
}
Expand All @@ -312,6 +308,7 @@ public function testUpdateWithoutConditionsNorTypes() {
$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();
}

/**
Expand All @@ -326,6 +323,7 @@ public function testUpdateWithConditionsNoTypes() {
$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();
}

/**
Expand All @@ -340,6 +338,7 @@ public function testUpdateWithConditionsCombinedNoTypes() {
$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();
}

/**
Expand All @@ -359,6 +358,7 @@ public function testUpdateWithTypes() {
$this->assertEquals('2012-01-01', $row['body']);
$row = $result->fetch('assoc');
$this->assertEquals('2012-01-01', $row['body']);
$result->closeCursor();
}

/**
Expand All @@ -376,6 +376,7 @@ public function testUpdateWithConditionsAndTypes() {
$this->assertCount(1, $result);
$row = $result->fetch('assoc');
$this->assertEquals('2012-01-01', $row['body']);
$result->closeCursor();
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Database/Driver/MysqlTest.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use \PDO;

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Database/Driver/PostgresTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Database\Connection;
use Cake\Database\Driver\Postgres;
use Cake\Database\Query;
use Cake\Datasource\ConnectionManager;
use \PDO;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Log/LoggingStatementTest.php
Expand Up @@ -37,7 +37,7 @@ public function testExecuteNoParams() {
->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', [])
));
Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase/ORM/CompositeKeysTest.php
Expand Up @@ -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'
];

Expand Down
8 changes: 1 addition & 7 deletions tests/test_app/TestApp/Model/Table/PaginatorPostsTable.php
Expand Up @@ -23,19 +23,13 @@
*/
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'
]);
Expand Down

0 comments on commit 0278300

Please sign in to comment.