0

We are using Zend Studio (Eclipse), and trying to run PHPUnit on our code. We have recently worked to move code from a '/vendor' source folder into libraries that are included by the IDE. So all our auto completion works, the project builds as expected.

However, our PHPUnit tests do not see the included libraries. The tests fail with an error of "undefined class" at the first instance we use.

The tests worked when the libraries were included in the code itself. So we're trying to figure out how to get them working with the Libraries instead.

CLo
  • 3,650
  • 3
  • 26
  • 44

1 Answers1

0

Looks to me your phpunit configuration is not setup to autoload your libraries. I believe you shouldn't have moved your code from the vendor/ directory to a library/ directory, as this is automatically configured by Composer (since you have a vendor/ directory I assume you also use Composer) so with each update of your "vendor" packages, you now need to copy/paste stuff over.

The beauty of Composer is that within the vendor/ directory it also creates an autoload.php file, which you can use as bootstrap (or include in another bootstrap) file for phpunit.

Example bootstrapping just the vendor packages (if phpunit.xml is in app root):

<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="vendor/autoload.php" haltOnFailure="true" haltOnError="true">
   ...
</phpunit>

Example bootstrapping project bootstrap in src/ (if phpunit.xml is in app root):

<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="src/MyProject/Bootstrap.php" haltOnFailure="true" haltOnError="true">
   ...
</phpunit>  

The bootstrap for this example project might look like the following:

<?php

defined ('APP_ROOT') ||
   define('APP_ROOT', dirname(__DIR__) . '/../');
$paths = array (
   APP_ROOT . '/src',
   APP_ROOT . '/vendor',
   get_include_path(),
);
set_include_path(implode(PATH_SEPARATOR, $paths));

/**
 * @see autoload.php
 */
require_once 'autoload.php';

...
DragonBe
  • 426
  • 2
  • 6
  • It's actually a matter of taking the library code completely out of the project folders. It's kind of like a Gradle project, where you don't have any of your libraries in your actual project files. So my question is, when the actual PHP files for the project are included by the IDE and don't exist in the folder structure, how can I get PHPUnit to recognize them. – CLo May 01 '14 at 17:40