Nesquik27
Nesquik27

Reputation: 254

PHPUnit test error, cannot find class

I'm new in PHPUnit and unit-testing, so I was install PHPUnit and phar via composer and everything had been going fine until I was try to start my simple test. I'm using PhpStorm where I can see all classes were autoload, but when I trying to start my test I got an error:

Fatal error: Class 'PharIo\Manifest\Simple' not found in C:\xampp\htdocs\mydocs\

I don't understand why he is looking for It in folder upper than PHPUnit is exists ?

I was trying to configure autoload section in composer.json and checking settings in phpunit.xml but nothing works.

Add:

I have to reinstall PHPUnit without PharIO, so now I have a little bit of progress, now I have a situation where I can test my class if I make require_once line with a name of the tested class. It looks like:

require_once '../src/Simple.php';

class SimpleTest extends PHPUnit_Framework_TestCase
{

    public function testAdd() {

        $sum = new Simple();

        $this->assertEquals(5, $sum->add(2, 3));

    }

}

So my simple class is:

class Simple {

public function add($a, $b) {

    return (int) $a + (int) $b;

}

}

But, of course, I want to use namespaces. I try to make some changes based on this question: Autoloading classes in PHPUnit using Composer and autoload.php (I was try even use that repo for test, but an error is still exists) but nothing works for me. I was try to edit my autoload section in the composer.json like this

"autoload": {
    "psr-4": {
        "app\\": "src/"
    }

},

But an error is still exists, another words autoload cannot see It. I was create phpunit.xml and phpunit.dist.xml with a same settings

<phpunit
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
        backupGlobals="true"
        backupStaticAttributes="false"
        colors="true"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        processIsolation="false"
        stopOnFailure="false"
        syntaxCheck="false"
        bootstrap="./tests/bootstrap.php">

        <testsuites>
                <testsuite name="The project's test suite">
                        <directory>./tests</directory>
                </testsuite>
        </testsuites>
</phpunit>

and I made tests/bootstrap.php too with

require_once '../vendor/autoload.php';

Upvotes: 5

Views: 12250

Answers (3)

motanelu
motanelu

Reputation: 4025

Composer's autoload relies on configuration located in the vendor/autoload.php file which needs to be loaded at some point in your execution thread. You application already includes this and that's why it works, but the tests use a different entry point so you need to configure it with a file called phpunit.xml.dist.

Assuming your file structure is something like:

app/
src/
tests/
  bootstrap.php <- create it in your test folder
vendor/
...
composer.json
composer.lock
phpunit.xml.dist <- create it if does not exist

You can see the various options here, but for a basic config, you can use this.

File phpunit.xml.dist:

<phpunit
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
    backupGlobals="true"
    backupStaticAttributes="false"
    bootstrap="tests/bootstrap.php">
</phpunit>

File tests/bootstrap.php:

require_once '../vendor/autoload.php';

You should run phpunit from the root.

Upvotes: 2

JorgeeFG
JorgeeFG

Reputation: 5941

I know this is an old question, but maybe you need to do

composer dump-autoload for composer to generate the map of classes.

I wasted 30mins trying to understand why PHPUnit was giving me:

Cannot stub or mock class or interface XXX because it doesn't exists

Upvotes: 8

BVengerov
BVengerov

Reputation: 3007

You should specify the script with autoloading classes.

You can either specify the file with autoloading in XML-file, as suggested in the other answer, or just by specifying --bootstrap option in your command to run tests:

phpunit --bootstrap vendor/autoload.php tests

Upvotes: 1

Related Questions