Zend_* classes are autoloaded using Zend_Loader_Autoloader, which by default
doesn't know about your module classes.

To fix this, add a setUp() method in your unit test that sets up the module
autoloader. The most straight-forward way to do this that should get it
working right away is to instantiate Zend_Application_Module_Autoloader:

http://pastie.org/1660038

Another approach would be to set up Zend_Application with your config and
bootstrap it:

http://pastie.org/1660060

This method helps ensure that your PHP environment is as close as possible
to your application's environment and benefits from also bootstrapping any
resources that your model may need.

--
*Hector Virgen*
Sr. Web Developer
http://www.virgentech.com



On Fri, Mar 11, 2011 at 5:45 AM, tonystamp <[email protected]> wrote:

> Hi, finally managed to get phpunit configured all nice and happy with that,
> but i'm having some trouble autoloading my models into my test scripts. I
> think the issue is that i am calling my models with their namespace prefix
> (new Domain_Model_Something) whereas the test scripts have no idea of the
> namespace as it is throwing a file not found on an include statement.
>
> My directory structure is:
> project/
> project/application
> project/application/models
> project/application/controllers (etc)
> project/application/tests
> project/library
> project/httpdocs
>
> Here is my phpunit.xml
> <?xml version="1.0" encoding="UTF-8"?>
>
>
>
>                        ./controllers
>
>
>                        ./models
>
>
>                        ./
>
>
>
>
> ...and the phpunit bootstrap, TestHelper.php;
> <?php
> error_reporting(E_ALL | E_STRICT);
> ini_set('display_errors', 1);
>
> date_default_timezone_set('Europe/London');
>
> define('APPLICATION_ENV', 'testing');
> define('APPLICATION_PATH', realpath(dirname(__FILE__) .
> '/../../application'));
>
> // Directories for include path
> $root = realpath(dirname(__FILE__) . '/../../');
> $library = $root . DIRECTORY_SEPARATOR . 'library';
> $models = $root . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR
> .'models';
>
> $path = array(
>    $library,
>    $models,
>    get_include_path()
> );
>
> set_include_path(implode(PATH_SEPARATOR, $path));
>
> require_once 'Zend/Loader/Autoloader.php';
> $loader = Zend_Loader_Autoloader::getInstance();
>
> // Unset global variables
> unset($root, $library, $models, $path);
>
> And my simple test:
> require_once 'PHPUnit/Framework.php';
>        class News_ArticleTest extends PHPUnit_Framework_TestCase
>        {
>                public function testInstance(){
>                        $news = new Domain_Model_News_Article();
>                        $this->assertInstanceOf('Domain_Model_News_Article',
> $news);
>                }
>        }
>
> Do i need to add the 'Domain' namespace to the autoloader? Testing with
> loading Zend_ classes seems to be ok so i can only assume it has no idea
> what or where a 'Domain_Model_' is?
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Testing-ZF-models-with-PHPUnit-tp3348158p3348158.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

Reply via email to