Skip to content

Commit

Permalink
Adding a test suite decorator to repeat tests for some provided
Browse files Browse the repository at this point in the history
permutations. Using the permutaitons decorator to run database tests
with and without auto indentifiers quoting
  • Loading branch information
lorenzo committed Nov 6, 2013
1 parent 3be0531 commit be47e0f
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Cake/Test/TestCase/DatabaseSuite.php
@@ -0,0 +1,53 @@
<?php
/**
* PHP Version 5.4
*
* 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 CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase;

use Cake\Database\ConnectionManager;
use Cake\TestSuite\TestSuite;
use Cake\TestSuite\TestPermutationDecorator;

/**
* All tests related to database
*
*/
class DatabaseSuite extends TestSuite {

/**
* Returns a suite containing all tests requiring a database connection,
* tests are decorated so that they are run once with automatic
*
* @return void
*/
public static function suite() {
$suite = new TestSuite('Database related tests');
$suite->addTestDirectoryRecursive(__DIR__ . DS . 'Database');
$suite->addTestDirectoryRecursive(__DIR__ . DS . 'ORM');

$permutations = [
'Identifier Quoting' => function() {
ConnectionManager::get('test')->driver()->autoQuoting(true);
},
'No identifier quoting' => function() {
ConnectionManager::get('test')->driver()->autoQuoting(false);
}
];

$suite = new TestPermutationDecorator($suite, $permutations);
return $suite;
}

}
86 changes: 86 additions & 0 deletions Cake/TestSuite/TestPermutationDecorator.php
@@ -0,0 +1,86 @@
<?php
/**
* PHP Version 5.4
*
* 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 CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\TestSuite;

use \PHPUnit_Extensions_TestDecorator;
use \PHPUnit_Framework_TestResult;
use \PHPUnit_Framework_Test;

/**
* A decorator class used to run a test case once for each permutation defined
* in the decorated test suite
*
*/
class TestPermutationDecorator extends PHPUnit_Extensions_TestDecorator {

/**
* An array containing callable methods that will be executed before the test
* suite is run
*
* @var array
*/
protected $_permutations = [];

/**
* Constructor
*
* @param \PHPUnit_Framework_Test The test or suite to decorate
* @param array $permutation An array containing callable methods that will
* be executed before the test suite is run
* @return void
*/
public function __construct(PHPUnit_Framework_Test $test, array $permutations) {
parent::__construct($test);
$this->_permutations = $permutations;
}

/**
* Returns the count of single test methods that this decorator contains, taking in
* consideration the possible permutations
*
* @return void
*/
public function count() {
return count($this->_permutations) + $this->test->count();
}

/**
* Runs each of the test methods for this test suite for each of the provided
* permutations.
*
* @param PHPUnit_Framework_TestResult $result
* @return PHPUnit_Framework_TestResult
*/
public function run(PHPUnit_Framework_TestResult $result = null) {
if ($result === NULL) {
$result = $this->createResult();
}

foreach ($this->_permutations as $permutation) {
if ($result->shouldStop()) {
break;
}
if (is_callable($permutation)) {
$permutation($this->test);
}
$this->test->run($result);
}

return $result;
}

}

0 comments on commit be47e0f

Please sign in to comment.