Zend Framework 1.9.2
I am writing a unit test for controller IndexController.php, which use
a different Layout:
---
application/controllers/IndexController.php
---
function indexAction()
{
$this->_helper->layout->setLayout('dashboard');
$this->_helper->actionStack('problem-dashboard', 'job');
$this->_helper->actionStack('problem-dashboard', 'volume');
$this->_helper->actionStack('next-dashboard', 'job');
$this->_helper->actionStack('running-dashboard', 'job');
$this->_helper->actionStack('terminated-dashboard', 'job');
}
---
That part "bootstrap.php" for unit tests:
---
tests/application/bootstrap.php
---
...
Zend_Layout::startMvc(array(
'layoutPath' => $appRoot . '/application/layouts/' . $config_layout->path,
'layout' => 'main'
));
...
---
Here's my test:
---
tests/application/controllers/IndexControllerTest.php
---
public function testIndex() {
$this->dispatch('index/index');
echo $this->response->outputBody(); // for debug !!!
$this->assertResponseCode(200);
$this->assertModule('default');
$this->assertController('index');
$this->assertAction('index');
// other asserts ...
}
---
This test fails. If I turn on the output "echo
$this->response->outputBody();", I see this:
---
---
Action Helper by name Layout not found
<h2>Trace:</h2>
<pre>
#0 /opt/prog/webacula/library/Zend/Controller/Action/HelperBroker.php(293):
Zend_Controller_Action_HelperBroker::_loadHelper('Layout')
#1 /opt/prog/webacula/library/Zend/Controller/Action/HelperBroker.php(339):
Zend_Controller_Action_HelperBroker->getHelper('layout')
#2 /opt/prog/webacula/application/controllers/IndexController.php(48):
Zend_Controller_Action_HelperBroker->__get('layout')
#3 /opt/prog/webacula/library/Zend/Controller/Action.php(513):
IndexController->indexAction()
#4 /opt/prog/webacula/library/Zend/Controller/Dispatcher/Standard.php(289):
Zend_Controller_Action->dispatch('indexAction')
#5 /opt/prog/webacula/library/Zend/Controller/Front.php(946):
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_HttpTestCase),
Object(Zend_Controller_Response_HttpTestCase))
#6 /opt/prog/webacula/library/Zend/Test/PHPUnit/ControllerTestCase.php(190):
Zend_Controller_Front->dispatch()
#7 /opt/prog/webacula/tests/application/controllers/IndexControllerTest.php(29):
Zend_Test_PHPUnit_ControllerTestCase->dispatch('index/index/tes...')
#8 [internal function]: IndexControllerTest->testIndex()
#9 /usr/share/pear/PHPUnit/Framework/TestCase.php(489):
ReflectionMethod->invoke(Object(IndexControllerTest))
#10 /usr/share/pear/PHPUnit/Framework/TestCase.php(404):
PHPUnit_Framework_TestCase->runTest()
#11 /usr/share/pear/PHPUnit/Framework/TestResult.php(607):
PHPUnit_Framework_TestCase->runBare()
#12 /usr/share/pear/PHPUnit/Framework/TestCase.php(375):
PHPUnit_Framework_TestResult->run(Object(IndexControllerTest))
#13 /usr/share/pear/PHPUnit/Framework/TestSuite.php(677):
PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#14 /usr/share/pear/PHPUnit/Framework/TestSuite.php(658):
PHPUnit_Framework_TestSuite->runTest(Object(IndexControllerTest),
Object(PHPUnit_Framework_TestResult))
#15 /usr/share/pear/PHPUnit/Framework/TestSuite.php(621):
PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult),
false, Array, Array)
#16 /usr/share/pear/PHPUnit/Framework/TestSuite.php(621):
PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult),
false, Array, Array)
#17 /usr/share/pear/PHPUnit/TextUI/TestRunner.php(324):
PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult),
false, Array, Array)
#18 /usr/share/pear/PHPUnit/TextUI/Command.php(128):
PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite),
Array)
#19 /usr/bin/phpunit(52): PHPUnit_TextUI_Command::main()
#20 {main}
</pre>
If I modify a test, like this:
---NEW---
tests/application/controllers/IndexControllerTest.php
---
public function testIndex() {
$this->dispatch('index/index/test/1'); // workaround
...
}
---
And new IndexController.php :
---NEW---
application/controllers/IndexController.php
---
function indexAction()
{
// workaround
$unit_test = $this->_request->getParam('test', null);
if ( empty($unit_test)) {
// not test
$this->_helper->layout->setLayout('dashboard');
}
...
$this->_helper->actionStack('problem-dashboard', 'job');
$this->_helper->actionStack('problem-dashboard', 'volume');
$this->_helper->actionStack('next-dashboard', 'job');
$this->_helper->actionStack('running-dashboard', 'job');
$this->_helper->actionStack('terminated-dashboard', 'job');
}
---
That test works OK.
Question: why does not work if I use
"$this->_helper->layout->setLayout('dashboard')" ?
--
with best regards
没有评论:
发表评论