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

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.

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

Illuminate\Console\Command not found when developing laravel 5.8 package

I am developing a package for Laravel 5.8. When I try to create a console command that extends Illuminate\Console\Command then "composer dump-autoload" fails with error message:
c:\Program Files (x86)\Ampps\www\ptest>composer dump-autoload
Generating optimized autoload files> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
ReflectionException : Class TestVendor\TestPackage\TestCommand does not exist
at C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
786| if ($concrete instanceof Closure) {
787| return $concrete($this, $this->getLastParameterOverride());
788| }
789|
> 790| $reflector = new ReflectionClass($concrete);
791|
792| // If the type is not instantiable, the developer is attempting to resolve
793| // an abstract type such as an Interface or Abstract Class and there is
794| // no binding registered for the abstractions so we need to bail out.
Exception trace:
1 ReflectionClass::__construct("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
2 Illuminate\Container\Container::build("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:667
I have tried to create the package by hand inside of C:\Program Files (x86)\Ampps\www\ptest\packages folder and I tried to use the packager https://github.com/Jeroen-G/laravel-packager but the result is identical in both cases.
TestCommand.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Console\Command;
class TestCommand extends Command {
protected $signature = 'test:hello';
protected $description = 'say hello';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info("hello!");
}
}
TestServiceProvider.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/testpackage.php', 'testpackage');
$this->app->singleton('testpackage', function ($app) {
return new TestPackage;
});
}
public function provides()
{
return ['testpackage'];
}
protected function bootForConsole()
{
// Registering package commands.
$this->commands([TestCommand::class]);
}
}
When I execute the TestCommand.php file directly from command line it fails with the error message
PHP Fatal error: Class 'Illuminate\Console\Command' not found
I have checked other working packages inside "Vendor" folder and all have the same structure as my package. It seems as if autoloading does not work properly.
The "console" folder was outside of "src" folder. Therefore it could not be discovered.
remove vendor and node_modules folder
then run following commands
composer update
npm install
it should work fine.

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.

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

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