Yii: Design-By-Contract with PHPUnit

245 views Asked by At

Why does the call of PHPUnit in the class CTestCase works:

require_once('PHPUnit/Autoload.php');
....

PHPUnit_Framework_Assert::assertNotNull($object);

But if I call it the same way in a file of components directory it shows me:

require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory

Even if I add the same imports as in CTestCase it doesn't work.

require_once('PHPUnit/Util/Filesystem.php'); // workaround for PHPUnit <= 3.6.11
require_once('PHPUnit/Autoload.php');
spl_autoload_unregister('phpunit_autoload');
Yii::registerAutoloader('phpunit_autoload');

It shows me:

require_once(PHPUnit/Util/Filesystem.php): failed to open stream: No such file or directory 

I am using namespaces and want to use design-by-contract.

I really hope someone can help me.

2

There are 2 answers

0
EvilKarter On BEST ANSWER

Thanks to this post I found the solution for my problem. Here is the code to enable Design-By-Contract asserts of PHPUnit in Yii Controllers and Helper Classes.

spl_autoload_unregister(array('YiiBase','autoload'));
require_once('PHPUnit/Autoload.php');
spl_autoload_register(array('YiiBase','autoload'));
1
Chaulagai On

If you surf inside the Frameworks/test/CTestCase.php, then you will find like this,

require_once('PHPUnit/Runner/Version.php');
require_once('PHPUnit/Util/Filesystem.php'); // workaround for PHPUnit <= 3.6.11

spl_autoload_unregister(array('YiiBase','autoload'));
require_once('PHPUnit/Autoload.php');
spl_autoload_register(array('YiiBase','autoload')); // put yii's autoloader at the end

if (in_array('phpunit_autoload', spl_autoload_functions())) { // PHPUnit >= 3.7 'phpunit_autoload' was obsoleted
    spl_autoload_unregister('phpunit_autoload');
    Yii::registerAutoloader('phpunit_autoload');
}

I think, it will help you to understand your problem.