Created
August 11, 2012 23:24
-
-
Save Raven24/3327785 to your computer and use it in GitHub Desktop.
make cakephp unit testing useable
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
<?php | |
App::import('Lib', 'TiTestFixture'); | |
/** | |
* do some magic to make testing more fun | |
* @author Florian Staudacher | |
*/ | |
abstract class TiTestCase extends CakeTestCase { | |
/** | |
* dummy content, in order for the overwritten _loadFixtures to be called | |
* by the parent class | |
*/ | |
var $fixtures = array("i am not empty"); | |
/** | |
* per default, this class will initialize fixturized tables for all | |
* known models. This will take quite some time, so that behavior can be | |
* disabled by setting this to false. | |
*/ | |
var $initDb; | |
/** | |
* overwrite this sh*t. | |
* ... to always have a nice, clean database for testing | |
*/ | |
function _loadFixtures() { | |
// skip loading fixtures | |
if( $this->initDb === false ) return; | |
$modelClasses = App::objects('model'); | |
$fixtureModels = array(); | |
// get ALL THE MODELS | |
foreach($modelClasses as $model) { | |
App::import('Model', $model); | |
$vars = get_class_vars($model); | |
// every model with a table will be fixturized | |
if( $vars['useTable'] !== false ) { | |
array_push($fixtureModels, $model); | |
} | |
} | |
// generate on-the-fly default fixtures for the models | |
foreach($fixtureModels as $model) { | |
$this->_fixtures['app.'.Inflector::tableize($model)] = new TiTestFixture($this->db); | |
$fix = $this->_fixtures['app.'.Inflector::tableize($model)]; | |
$fix->name = $model; | |
$fix->import = array('model' => $model); | |
$fix->records = array(); | |
$fix->init(); | |
$this->_fixtureClassMap[$model] = 'app.'.Inflector::tableize($model); | |
} | |
//pr($this->_fixtures); | |
//pr($this->_fixtureClassMap); | |
} | |
} | |
?> |
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
<?php | |
/** | |
* do some magic to make testing more fun | |
* @author Florian Staudacher | |
*/ | |
class TiTestFixture extends CakeTestFixture { | |
/** | |
* we have to wait for some vars to be assigned manually, before actually | |
* doing what would be in the constructor | |
*/ | |
function __construct() { | |
// noop | |
} | |
function init() { | |
// copied from the parent::__constructor | |
App::import('Model', 'CakeSchema'); | |
$this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test_suite')); | |
parent::init(); | |
} | |
/** | |
* maps enum fields in the database to strings with a length of 64. | |
* otherwise cakephp's testing framework won't stop complaining... | |
*/ | |
function create(&$db) { | |
foreach($this->fields as $name => &$field) { | |
if( strstr($field['type'], "enum") !== false ) { | |
$field['type'] = 'string'; | |
$field['length'] = 64; | |
} | |
} | |
parent::create($db); | |
} | |
} | |
?> |
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
<?php | |
/** | |
* this file is part of the test suite | |
* @author Florian Staudacher | |
*/ | |
App::import('Lib', 'TiTestCase'); | |
class AppTestCase extends TiTestCase { | |
// do your testing here | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment