PHPUnit can't found the "TestCase" class - php

To run my tests using the project's PHPUnit I do the following : php vendor/bin/phpunit tests/SomeClassTest.php which works fine given the following class declaration :
class SomeClassTest extends PHPUnit_Framework_TestCase {
public function test_someMethod() {}
}
But it fails when I do this :
use PHPUnit\Framework\TestCase;
class SomeClassTest extends TestCase {
public function test_someMethod() {}
}
I get PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found...

Class TestCase exists since PHPUnit 5.4. You can see it on github if you set 5.3 tag (look for ForwardCompatibility folder) or you can compare doc for 5.3 and 5.4 in the 2. Writing Tests for PHPUnit section where it says:
"ClassTest inherits (most of the time) from PHPUnit_Framework_TestCase." for PHPUnit 5.3
and
"ClassTest inherits (most of the time) from PHPUnit\Framework\TestCase." for PHPUnit 5.4

In my library that I still have marked as usable by PHP 5.4, I've had to add this to my top-level testcase class in order to bridge the non-namespaced / namespaced difference, depending on which version of PHPUnit gets installed by Composer based on the runtime PHP version.
/*
* Allow for PHPUnit 4.* while XML_Util is still usable on PHP 5.4
*/
if (!class_exists('PHPUnit_Framework_TestCase')) {
class PHPUnit_Framework_TestCase extends \PHPUnit\Framework\TestCase {}
}
abstract class AbstractUnitTests extends \PHPUnit_Framework_TestCase
{
This works fine on PHP 5.4 (PHPUnit 4.8.34) up to PHP 7.1 (PHPUnit 6.0.2).

Related

phpunit 8 and PHPUnit_Framework_TestCase

I wrote the following test:
use PHPUnit\Framework\TestCase;
//other uses
class StatusCheckerTest extends TestCase
{
public function testEntryValidation() {
//some code
$this->assertFalse($validation_result);
}
}
I tried to run it with phpunit StatusCheckerTest.php
But when I do it, I see error:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in
/var/www/html/nextcloud/apps/globalstatus/tests/OCA/GlobalStatus/Tests/StatusCheckerTest.php on line 12
I used PHPUnit 8.4.3 (also tried with 6.5.5). I installed it manually.
phpunit --version
PHPUnit 8.4.3 by Sebastian Bergmann and contributors.
Why does it look for PHPUnit_Framework_TestCase? It must be deprecated.
If you want to run only a specific test class, you need to pass it with a filter option:
phpunit --filter StatusCheckerTest

PHPUnit Error: Call to undefined method Controller::request()

On running this test, I get Error: Call to undefined method OrderControllerTest::request()
<?php
use PHPUnit\Framework\TestCase;
class OrderControllerTest extends TestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
PHPUNIT Version: 7.2.4. Appreciate any help
For the kenjis/ci-phpunit-test package if you run phpunit from the application/tests folder so it picks up on the included phpunit.xml and included TestCase Class then they should run.
cd application/tests
then, if you are using the composer installed version of phpunit in your project:
../../vendor/bin/phpunit
or if you're using the globally (apt etc.) installed version simply:
phpunit
see http://blog.a-way-out.net/blog/2015/06/12/codeigniter3-phpunit/#how-to-run-tests
The package kenjis/ci-phpunit-test extends the standard PHPUnit package so what you need to do is to extend the CIPHPUnitTestCase instead like in example below.
<?php
class OrderControllerTest extends CIPHPUnitTestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
You may need to configure your IDE so that it can find CIPHPUnitTestCase class.

how to fix Declaration of PHPUnitLogger::flush() error?

I am using Eclipse pdt for PHP Developers Version: Oxygen.2 Release (4.7.2).
I created a Composer Project and I added those dependencies:
then I created a TestCase file to test my class.
I could not change the superclass "PHPUnit_Framework_TestCase".
I got this warning when creating the TestCase file
There is no element 'PHPUnit_Framework_TestCase' in the project
'PayementAPI'
then into the default TestCase Class created I changed the extends "PHPUnit_Framework_TestCase" to "TestCase" and I added the import.
<?php
use PHPUnit\Framework\TestCase;
include 'otherClass.php';
/**
* MyClass1 test case.
*/
class MyClass1Test extends TestCase
{
...
}
Then I tried to run my class test as PHPUnit test but got this error:
PHP Fatal error: Declaration of PHPUnitLogger::flush() must be compatible with PHPUnit\Util\Printer::flush(): void in C:\Users\User\AppData\Local\Temp\phpunit_printer\PHPUnitLogger.php on line 33
Try one with a lower version of phpunit.phar, download the lower version here.

Use PHPUnit on PhpStorm IDE

I've developed a simple test class usign PHPUnit on my PhpStorm IDE.
PHP version: 5.4.25
PHPUnit version 4.2.1
MAMP 3.0.2
PHPStorm 7.1
This is the configuration of my IDE:
And this is my Test class:
namespace test\controllers;
class AuthTest extends \PHPUnit_Framework_TestCase {
protected function setUp() {
parent::setUp(); // TODO: Change the autogenerated stub
echo "start";
}
protected function tearDown()
{
parent::tearDown(); // TODO: Change the autogenerated stub
echo "end";
}
public function testProva() {
$tot = 5;
$this->assertEquals($tot, 5);
}
}
EDIT:
This is the configuration of my test run:
When I Run the class (class name: AuthTest, file name: AuthTest.php) I obtain the following error: Process finished with exit code 255
But when I run the same class by command line phpunit path/test/folder/ it works fine.
What is wrong?
PhpStorm v7.x does not support PhpUnit 4.x (only 3.6 ..or maybe 3.7 MAX).
PhpStorm uses special helper/wrapper script for integration purposes (so that IDE receives test progress/results in understandable format (much-much easier that parsing PHPUnit native output, which does not provide much details needed for IDE)). This wrapper in PhpStorm v7.x does not support PHPUnit v4.x.
For PhpUnit 4.x support you should try v8 EAP build (or wait for v8 official release -- 1-2 months from now, approximately).

Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?

Why I'm getting this PHP error?
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...
For those arriving here after updating phpunit to version 6 or greater released on 2017-02-03 (e.g. with composer), you may be getting this error because phpunit code is now namespaced (check changelog).
You will need to refactor things like \PHPUnit_Framework_TestCase to \PHPUnit\Framework\TestCase
The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:
require_once ('PHPUnit/Framework/TestCase.php');
UPDATE
As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:
require_once 'PHPUnit/Autoload.php';
Thanks to Phoenix for pointing this out!
For higher version of phpunit such as 6.4
You must use the namespace PHPUnit\Framework\TestCase
use TestCase instead PHPUnit_Framework_TestCase
// use the following namespace
use PHPUnit\Framework\TestCase;
// extend using TestCase instead PHPUnit_Framework_TestCase
class SampleTest extends TestCase {
}
I was running PHPUnit tests on PHP5, and then, I needed to support PHP7 as well. This is what I did:
In composer.json:
"phpunit/phpunit": "~4.8|~5.7"
In my PHPUnit bootstrap file (in my case, /tests/bootstrap.php):
// PHPUnit 6 introduced a breaking change that
// removed PHPUnit_Framework_TestCase as a base class,
// and replaced it with \PHPUnit\Framework\TestCase
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
In other words, this will work for tests written originally for PHPUnit 4 or 5, but then needed to work on PHPUnit 6 as well.
You may get this error because you namespaced the file. If so you will need to specify that PHPUnit_Framework_TestCase is in the global namespace by preceding it with a backslash:
namespace AcmeInc\MyApplication\Tests
class StackTest extends \PHPUnit_Framework_TestCase {}
I submitted a crude PR to start conversation for correcting the documentation.
You can simply install PHPUnit to run commands (https://github.com/sebastianbergmann/phpunit/#php-archive-phar):
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
Run single test
And then run PHPunit test:
phpunit test.php
Content of test file is following:
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
}
public function testSave()
{
}
}
Run test suite
Configuration of test suite: demosuite.xml. demo is directory containing all tests. Test files must be named as *_test.php (suffix).
<testsuites>
<testsuite name="DemoTestSuite">
<directory suffix="test.php">demo</directory>
</testsuite>
</testsuites>
Test suite runs with following commands:
phpunit -c demosuite.xml --testsuite DemoTestSuite
Assumption:
Phpunit (3.7) is available in the console environment.
Action:
Enter the following command in the console:
SHELL> phpunit "{{PATH TO THE FILE}}"
Comments:
You do not need to include anything in the new versions of PHPUnit unless you do not want to run in the console. For example, running tests in the browser.
I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'
I got it working with
include("vendor/autoload.php");
at the top of my test function.
If you have Centos or other Linux distribution you have to install phpunit package, I did that with yum install phpunit and it worked. Maybe you can have to add a repository, but I think it has to work smooth with the default ones (I have CentOS 7)
It may well be that you're running WordPress core tests, and have recently upgraded your PhpUnit to version 6. If that's the case, then the recent change to namespacing in PhpUnit will have broken your code.
Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547 which will work around the problem. It also includes changes to travis.yml, which you may not have in your setup; if that's the case then you'll need to edit the .diff file to ignore the Travis patch.
Download the "Unified Diff" patch from the bottom of https://core.trac.wordpress.org/changeset/40547
Edit the patch file to remove the Travis part of the patch if you don't need that. Delete from the top of the file to just above this line:
Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself
Use the Unix patch tool to patch the files. You'll also need to strip the first few slashes to move from an absolute to a relative directory structure. As you can see from point 3 above, there are five slashes before the include directory, which a -p5 flag will get rid of for you.
$ cd [WORDPRESS DIRECTORY]
$ patch -p5 < changeset_40547.diff
After I did this my tests ran correctly again.
NOTICE: Command php bin/console generate:doctrine:crud also create TestController in src/Tests so it can throw error when you tried to start server if you don't have UnitTests. Remove the file fix it!
For me, it was because I ran
$ phpunit .
instead of
$ phpunit
when I already had a configured phpunit.xml file in the working directory.
I am using php 5.6 on window 10 with zend 1.12 version for me adding
require_once 'PHPUnit/Autoload.php';
before
abstract class Zend_Test_PHPUnit_ControllerTestCase extends
PHPUnit_Framework_TestCase
worked. We need to add this above statement in ControllerTestCase.php file

Categories