2009年9月28日星期一

Re: [fw-mvc] unit test and set layout

I do not understand. All the tests I run and operate normally.
However, if the controller have "setLayout ( 'other (non default)
layout')" then you have the bug.


2009/9/28 Алексей Шеин <confik@gmail.com>:
>
>
> 2009/9/27 Yuri Timofeev <tim4dev@gmail.com>
>>
>> Hi,
>>
>> 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'
>> ));
>> ...
>> ---
>
> You get this behavior because mvc instance resets on each test, i.e. on
> setUp() calling - you can see it in
> Zend_Test_PHPUnit_ControllerTestCase::reset() method.
> You can fully reboostrap your application, or just some parts (I went this
> way, it's a bit faster).
>
> My own ControllerTestCase class:
>
> <?php
>
> require_once('Zend/Test/PHPUnit/ControllerTestCase.php');
>
> abstract class ControllerTestCase extends
> Zend_Test_PHPUnit_ControllerTestCase
> {
>     /**
>      * @var Zend_Application
>      */
>     static protected $_application = null;
>
>     static $time = 0;
>
>     public function setUp()
>     {
>         $this->bootstrap = array($this, 'appBootstrap');
>         parent::setUp();
>     }
>
>     public function appBootstrap()
>     {
>         if (null === self::$_application) {
>             // init application
>             self::$_application = new Zend_Application(
>                 APPLICATION_ENV,
>                 APPLICATION_PATH . '/configs/application.ini'
>             );
>
>             $classFileIncCache = '/tmp/pluginLoaderCache.php';
>             if (file_exists($classFileIncCache)) {
>                 include_once $classFileIncCache;
>             }
>
> Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);
>
>             self::$_application->bootstrap();
>         }
>
>         $dirs = array(
>             'default' => APPLICATION_PATH . '/controllers',
> // add your modules here
>         );
>
> $this->getFrontController()->getDispatcher()->setControllerDirectory($dirs);
> // controller dirs are reseted too, restore them
>
>         Zend_Layout::startMvc(array('layoutPath' => APPLICATION_PATH .
> '/layouts/scripts')); // restart MVC
>
> //        self::$_application->setBootstrap(null)->bootstrap(); // or
> instead we can fully rebootstrap it again
>     }
> }
>
> ---
>
> tests/bootstrap.php (phpunit calls that one)
>
> <?php
> // Define path to application directory
> defined('APPLICATION_PATH')
>     || define('APPLICATION_PATH', realpath(dirname(__FILE__) .
> '/../application'));
>
> // Define application environment
> defined('APPLICATION_ENV')
>     || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?
> getenv('APPLICATION_ENV') : 'testing'));
>
>
> // Ensure library/ is on include_path
> set_include_path(implode(PATH_SEPARATOR, array(
>     realpath(APPLICATION_PATH . '/../library'),
>     get_include_path()
> )));
>
>
> /** Zend_Application */
> require_once 'Zend/Application.php';
>
> /** Register autoloader */
> require_once 'Zend/Loader/Autoloader.php';
> $autoloader =
> Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
>
> require_once 'ControllerTestCase.php';
>
> --
>
> Hope it helps.
>
> Regards,
> Shein Alexey
>
>
>
>

--
with best regards

没有评论: