Skip to content

Instantly share code, notes, and snippets.

@overnin
Last active December 19, 2015 05:59
Show Gist options
  • Save overnin/5907845 to your computer and use it in GitHub Desktop.
Save overnin/5907845 to your computer and use it in GitHub Desktop.
Example of refactoring to support testing in test database
//in ProgramsController
...
function constructClasses()
{
parent::constructClasses();
$this->_instanciateVumiRabbitMQ();
$this->_instanciateMongoModel("vusion"); //this is hard coded at the moment also so no worry about that
}
protected function _instantciateMongoModel(vusionDB)
{
$this->Shortcode = new Shorcode(vusionDB)
}
...
//in TestProgramController
...
class TestProgramsController extends ProgramsController
{
public $autoRender = false;
public function redirect($url, $status = null, $exit = true)
{
$this->redirectUrl = $url;
}
protected function _instanciateVumiRabbitMQ() {
}
protected function _instanciateMongoModel(vusionDB) //u overwrite this function form the ProgramsController
{
$this->Shortcode = new Shortcode("testvusiondb") //hard code here the name of the test database
}
}
@jkage
Copy link

jkage commented Jul 2, 2013

// in the ProgramsController i have

function constructClasses()
{
parent::constructClasses();

    $this->_instanciateVumiRabbitMQ();
    $this->_instanciateMongoModel('vusion');

}

protected function _instanciateMongoModel($vusionDB){
$this->ShortCode = new ShortCode(array('database' => $vusionDB));
}

// In the ProgramsControllerTest i have

class TestProgramsController extends ProgramsController
{

public $autoRender = false;


public function redirect($url, $status = null, $exit = true)
{
    $this->redirectUrl = $url;
}

protected function _instanciateVumiRabbitMQ() {
}

protected function _instanciateMongoModel($vusionDB) {
    $this->ShortCode  = new ShortCode(array('database' => "testdbmongo"));
}

}

public function testIndex_filter()
{
$shortcode1 = array(
'country' => 'uganda',
'shortcode' => 8282,
'international-prefix' => 256
);

    $this->ShortCode->create(); // line 194
   $this->ShortCode->save($shortcode1); // line 195
.......
}

// the stack trace is below

Undefined property: ProgramsControllerTestCase::$ShortCode
Test case: ProgramsControllerTestCase(testIndex_filter)
Stack trace:
/home/jared/Development/vusion-frontend/app/Test/Case/Controller/ProgramsControllerTest.php : 194
ProgramsControllerTestCase::testIndex_filter
/opt/lampp/lib/php/PHPUnit/Framework/TestCase.php : 969
/opt/lampp/lib/php/PHPUnit/Framework/TestCase.php : 824
/opt/lampp/lib/php/PHPUnit/Framework/TestResult.php : 648
/opt/lampp/lib/php/PHPUnit/Framework/TestCase.php : 769
/home/jared/Development/vusion-frontend/lib/Cake/TestSuite/CakeTestCase.php : 78
/opt/lampp/lib/php/PHPUnit/Framework/TestSuite.php : 775
/opt/lampp/lib/php/PHPUnit/Framework/TestSuite.php : 745
/opt/lampp/lib/php/PHPUnit/TextUI/TestRunner.php : 346
/home/jared/Development/vusion-frontend/lib/Cake/TestSuite/CakeTestRunner.php : 56
/home/jared/Development/vusion-frontend/lib/Cake/TestSuite/CakeTestSuiteCommand.php : 110
/home/jared/Development/vusion-frontend/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php : 250
/home/jared/Development/vusion-frontend/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php : 98
/home/jared/Development/vusion-frontend/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php : 115
/home/jared/Development/vusion-frontend/app/webroot/test.php : 92

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment