I am running the example from the documentation: http://docs.phalconphp.com/en/latest/reference/unit-testing.html#sample-unit-test
I want to create an abstract unit test from Phalcon\Test\UnitTestCase as in the documentation. However when I run my test I become:
PHP Fatal error: Class 'Phalcon\Test\UnitTestCase' not found
I have followed the exact documentation steps. Did anyone have the same problem and solved it?
This class is part of the incubator: https://github.com/phalcon/incubator
$loader = new Phalcon\Loader();
$loader->registerNamespaces(array(
'Phalcon' => '/path/to/incubator/Library/Phalcon/'
));
$loader->register();
I figure it out.
Basically we have to do 2 things.
is whats in the #twistedxtra's answer. (setting up the path to where the incubator is)
in the testsTestUnitTest.php we created, it has the following line
class UnitTest extends \UnitTestCase {
we have to change that line to
class UnitTest extends \Phalcon\Test\UnitTestCase {
What we did was set the proper namespace so that the code knows where the UnitTestCase class is.
Thats it. Cheers...!!!
Make sure you run phpunit command in tests folder. It's very important.
Don't run something like phpunit tests/
Related
I followed Codeception's quick start guide (http://codeception.com/quickstart) and read their documentation (http://codeception.com/docs/05-UnitTests).
I have managed to set up the testing environment, and
Created the unit test file (php codecept.phar generate:test unit ExampleTest)
Run the test command (php codecept.phar run unit ExampleTest), which returns an error:
There was 1 error:
1) ExampleTest: Validation
Test tests\unit\ExampleTest.php:testValidation
[Error] Class 'User' not found
#1 ExampleTest->testValidation
#2 C:\laragon\www\kario\vendor\bin\codecept.phar:5
How does the test file know which PHP file to run the test on?
My laragon project is named kario, and sits in C:\laragon\www\kario\resources\views\pages\orders while the test unit file is in C:\laragon\www\kario\vendor\bin\tests\unit.
I had this question too and found the answer for myself. Posted here http://phptest.club/t/beginner-codeception-unit-test-help/1849 but also, here you go:
First, some details. I am using Codeception v2.4.1, powered by PHPUnit 7.1.4. The answer is:
In codeception.yml, add these two lines:
settings:
bootstrap: _bootstrap.php
Here _bootstrap.php can be whatever you want the name of your bootstrap file to be.
You must place _bootstrap.php in each of the following directories as follows:
tests/unit/_bootstrap.php
tests/functional/_bootstrap.pp
tests/acceptance/_bootstrap.php
In my tests/unit/_bootstrap.php file, I placed the following code:
<?php
use Codeception\Util\Autoload;
Autoload::addNamespace('myclassnamespace', __DIR__ . '/../../Classes/');
To make sure I had the right path to Classes, I used trigger_error(__DIR__) in my _bootstrap.php before I added the Autoload line.
Then in my tests/unit/TestAddCest.php, I placed the following line at the beginning of the file:
<?php
use mynamespace;
And in my test function looks like this (note the instantiation of the User class):
public function tryToTest(UnitTester $I)
{
$user = new mynamespace\User('someusername');
$I->assertEquals('someusername', $user->username);
}
I hand typed that function because I'm not on the same machine with the code and didn't feel like getting it over, so it may have a typo or bug, but you get the idea.
Edit 05/01/2018: someone else answered me on http://phptest.club:
It would be better to configure autoloading of your classes in composer.json and let Composer to do the rest, unless you try to avoid using Composer.
https://getcomposer.org/doc/01-basic-usage.md#autoloading
I'm using the console component without Symfony standard edition and I'm working on my application.php file. When I run it it says:
Fatal error: Class 'GenerateTableCommand' not found in D:\ConnectCommand\vendor\application.php on line 10
My code:
<?php
require __DIR__.'\autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new GenerateTableCommand());
$application->run();
?>
My repository can be found here if needed: https://github.com/guuse/ConnectCommand
Any help is greatly appreciated.
First of all there's no \GenerateTableCommand, as stated in the error message.
This class is under AppBundle\Command namespace, so it's full name is AppBundle\Command\GenerateTableCommand.
You should add use statement at the beggining:
use AppBundle\Command\GenerateTableCommand;
Anyway, you'll probably encounter further issues with loading this class, since you do not really have any autoloaders for your custom code, so PHP won't be able to load this class.
Also mixing 3rd party code with your own (in vendor directory) is not a good idea.
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 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