This is my directory Structure
application
---modules
------admin
---------models
-----------User.php
This is my user Model class
class admin_Model_User
{
//User.php
}
This is my UserTest Class with simple AssertType
class admin_Model_UserTest
extends PHPUnit_Framework_TestCase
{
public function testUserModel()
{
$testUser = new admin_Model_User();
$this->assertType("admin_Model_User",$testUser);
}
}
When I run this. I am getting following Errors
[tests]# phpunit
PHPUnit 3.5.13 by Sebastian Bergmann.
0
Fatal error: Class 'admin_Model_User' not found in /web/zendbase/tests/application/modules/admin/models/UserTest.php on line 18
I know there my must be some path setting. I really could not able to figure out what is really wrong. Looking for help.....
You need to bootstrap Zend in your project's PHPUnit bootstrap.php file. Even though you are testing models and thus don't need the dispatcher, you must still have Zend_Application
load application.ini
and register its autoloader.
You can use Zend_Test_PHPUnit_ControllerTestCase
to do the bootstrapping instead and make sure your model tests run after one of these, but that's a bit hacky.
Another option is to require_once
the model classes manually for each test. The reason this doesn't work automatically via PHPUnit's autoloader is that it doesn't know how to transform the "namespace" admin_Model
into the path admin/models
.
Finally, you could write a simple autoloader to replace the one in PHPUnit. Before converting underscores to slashes, check if the class begins with the "namespace" above and replace it if so.
All I need to do is this
//file:ControllerTestCase.php
<?php
require_once GLOBAL_LIBRARY_PATH. '/Zend/Application.php';
require_once GLOBAL_LIBRARY_PATH. '/Zend/Test/PHPUnit/ControllerTestCase.php';
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
protected $_application;
protected function setUp()
{
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->_application = new Zend_Application(APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->_application->bootstrap();
/**
* Fix for ZF-8193
* http://framework.zend.com/issues/browse/ZF-8193
* Zend_Controller_Action->getInvokeArg('bootstrap') doesn't work
* under the unit testing environment.
*/
$front = Zend_Controller_Front::getInstance();
if($front->getParam('bootstrap') === null) {
$front->setParam('bootstrap', $this->_application->getBootstrap());
}
}
}
// and then require_once it in Bootstrap file.
thats all :) it is working.