5

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';
3
  • Post screenshots with your config. Other than that: confluence.jetbrains.com/display/PhpStorm/… Commented Jan 23, 2017 at 22:22
  • Do you have a phpunit.xml.dist file? If so, can you post it? Commented Jan 23, 2017 at 22:27
  • Yes, I already add It to my post Commented Jan 25, 2017 at 7:25

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

I try to do every advised step, but autoload still doesn't work. Why we need to create bootstrap.php?! can we add this path right into xml config to the vendor/autoload.php?!
1

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

1 Comment

When I'm trying this command I'm getting an error: Fatal error: Call to undefined method PHPUnit_Util_Configuration::getSeleniumBro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.