I'm just starting to try out phpunit on some existing code. The naming convention we use is that the MyClass class should be in MyClass.class.php. PHPUnit seems to require that the file should be called MyClass.php.
Is there any way around this?
I noticed it while trying to generate a skeleton test class:
phpunit --skeleton-test MyClass.class
PHPUnit 3.3.4 by Sebastian Bergmann.
Could not find class "MyClass.class" in "/home/jd/skeleton/classes/MyClass.class.php".
Fatal error: Call to a member function getOutClassName() on a non-object in /usr/share/php/PHPUnit/TextUI/Command.php on line 470
Its not a requirement, its just assumptive. You can write your own test-cases.
Skeleton just makes a mock-up one "the easy way" that makes dummy functions for all your classes dummy functions.
Also,
phpunit --skeleton-test MyClass MyClass.class.php
Might do what you want.
Usage: phpunit [switches] UnitTest [UnitTest.php]
phpunit [switches] <directory>
--skeleton-class Generate Unit class for UnitTest in UnitTest.php.
--skeleton-test Generate UnitTest class for Unit in Unit.php.
so by that reasoning, if you don't tell it what file the class is in it will try guess, if you do tell it what file the class is in, it wont guess.
I ran up against a similar problem when the class a skeleton is being generated for is namespaced. I'm using phpunit 3.5.6 from the command-line. The phpUnit documentation example didn't work for me and I couldn't find the answer online so I figured I share it here. Suppose you have class Foo inside directory /my/dir and it's declared in a single file Foo.php so the full path to the class file is /my/dir/Foo.php
Also suppose that Foo is declared as follows:
<?php
namespace my\space;
class Foo
{
...
}
In order to generate a test skeleton I made sure I was in the directory where Foo.php resides and then I ran the following command:
user[/my/dir]$ phpunit --skeleton-test my\\dir\\Foo Foo.php
That got my skeleton to generate. The key was escaping the backslash as the documentation of phpUnit mentions namespaces but only shows one backslash. Hope this helps someone.
Related
I'm using Codeception to TDD the development of a simple application in PHP. I've created my first unit test called DogTest.php with a simple assertion, but it's complaining about not being able to find the Dog class.
I have created a Dog.php file in the root directory, and also placed it under /src, but neither is working. I think this is either a namespace issue or an autoloader issue, but the Codeception documents (and the various TDD guides I've looked at) have this important detail missing.
Can someone please advise on how to get my DogTest to detect the Dog class?
In your composer.json, make sure you have added autoloading configuration. As an example:
"autoload":{
"psr-4":{
"Del\\":"src/"
}
}
Every file in src should have namespace Del. For instance, src/Blank.php would look like:
<?php
namespace Del;
class Blank
{
}
Whereas src/Http/Client.php would have namespace Del\Http.
Once added, run composer dumpautoload to generate the class maps. Your classes should now autoload without problems.
See my Blank starter project with codeception test for more info.
https://github.com/delboy1978uk/blank
I have a scenario that I wanted to 'derequire' or just the require_once needs to be on that class only.
I learned that in PHPUnit the test files are gonna be included. And because of that, the required_once is overriding the class that I needed. I needed to use the original class. Is it possible?
PHPUnit: include class after mocking it
Isn't PHPUnit supposed to require the class itself using the naming conventiong "ClassTest"?
I have a php file "Router.php" with a class Router
My PHPUnit testing class is defined as following:
class RouterTest extends PHPUnit_Framework_TestCase
However it raises a fatal error for file not found "Router.php".
If I require it manually everything is ok.
What's wrong with it?
There is nothing wrong with it. How would PHPUnit know where your classes are in relation from the tests?
For your tests, you need to either require the class under test yourself or create an autoloader that will require the class for you. PHPUnit has no idea where it should be getting the files from. It has an autoloader for its internal files but that is it.
i'm newbie & trying to learn travis within my PHP development environment.
It's very "basic", i just trying a sample .travis.yml from official guide. But it always failing.
My repo is here https://github.com/rahmatawaludin/learn_travis and my travis build is here http://travis-ci.org/rahmatawaludin/learn-travis .
Could anyone give me some help?
I guess you followed the PHPUnit manual.
If you want to write a test for class MyClass (file MyClass.php), the corresponding tests would be in class MyClassTest (file MyClassTest.php).
Now if you want to run the test you'd call phpunit like that:
phpunit MyClassTest
But you called phpunit with the filename, just remove the .php extension, then it should work.
This might be a stupid question, but I can't seem to make it to work.
I'm using PHPUnit to test. Currently I have two classes in a file called Tests.php:
class XTest extends PHPUnit_Framework_TestCase {...}
class YTest extends PHPUnit_Framework_TestCase {...}
However, I'm unable to run both classes. I'm running the following command on Windows:
php "C:\Program Files (x86)\PHP\phpunit" Tests
And it tries to run a test class called "Tests". Instead, I'd like it to run "XTest" and "YTest" and all that are on the file. How could I run multiple test classes easily?
Putting all of your tests under the same directory and asking PHPUnit to traverse them recursively would work, but if you have your tests under different directories or only want to run specific portions of specific test classes, then the #group annotation might be what you're looking for.
When you execute your tests, you can use the php "C:\Program Files (x86)\PHP\phpunit" --group <insert_name_of_group_to_which_xtests_and_ytests_belong> and PHPUnit will only execute those tests that have #group insert_name_of_group_to_which_xtests_and_ytests_belong in their PHPDoc.
The PHPUnit Docs explain the arguments the command line test runner expects.
In your case, you're providing Tests, which means PHPUnit looks for a class Tests in a file Tests.php.
With this knowledge, it's easy to see that the best way to organise your tests will be to write one test class per file, with the filenames equal to TestClassName.php.
However, if for some reason you don't want to do that, you can provide an extra argument to tell the test runner which file the test class is declared in:
php "C:\Program Files (x86)\PHP\phpunit" XTest Tests.php
php "C:\Program Files (x86)\PHP\phpunit" YTest Tests.php