Zend Framework: getting started with Zend_Test

Has anyone managed to configure Zend_Test? What was your method / approach and how do you execute your tests / test suites?

PHPUnit is already installed and working for me. Now I'm trying to write some simple controller tests. The Zend Framework documentation assumes startup is configured, which I haven't done yet. What method do you use to automatically download the appropriate files? I do this in my regular bootstrap file, but I don't want to clutter up my test with a bunch of inclusions and path settings. Will the abstract controller test case class be the way to go?

What about a bootstrap plugin like documentation ... is this how you load your tests, or do you like to do it differently? I would like to reuse as much of a regular bootstrap file as possible. How should I put my loading tray in for testing and normal use?

Here is my test:

<?php class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase { public function setUp() { $this->bootstrap = array($this, 'appBootstrap'); parent::setUp(); } public function appBootstrap() { $this->frontController->registerPlugin(new Bootstrapper('testing')); } public function testIndexAction() { $this->dispatch('/index'); $this->assertController('index'); $this->assertAction('index'); } } //straight from the documentation class Bootstrapper extends Zend_Controller_Plugin_Abstract { /** * @var Zend_Config */ protected static $_config; /** * @var string Current environment */ protected $_env; /** * @var Zend_Controller_Front */ protected $_front; /** * @var string Path to application root */ protected $_root; /** * Constructor * * Initialize environment, root path, and configuration. * * @param string $env * @param string|null $root * @return void */ public function __construct($env, $root = null) { $this->_setEnv($env); if (null === $root) { $root = realpath(dirname(__FILE__) . '/../../../'); } $this->_root = $root; $this->initPhpConfig(); $this->_front = Zend_Controller_Front::getInstance(); } /** * Route startup * * @return void */ public function routeStartup(Zend_Controller_Request_Abstract $request) { $this->initDb(); $this->initHelpers(); $this->initView(); $this->initPlugins(); $this->initRoutes(); $this->initControllers(); } // definition of methods would follow... } 

What should I do?

+4
source share
3 answers

Here is what I did to make it work:

First, we need to solve the startup problem. We will do this by creating a file in which all tests will be included, and place it in the test directory. Note. I almost completely copied all of this from my /public/index.php.

 # /tests/loader.php <?php define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/')); set_include_path( APPLICATION_PATH . '/../library' . PATH_SEPARATOR . APPLICATION_PATH . '/models' . PATH_SEPARATOR . APPLICATION_PATH . '/forms' . PATH_SEPARATOR . get_include_path() ); require_once "Zend/Loader.php"; Zend_Loader::registerAutoload(); 

Secondly, we need to include this file in our test. Our test file is located in / tests / application / controller /. I will not use my boot file as a plugin, since my boot file works the same as the QuickStart Tutorial , I will just contact it by setting the location as the public $ bootstrap. When Zend_Test_PHPUnit_ControllerTestCase constructed, it will look for the boot file that we installed here.

 <?php require_once '../../loader.php'; class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase { public $bootstrap = '../../../application/bootstrap.php'; public function testIndexAction() { $this->dispatch('/index'); $this->assertController('index'); $this->assertAction('index'); } } 

And this! If my boot file was already a plugin, it might be a little more complicated, but since it is not, it is very simple.

+3
source

Instead of using

 require_once "Zend/Loader.php"; Zend_Loader::registerAutoload(); 

change it to

 require_once 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance(); 

registerAutoLoad () deprecated from 1.8.0

+1
source

I always use simple TestHelper.php, which does some basic initialization things. This file is included in each test file file. One of the things I do is registering the Zend Framework autoloader, as I am having serious dependency problems, especially when using filters, validators, and forms. It is almost impossible to track all the necessary files and manually include them in the test files.

You can transfer the startup startup and setting of the included paths to your bootstrap plugin, as this procedure should be the same for a real application.

0
source

All Articles