6

I have a Zend Framework project, and want to using unit testing to test it.

In tests folder, I have the phpunit.xml as following;

<phpunit bootstrap="./application/bootstrap.php" colors="true">
<testsuite name="Application Test Suite">
    <directory>./</directory>
</testsuite>

<filter>
    <whitelist>
        <directory suffix=".php">../application/</directory>
        <exclude>
            <directory suffix=".phtml">../application/</directory>
            <file>../application/Bootstrap.php</file>
            <file>../application/controllers/ErrorController.php</file>
        </exclude>
    </whitelist>
</filter>

<logging>
    <log type="coverage-html" target="./log/reprot" charset="UTP-8"
    yui="true" highlight = "true" lowUpoerBound="50" highLowerBound="80"/>
    <log type="textdox" target="./log/testdox.html" />
</logging>

And I have bootstrap.php in /tests/application folder as follows:

    <?php
error_reporting(E_ALL | E_STRICT);

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

require_once 'Zend/Loader/Autoloader.php';

require_once 'controllers/ControllerTestCase.php';
Zend_Loader_Autoloader::getInstance();

when I go to the command line, run the following command

phpunit --configuration phpunit.xml

it throws the exception:

PHP Fatal error:  Uncaught exception 'PHPUnit_Framework_Exception' with message
'Neither "Application Test Suite.php" nor "Application Test Suite.php" could be
opened.' in D:\PHP\php5\PEAR\PHPUnit\Util\Skeleton\Test.php:102
Stack trace:
#0 D:\PHP\php5\PEAR\PHPUnit\TextUI\Command.php(157): PHPUnit_Util_Skeleton_Test-
>__construct('Application Tes...', '')
#1 D:\PHP\php5\PEAR\PHPUnit\TextUI\Command.php(129): PHPUnit_TextUI_Command->run
(Array, true)
#2 D:\PHP\php5\phpunit(53): PHPUnit_TextUI_Command::main()
#3 {main}
  thrown in D:\PHP\php5\PEAR\PHPUnit\Util\Skeleton\Test.php on line 102

How could I fix it?

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80

3 Answers3

7

I was having the same problem with the same setup. From what I understand, more recent versions of PHPUnit require at least one actual test to run properly. After creating a simple test, it worked.

myProject/tests/application/DemoTest.php

<?php
class DemoTest extends ControllerTestCase
{
    public function setUp() {
        parent::setUp();
    }

    public function testCanDoUnitTest() {
        $this->assertTrue(true);
    }
}
originalbryan
  • 1,027
  • 1
  • 13
  • 21
5

Youll notice its throwing the exception because its looking for a file named the same as the name you provided for your test suite. You need to actually write a test suite and then supply the name of that test suite to your config: http://www.phpunit.de/manual/3.2/en/organizing-test-suites.html

prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • I add the following code under the folder which contains bootstrap.phpaddTestSuite('MyApp'); // ... return $suite; } } and it is still the same error –  Apr 25 '11 at 21:54
  • Did you change the name you providede in you XML? This needs to correspond directly to the filename of the test suite class minus the extension. So if the code you just posted is in `MyApp.php` then your xml should look like: `` as opposed to `` – prodigitalson Apr 25 '11 at 23:35
  • yes, I already changed the name in xml file,the error is the same –  Apr 26 '11 at 08:51
  • @ratzip: Its doubtful you have the exact same error. Pleas post the new error message unles it is **exactly** the same. – prodigitalson Apr 26 '11 at 08:54
  • Now, the error gone, but it gives another error, http://stackoverflow.com/questions/5789733/need-help-for-the-zend-unit-test, I am new to zend and phpunit, it really drive me nuts, can you help me on this. –  Apr 26 '11 at 11:42
0

I noticed that you're not using the <testsuites> which contains multiple occurences of <testsuite>.

Here is my phpunit.xml which works fine for Zend Framework projects:

<testsuites>
  <testsuite name="UnitTests">
    <directory>./library</directory>
  </testsuite>
  <testsuite name="ApplicationTests">
    <directory>./application</directory>
  </testsuite>
</testsuites>
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
A.J. Brown
  • 913
  • 7
  • 14