PHP Fatal error: class 'PHPUnit \ Framework \ TestCase' not found with PHPUnit 6 and PHP 7.0

I play 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(' foo@bar.com '); $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 content for composer.json file:

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

According to PHPUnit 6 documentation , your tests should now extend PHPUnit \ Framework \ TestCase instead of PHPUnit_Framework_TestCase.

I know that this is not a problem with startup. Actually, if I replaced PHPUnit \ Framework \ TestCase with PHPUnit_Framework_TestCase, it works fine, but I was wondering why this syntax is not working.

I tried some research on google, stackoverflow and the PHPUnit github repository but found nothing.

I look forward to 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 
+11
source share
2 answers

I found the answer:

I tried my test using this command line:

 phpunit Test/ObserverTest.php 

PHPUnit is installed globally on my computer, but this is version 5.1.3:

 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 of PHPUnit \ Framework \ TestCase only works with PHPUnit 6

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

+15
source

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 
0
source

Source: https://habr.com/ru/post/1014962/


All Articles