Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a test suite decorator to repeat tests for some provided
permutations. Using the permutaitons decorator to run database tests with and without auto indentifiers quoting
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |