Pergunta

I asked the same question in another forum, but I don't yet have any luck there. So please allow me to ask the same question here.

I want to setup Zend_Test to test my controller code (I am using PHP, Zend Framework). Everything seems to be correct and according to the official documentation, but I kept getting an error.

For the detailed explanation about the problem and my setup, please refer to the forum post here. Anyone can give me a clue, what's wrong with my setup?

Thanks! Regards, Andree.

Foi útil?

Solução

I've had this problem before when using setFallbackAutoloader(true), which you are, but I've never been able to track down the root cause. If you Google the error you'll find a few ZF bug reports mentioning it.

Can you confirm that you are also using setFallbackAutoloader(true) in your application? If not, then you can remove that line from your TestHelper.php. If you are, then try adding:

$autoLoader->suppressNotFoundWarnings(true);

just after it, this normally fixes the problem for me (but may cause some other issues).

Outras dicas

Check out this Tutorial , and follow it step by step. Works fine for me.

Tutorial PHPUNIT + Zend Framework

I've given a talk on this subject at several conferences, and there's even a webinar on the Zend Website (see links below).

When I look at your setup scripts, you have a lot of clutter in there making it hard to maintain once you add more features to your application.

My TestHelper class only contains the following:

<?php
/*
 * Example test helper script taken from the blog article of Matthew Weier
 * O'Phinney on September 11, 2008
 * 
 * {@link http://weierophinney.net/matthew/archives/190-Setting-up-your-Zend_Test-test-suites.html}
 */

/*
 * Start output buffering
 */
//ob_start();

/*
 * Set error reporting to the level to which code must comply.
 */
error_reporting( E_ALL | E_STRICT );

/*
 * Set default timezone
 */
date_default_timezone_set('Europe/Brussels');

/*
 * Testing environment
 */
if (!defined('APPLICATION_ENV'))
    define('APPLICATION_ENV', 'testing');

/*
 * Determine the root, library, tests, and models directories
 */
$root        = realpath(dirname(__FILE__) . '/../');
$library     = $root . '/library';
$tests       = $root . '/tests';
$models      = $root . '/application/models';
$controllers = $root . '/application/controllers';

/*
 * Set up application and test path constant for easy access helper classes
 */
if (!defined('APPLICATION_PATH'))
    define('APPLICATION_PATH', $root . '/application');
define('TEST_PATH', $tests);

/*
 * Prepend the library/, tests/, and models/ directories to the
 * include_path. This allows the tests to run out of the box.
 */
$localFrameworkPaths = array (
    '/usr/local/zend/share/ZendFramework/library',
    '/opt/ZendFramework',
);
$include_path = get_include_path();
foreach ($localFrameworkPaths as $zfPath) {
    $include_path = str_replace($zfPath . PATH_SEPARATOR, '', $include_path);
}
$path = array(
    APPLICATION_PATH,
    $models,
    $library,
    $tests,
    $include_path,
);
set_include_path(implode(PATH_SEPARATOR, $path));

/**
 * Register autoloader
 */
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

/**
 * Store application root in registry
 */
Zend_Registry::set('testRoot', $root);
Zend_Registry::set('testBootstrap', $root . '/application/bootstrap.php');

/**
 * Say to the sessions we use unit testing here
 */
Zend_Session::$_unitTestEnabled = true;

/*
 * Unset global variables that are no longer needed.
 */
unset($root, $library, $models, $controllers, $tests, $path);

This is similar to the original settings MWOP provided on his blog (see the link on top of the class).

And that's it, no need to worry about adding modules manually or keeping track of changes in your application architecture since this will use your application's own bootstrap.

For more information, do check out these links:

Let me know if it did the job for your case as well.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top