phpunit 8 and PHPUnit_Framework_TestCase - php

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

Related

PHPUnit Laravel not running next test case

I created a feature test but the problem is it is only executing the first test and never executing the next test.
namespace Tests\Feature\Api\Auth;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class AgentLoginTest extends TestCase
{
use DatabaseTransactions;
public function test_successful_with_email()
{
$response = $this->post(
$this->route_api_agent('login/email'),
['email' => 'someemail#gmail.com'],
$this->createHeaders()
);
$response->assertStatus(200);
}
public function test_failed_with_email_not_found()
{
$response = $this->post(
$this->route_api_agent('login/email'),
['email' => '000somerandombullshitgo#gmail.com'],
$this->createHeaders()
);
$response->assertStatus(200);
}
}
However, this is the result:
./vendor/bin/phpunit
PHP Warning: Module "mbstring" is already loaded in Unknown on line 0
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
{"code":401,"success":true,"message":"You are not registered yet"}
In which that does include not executing the second function. I tested it by using --debug flag, and this is the result:
./vendor/bin/phpunit --debug
PHP Warning: Module "mbstring" is already loaded in Unknown on line 0
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
Test 'Tests\Feature\Api\Auth\AgentLoginTest::test_successful_with_email' started
{"code":401,"success":true,"message":"You are not registered yet"}
Am I doing something wrong here?

Mockery fails with 'Could not load mock ... class already exists' when running with --code-coverage

I am trying to mock a class for phpunit. Php unit fails with the error Could not load mock ... class already exists. This is the only test I'm running, so it can't be the case that the class is mocked already.
Any suggestion would be appreciated.
Here is the error case:
namespace Tests\Feature;
use Tests\TestCase;
class DeactivateACSTest extends TestCase
{
public function testDeactivateAcs()
{
$deviceController = \Mockery::mock('overload:App\Http\Controllers\Cloud\DeviceController');
$deviceController
->shouldReceive('deactivateACS')
->andReturn('hilfehilfehilfe');
$devCon = new \App\Http\Controllers\Cloud\DeviceController();
$this->assertEquals('hilfehilfehilfe', $devCon->deactivateACS());
}
}
When running it without --code-coverage it works:
[13:10:15] vagrant#homestead [~/Code/ekp] $ phpunit --filter DeactivateACS
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.
==> Tests\Feature\DeactivateACSTest ✓
Time: 1.08 seconds, Memory: 16.00MB
OK (1 test, 3 assertions)
However, when running it with --code-coverage it fails:
[13:10:23] vagrant#homestead [~/Code/ekp] $ phpunit --coverage-html coverage --coverage-text=code_coverage.txt --filter DeactivateACSTest
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.
==> Tests\Feature\DeactivateACSTest ⚈
Time: 5.79 seconds, Memory: 44.00MB
There was 1 error:
1) Tests\Feature\DeactivateACSTest::testDeactivateAcs
Mockery\Exception\RuntimeException: Could not load mock \App\Http\Controllers\Cloud\DeviceController, class already exists
/home/vagrant/Code/ekp/vendor/mockery/mockery/library/Mockery/Container.php:220
/home/vagrant/Code/ekp/vendor/mockery/mockery/library/Mockery.php:116
/home/vagrant/Code/ekp/tests/Feature/DeactivateACSTest.php:11
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Generating code coverage report in HTML format ... done
You should add these annotations before the functions that are mocking this class.
/**
* #runInSeparateProcess
* #preserveGlobalState disabled
*/
For reference you can check out the phpunit documentation.
https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.runInSeparateProcess
https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.preserveGlobalState
I ran into the same issue and fixed like this:
There was another test in my unit tests (not mockery test) which had require_once on the PHP file that had the class I was mocking. I've removed that line.
I've added processIsolation="true" in test suite

PHPUnit can't found the "TestCase" class

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).

Laravel phpunit skipping custom test files

I have created a laravel test file using the command
php artisan make:test EventUserRelations
When I run phpunit, it is as if the file does not even exist. I tried to fail the test just to make sure it is being tested. Here is the code in the file:
class EventUserRelations extends TestCase
{
public function testExample()
{
$this->fail();
}
}
And here is the result.
phpunit --debug
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.
Starting test 'ExampleTest::testBasicExample'.
. 1 / 1 (100%)
Time: 119 ms, Memory: 20.75MB
In Laravel test files need to end with ..test.php. So change EventUserRelations.php to EventUserRelationsTest.php and it should work.

PHPUnit: Class <ClassName> could not be found in <ClassNameTest>.php

I'm at my wit's end. I must have read every SO question on the same topic, but no joy.
I can't get phpUnit working properly. I've successfully installed phpUnit and it's dependencies using PEAR. I've also modified my php.ini file and added the path to phpUnit to the include path: (".:/php/includes:usr/lib/php/pear").
To test phpunit is working, I've copied this simple class, so MyClassTest.php is as follows:
class MyClassTest extends PHPUnit_Framework_TestCase
{
public function testCalculate()
{
$this->assertEquals(2, 1 + 1);
}
}
Running "phpunit MyClassTest" produces the following output: (running "phpunit MyTestClass MyTestClass.php" produces the same result);
class MyClassTest extends PHPUnit_Framework_TestCase
{
public function testCalculate()
{
$this->assertEquals(2, 1 + 1);
}
}
PHPUnit 3.7.13 by Sebastian Bergmann.
Class 'MyClassTest' could not be found in 'MyClassTest.php'.
I can't think what's wrong. I've tried uninstalling and reinstalling phpunit/PHPUnit, but no joy. Can you identify what's wrong? If you need any more info, let me know and I'll edit this post. Thanks in advance.
PHP 5.3.15
PHPUnit 3.7.13
OSX 10.8.2
Your source code gets printed to the console, so it seems like you forgot <?php at the beginning.

Categories