Failing in basic travis.yml in PHP env - php

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.

Related

How to specify a specific folder when generating test case from phpunit --skeleton?

I'm trying to generate TestCase with the phpunit generator.
I'm using the following command:
phpunit --skeleton-test "Namespace\Service\ArticleService" ../library/Namespace/Service/ArticleService.php
I'd like my tests to go in /tests/Namespace/Service/ArticleServiceTest.php
Is it possible to specify such options with PHPUnit?
No, it is not possible. Open a feature request in the phpunit bug tracker on github.
The newer phpunit-skelgen (1.2.0) allows you to specify the destination file for tests, which may include path references.

PHPUnit generated test skeleton path

Is it possible to tell phpunit where to put generated test skeleton file by the --skeleton-test command? Even is it possible to tell phpunit to repeat directory structure?
Lest say i have file for testing in lib/model/SomeClass.php and i want phpstorm to generate unit test class and put it in test/lib/model/SomeClassTest.php without creation of all neccessary directory structure.
Thanks in advance!
Example reproduced for convenience
mkdir lib/model tests/lib/model
echo "<?php class SomeClass{}" > lib/model/SomeClass.php
Calling the code below should do the trick
phpunit --skeleton-test SomeClass library/model/SomeClass.php SomeClassTest tests/library/model/SomeClassTest.php
The arguments (only the first one being mandatory) are
$inClassName,
$inSourceFile = '',
$outClassName = '',
$outSourceFile = ''
as described in the constructor of PHPUnit_Util_Skeleton_Class
Yeah that is possible in netbeans IDE. if you configure PHPUnit(Xdebug required) with netbeans then it automatically creates file/folder structure for your framework
Netbeans link
Also it creates the skeleton for each class of Controller and module
/vendor/phpunit/phpunit-skeleton-generator/src/TestGenerator.php
In the constructor change the $outSourceFile to the path you would like the file to save to.
Cheers

Testing multiple classes with PHPUnit

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

PHPUnit filenaming conventions

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.

How do I run unit tests stored in my application directory as opposed to in the PHP directory?

I've just installed PHPUnit and wrote a quick class which I saved to C:\PHP and it worked fine. If however I move the php file containing the test class to the tests directory of my application, it returns the error Class firstTest could not be found in ..
How do I resolve the problem such that it can see the class in the application test directory?
Thanks for the response - it wasn't the solution I used, but it led me to research that produced an alternative.
What I did was to add my PHP directory (C:\PHP) to the PATH environment variable. This allowed me to call phpunit from the tests directory of my application.
Check your config file and ensure that the correct path to the tests dir is given.

Categories