I've just started using Travis CI to test my PHP code. Sometimes, builds fail with the message
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /home/travis/build/ms609/citation-bot/tests/phpunit/expandFnsTest.php on line 13
On other occasions, without my having changed any relevant code, the builds succeed.
This makes me suspect that the issue is at Travis's end rather than my own.
This makes me wonder: is there anything that I can do to reduce the likelihood of my encountering this error? And on builds when the error arises, is it possible to have Travis re-attempt the build without making a new commit?
I had inherited a testcase written using an older version of phpunit. For backwards compatibility, following advice elsewhere, I had added the code
if (!class_exists('\PHPUnit\Framework\TestCase') &&
class_exists('\PHPUnit_Framework_TestCase')) {
class_alias('\PHPUnit_Framework_TestCase', 'PHPUnit\Framework\TestCase');
}
What I needed to do next was replace
class myTest extends PHPUnit_Framework_TestCase {
with
class myTest extends PHPUnit\Framework\TestCase {
With regards to the second part of the question, signing in to Travis CI reveals a "restart build" option on the build page.
Related
So have PHPUnit and CodeIgniter installed:
http://d.hatena.ne.jp/Kenji_s/20120117/1326763908
Couldn't download the PEAR as its been deprecated. So had to download the phpunit phar file:
http://phpunit.de/manual/4.0/en/installation.html#installation.phar
So was able to get some tests to run properly. Moved my phpunit.phar to /usr/local/bin and ran on the tests dir:
php /usr/local/bin/phpunit.phar
And all the tests ran correctly. But when i tried to run the php generate fixtures and php generate.php fixtures:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /www/test/application/third_party/CIUnit/libraries/CIUnitTestCase.php on line 15
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /www/test/application/third_party/CIUnit/libraries/CIUnitTestCase.php on line 15
Seems like its not finding the classes inside the phar file or at least they are not in the correct order? What is funny is that it runs the tests fine but not the generate fixtures.
Additionally i also installed using composer the phpunit so i have a /www/test/vendor/bin/phpunit installed as well.
Any help would be appreciated.
I had the same problem in my code, although I do not use the CodeIgniter. Trying to run tests would result in the error message:
Class 'PHPUnit_Framework_TestCase' not found
For what it's worth I had this fixed by adding a backslash to my test class declaration.
// Before
namespace IMAVendor\Super\Duper;
class MyClassTest extends PHPUnit_Framework_TestCase
// After
namespace IMAVendor\Super\Duper;
class MyClassTest extends \PHPUnit_Framework_TestCase
^
Added this backslash here
This seems to have something to do with namespaces and the autoloader that's built in phpunit. I have my own autoloader for the project code and it seems that it was trying to load the phpunit's classes from my code. I'm not really sure why it didn't try to load it from the 'base' when it wasn't able to find it in the projects namespace (This may very well be due to my own autoloader being faulty).
I know this is an old question, but I'll just leave this here in case it may help somebody somewhere.
I'm using payumbundle on my website but in my prod server (in dev server everything works fine) i get this error:
FatalErrorException: Compile Error: Can't inherit abstract function Payum\Request\StatusRequestInterface::getModel() (previously declared abstract in Payum\Request\ModelRequestInterface) in /path/to/folder/vendor/payum/payum/src/Payum/Request/BinaryMaskStatusRequest.php line 5
Here is the code at vendor/payum/payum/src/Payum/Request/BinaryMaskStatusRequest.php at line 5
class BinaryMaskStatusRequest extends BaseModelInteractiveRequest implements StatusRequestInterface {
in my development server i have PHP 5.3.10 while in prod server i have PHP 5.3.3.
Actually i commented Payum\Request\StatusRequestInterface::getModel() function, but i'm pretty sure that this isn't the right way to fix it
Nice catch! I will remove these methods (getModel and setModel) on StatusRequestInterface interface. They come from previous version and should not be there any longer. The bad thing is I can remove them only since version 0.6 as it introduce BC break.
For unittest
class SampleTest extends PHPUnit_Framework_TestCase
{
public function testBreakpoint()
{
$a = 18;
}
}
with breakpoint on line 5 "$a = 18;",
Xdebug v2.1.0,
PHPUnit 3.6.10,
PHP 5.3.6,
ubuntu 10.11
Running unittest with NO --process-isolation option stops script execution on the line 5, as expected.
Running the same configuration WITH --process-isolation option does not stop execution on line 5.
The option --process-isolation runs every test in new process using 'proc_open' in runJob function in https://github.com/sebastianbergmann/phpunit/blob/3.6/PHPUnit/Util/PHP.php
Tested with PhpStorm 3 and vim 7 with debugger plugin. It allows to debug PHPUnit itself, but not testcases.
Is there any way to debug the child process created by PhpUnit using Xdebug? may be Zend Debugger?
As stated in the comments on the question. The issue is that PHP Storm didn't support multiple parallel debugging sessions.
Go into PHPStorm Project Settings - PHP - Debug and set Xdebug to "force break at first line when script is outside the project".
It should break on some main() method and if you step over a couple of times (or press resume), it will reach your test.
This isn't a perfect answer, but you can surround any block of code with xdebug_start_trace() and xdebug_stop_trace() calls to generate a stack trace for a targeted block of code. I've used this to see exactly what it happening at specific points in my unit tests when testing other peoples' code.
class SampleTest extends PHPUnit_Framework_TestCase
{
public function testBreakpoint()
{
xdebug_start_trace('/tmp/testBreakPointTrace');
$a = 18;
xdebug_stop_trace();
}
}
Just keep in mind that any failures will cause PHPUnit's exception handler to step in and cause the stack trace to look a little strange. If you are getting an error, you can get a clean trace by adding an exit; call right after xdebug_stop_trace:
class SampleTest extends PHPUnit_Framework_TestCase
{
public function testBreakpoint()
{
xdebug_start_trace('/tmp/testBreakPointTrace');
$a = 18;
xdebug_stop_trace();
exit;
}
}
I installed PHPUnit and my Test class looks like this:
require_once 'PHPUnit/Framework/TestCase.php';
class Test extends PHPUnit_Framework_TestCase {...}
When I execute the PHP script in Eclipse, I get the following error:
Fatal error: Class 'PHPUnit_Framework_Assert' not found in .../PEAR/PHPUnit/Framework/TestCase.php on line 99
So I created a general PHP classloading test:
A.php and B.php in the same directory
A.php:
class AA {}
B.php:
class BB extends AA {}
new BB();
When executing the PHP script B.php I get the same error:
Fatal error: Class 'AA' not found in .../B.php on line 2
There must be an option for PHP to be able to resolve these classes otherwise PHPUnit could not work. Any ideas?
Thank you.
You should not be loading / require
require_once 'PHPUnit/Framework/TestCase.php';
in your tests at all. The normal phpunit runner should be able to figure that out.
Usually IDEs should care about setting phpunit up properly (or invoking it properly) but if that doesn't work out requiring
require_once 'PHPUnit/Autoload.php';
That should do the trick then as this is whats needed to make PHPUnit working
I ran into this issue when integrating with NetBeans. The solution for me was to load a bootstrap.php file, which would include all necessary dependencies while leaving my class files untouched.
Oops: just realized you're using Eclipse. It should be pretty similar. The problem is likely that your include script is relative to Eclipse's working directory (or some directory other than where you application normally runs). But that's a stab in the dark without being too familiar with Eclipse myself...
In case PHPUnit 6.x is used, then PHPUnit_Framework_Assert class has been removed. You should use namespaces instead, or downgrade to ~4.5.
So replace PHPUnit_Framework_Assert with \PHPUnit\Framework\Assert, ot use statement like:
use PHPUnit\Framework\Assert;
And use Assert directly, e.g. Assert::assertNotEmpty(...);.
Source: Class 'PHPUnit_Framework_Assert' not found (Behat\Testwork\Call\Exception\FatalThrowableError #2585
I just installed PHPUnit 3.5 on my system, upgrading it from 3.4, and I'm having some trouble with the new version. When I try to run a test, I always get the same output. Here's what I get when I try to run on the command line the StackTest example from the PHPUnit manual, example 4.1:
> phpunit StackTest
X-Powered-By: PHP/5.2.17
Content-type: text/html
PHPUnit 3.5.13 by Sebastian Bergmann.
Class StackTest could not be found in StackTest.php.
Worse yet, when I try to run it from a web browser, I get the following output:
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /path/to/tests/StackTest.php on line 2
Does anyone know how to set this up? Thanks.
I had the problem you described on Windows.
The problem was in the file pear\PHPUnit\Runner\StandardTestSuiteLoader.php on line 131 an it was caused by different drive letter case in file name in the condition
if ($class->getFileName() == realpath($suiteClassFile)) {
My simple fix is to change this line to be case insensitive
if (strtolower($class->getFileName()) == strtolower(realpath($suiteClassFile))) {
phpunit MyTestClass
In my case
MyTestClass.php should be in the project home directory
it should starts with long php open tag (<?php, not <?)
it should contain class MyTestClass extends PHPUnit_Framework_TestCase {
I know this is most likely not the best way, just point for a beginner to start with.
Try
pear upgrade pear
(if it asks you to channel upgrade do so)
and then
pear install --force --alldeps phpunit/phpunit
and try again.
The 3.5 upgrade combined with a buggy pear installer (1.9.1 has a kinda annoying bug so make sure you are really on 1.9.2) can be a pain sometimes.
I think your PHPUnit Class named StackTest, and the class you want to test is also named StackTest. This will cause a path conflict in PHPUnit.
Make these 2 names different and you will get this resolved.
In my case, this problem was caused by including PHPUnit in the source file via require_once:
require_once 'phar://phpunit.phar';
Removing that line made my test case runnable.
This error can also be caused when you forget to have your test class extend the PHPUnit TestCase class, like
class MyTestClass extends \PHPUnit\Framework\TestCase { ...
I was able to fix the problem. It was a result of how I was loading the class. I used my arguments in the argument array like so and it worked. But there were a lot of other problems with the classpath etc that I had to fix first. To see a working solution look here (http://www.siteconsortium.com/h/p1.php?id=php002).
$command = new PHPUnit_TextUI_Command();
$command->run(array('test', 'testCase', 'c:\workspace\project\testCase.php'), true);
Starting from PHPUnit 9, it is required that the filename match the class name in the test.
#4105: Deprecate multiple test case classes in single file and test case class name differing from filename
So test-plugin.php with a class name PluginTest will fail with this error. To fix it, you'd need to rename the file to PluginTest.php.
Bad error message IMO.
It sounds like PHPUnit isn't on your include path. To easily test this, try this:
$ phpunit --include-path /path/to/PHPUnit StackTest