0
votes

I am using the Symfony3 plugin in PhpStorm. My PHP Interpreter is 7.0.18. I have PHPUnit 6.3.0 configured in PhpStorm by having the .phar file in the root directory of my project.

Unit test work like a charm inside the IDE however performing any operation on the server (like bin/console server:start) triggers the following messages:

PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /1tb/programming/PhpstormProjects/binary_search/src/AppBundle/Search/BinarySearchTest.php on line 13
PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /1tb/programming/PhpstormProjects/binary_search/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Tests/TestCase.php on line 17

BinarySearchTest.php:

 <?php

namespace AppBundle\Search;

use PHPUnit\Framework\TestCase;

class BinarySearchTest extends TestCase
{

}

TestCase.php:

<?php

namespace Symfony\Bundle\FrameworkBundle\Tests;

use PHPUnit\Framework\TestCase as PHPUnitTestCase;

class TestCase extends PHPUnitTestCase
{

}

I have read many posts with problems similar but none of them describe the problem the way I do. Then I tried running PHPUnit with phpunit . in the root directory of the folder with this error:

PHP Fatal error: Class 'Doctrine\Tests\Common\Cache\CacheTest' not found in /1tb/programming/PhpstormProjects/binary_search/vendor/doctrine/cache/tests/Doctrine/Tests/Common/Cache/ApcCacheTest.php on line 10

It seems whatever I do I just run into more errors. I only just got started with Symfony and read a bit of the documentation but I can't get a grip on this thing, I have been at it for two days. Any suggestions for me?

2
Try using composer installed PHPUnit instead of PHAR version... So far it could be that 1) on a server (or when run phpunit .) it's not using PHAR (very likely) or 2) you do not have dev dependencies installed (PHPUnit is usually a dev-time only dependency as it's not really needed on production server). - LazyOne
Seemed to have worked. I did the installation steps from the website and placed the composer in my /usr/local/bin directory with "mv /usr/local/bin/composer" Note that I cut off the .phar. Now it's an executable. I started a new project and did "init composer" and this time with the executable then immediately I did "Manage dependency" and installed phpunit/phpunit. It worked. Finally I got an error saying the class CacheTest was not found, I circumvented this by editing the test configuration to only run the directory with test and not the entire project directory. Server works now too. - BFMC2

2 Answers

1
votes

I have PHPUnit 6.3.0 configured ... by having the .phar file in the root directory of my project

Such a bad idea. PHPUnit should not be installed on your (production?) server.

If this is a local staging server that you're trying to test on, then you need to install the phar in the path.

To globally install the PHAR:

$ wget https://phar.phpunit.de/phpunit-6.2.phar
$ chmod +x phpunit-6.2.phar
$ sudo mv phpunit-6.2.phar /usr/local/bin/phpunit
$ phpunit --version

Also, consider upgrading PHP to the newest version. There are several vulnerabilities in the one you're using. (See: change log for versions between yours and current).

EDIT:

Why are you running bin/console server:start on your server? Also not meant to be on a production server.

My guess here is that it is seeing the phar in your document root and trying to execute it, which is what is causing all the errors.

0
votes

Installation of PHPUnit via composer worked. It turns out my composer installation went wrong in some way. After composer was successfully installed, I let it handle installing PHPUnit. After that it just worked. Tests work fine in the IDE and the server is responsive again. Thanks LazyOne.