PHPUnit working in IDE, but server says class not found

流过昼夜 提交于 2019-12-20 06:59:29

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/45585073/phpunit-working-in-ide-but-server-says-class-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!