16

I am playing with php 7 and phpunit 6. Here is the test I wrote:

<?php declare(strict_types=1);

namespace Test;

use DesignPatterns\Observer\User;
use DesignPatterns\Observer\UserObserver;
use PHPUnit\Framework\TestCase;

class ObserverTest extends TestCase
{
    public function testChangeInUserLeadsToUserObserverBeingNotified()
    {
        $observer = new UserObserver();

        $user = new User();
        $user->attach($observer);

        $user->changeEmail('[email protected]');
        $this->assertCount(1, $observer->getChangedUsers());
    }
}

When I tried to run this test, I got the following error message:

PHP Fatal error:  Class 'PHPUnit\Framework\TestCase' not found in /home/.../.../Test/ObserverTest.php on line 9

I installed PHPUnit with composer, here is my composer.json file content:

{
    "require": {
        "phpunit/phpunit": "^6.0"
    },
    "autoload": {
        "psr-4": {"DesignPatterns\\": "src/"}
    }
}

According to PHPUnit 6 documentation, your tests are now supposed to extends PHPUnit\Framework\TestCase instead of PHPUnit_Framework_TestCase.

I know it's not an issue with autoloading. Actually, if I replace PHPUnit\Framework\TestCase with PHPUnit_Framework_TestCase, it works just fine, but I was wondering why this syntax didn't works.

I tried some research on google, stackoverflow and PHPUnit's github repository, but couldn't find anything.

I am looking forward for your answers,

EDIT

This is how my files are organized:

src/
├── DataMapper
│   ├── StorageAdapter.php
│   ├── UserMapper.php
│   └── User.php
├── Observer
│   ├── UserObserver.php
│   └── User.php
Test/
├── DataMapperTest.php
└── ObserverTest.php
7
  • go in vendor and look if phpunit is there or not Commented Feb 14, 2017 at 10:06
  • the test file is in the src folder? if yes it should have the DesignPatterns suffix in the namespace? Commented Feb 14, 2017 at 10:21
  • Hi, yes, phpunit is there. Commented Feb 14, 2017 at 10:30
  • no, the test file is not in src/, it's in a Test folder Commented Feb 14, 2017 at 10:31
  • how do you load the test folder in the autoloader? Commented Feb 14, 2017 at 10:43

4 Answers 4

18

I found the answer:

I was excuting my test with this command line:

phpunit Test/ObserverTest.php

PHPUnit is installed globally on my computer, but it's the 5.1.3 version:

phpunit -v

PHPUnit 5.1.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.13-0ubuntu0.16.04.1 with Xdebug 2.4.0
Configuration: /home/.../.../DesignPatterns/phpunit.xml

And the syntax PHPUnit\Framework\TestCase only works with PHPUnit 6

Now, if I run php vendor/bin/phpunit Test/ObserverTest.php, it works perfectly...

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

1 Comment

Just to confirm, you only updated the version of phpunit to v5.1.3?
3

I'm posting this in case somebody ends up here after upgrading to Symfony 4.4 / 5.0 and is having this error.

In newer versions of Symfony, you have a version which is defined in the phpunit.xml.dist. This version will be downloaded when you run ./bin/phpunit and it's sources will be placed under ./bin/.phpunit/phpunit-VERSION. If you are executing php ./bin/console --env=test debug:container you might get the mentioned by the OP exception. The fix is easy - you need to add the path to the classes in the composer.json:

    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        },
        "classmap": [
            "tests/",
            "bin/.phpunit/phpunit-9.2.0-0/src" <--- Make sure the version is correct.
        ]
    },

Afterwards run composer dump-autoload and finally php ./bin/console --env=test debug:container should work.

1 Comment

I ended up with this: "autoload-dev": { "psr-4": { "App\\Tests\\": "tests/" }, "classmap": [ "bin/.phpunit/phpunit/src" ] },. bin/.phpunit is a symlink. Thanks for sharing!
1

In my case I had a different version of phpunit earlier in my path in the /Applications/MAMP/Library/bin directory. After creating a symbolic link to the globally installed version (7.5.1) instead of 5.1.3, this fixed the error for me:

cd /Applications/MAMP/Library/bin
mv phpunit phpunit_bak
ln -s /usr/local/bin/phpunit phpunit

Comments

1

Just in case you are facing PHPUnit\Framework\TestCase not found in PhpStorm,

run php bin/phpunit at root of project, it will download all needed classes and PhpStorm error should gone

1 Comment

php bin/phpunit gives to me phpunit info and manual

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.