ZF2 phpunit.xml <directory> - php

I've been following the Zend Framework 2 tutorial on unit testing, but once I reached the section 'A failing test case' my test actually passed instead of failing.
I've changed the Album Controller to become a definite failing test e.g. $this->assertTrue(false) and it still passes.
However, when I edit the phpunit.xml file and change <directory>./AlbumTest</directory> to become <directory>./AlbumTest/Controller/AlbumController.php</directory> the test fails as desired.
Does anyone know how to solve this?

PHPUnit looks for files that end with Test.php. Since your file is named AlbumController.php, it's being ignored. When you give PHPUnit the full path to the file, then it's reading it. So renaming the file to AlbumControllerTest.php should fix your problem.

Related

How do I set the working directory for phpunit using the XML file?

I'm looking through the documentation, but I'm not seeing any option to change the working directory used when running tests.
I'm using PhpUnit as it's included in Laravel. I want to be able to run vendor/bin/phpunit from my project's root directory, and have it run using the /public directory as the working directory.
I tried running ../vendor/bin/phpunit from the /public, but since the phpunit.xml file isn't in the public directory and I don't want to specify my config file path every time, that won't work.
Is there something I can add to my phpunit.xml file to tell it to run tests using the /public directory as the "cwd" (current working directory)?
Based on the feedback I received in the comments and the documentation, I determined the following:
It's probably not possible to change the cwd that phpunit uses by default (well, it's possible in PhpStorm, but not the command line without writing some kind of wrapper script)
Code that depends on being run from a specific directory is not a good idea.
What I had was some code in one of my classes like this:
$var = file_get_contents("../some_file.json");
This works fine -- until you try to add unit tests. The web server runs using the /public directory as the cwd, while phpunit will run using the root directory.
Rather than trying to force phpunit to always use a particular cwd (/public), I decided it's probably best to remove relative paths from the code that rely on a consistent cwd. So the line above becomes:
$var = file_get_contents(base_path("some_file.json"));
I didn't want to change production code that was already working just to get some tests in place, but this change seemed insignificant enough. (and it's an improvement anyway)
Well, you'd have to do the actual chdir in PHP, but you can define a bootstrap script in the XML (<phpunit bootstrap="./bootstrap.php">) and have that change the working directory.
Alternatively, you can put a setUpBeforeClass function into your test class that changes the working directory.

How can I tell phpunit where the source and tests can be found

I am a newbie with phpunit but I need it to test something
So, suppose that have the following folders
mainFolder/SRC/x/y/sourceFiles.php
mainFolder/TESTS/x/y/testFiles.php
mainFolder/TESTS/bootstrap.php
When I run phpunit mainFolder it tells me:
Fatal error: Class 'x/y/sourceFile' not found in mainfolder/tests/x/y/testfile.php on line 28
Note that I am a newbie, and I need some help.
Thanks
The test files should be able to find the tested classes. Which means you have to require them, or set up a proper autoloading mechanism or pre-load them in a bootstrap file. And probably there are lots of other ways to do this. The test files behave exactly the same in this sense like simple php files.

Run unit test when saving file in PhpStorm

I have set up PhpStorm 5 with PHPUnit, and I'm curious if PhpStorm might have some functionality that will automatically run a unit test when saving a file. Like watchr and guard. I have tried search our beloved www and the PhpStorm docs, but haven't been able to find a solution for it.
As of version 6, PHPStorm has "File Watchers"
Open your project preferences.
Select File Watchers from the left hand list of options.
Click the + symbol at the bottom of the empty right hand panel.
Select <custom>
You will have to set up a command line for PHPUnit, it wont be the integrated testing, but you can have errors output to the console (which is good a good start!)
Various macro options are available to you, so you can include (for example) $FileNameWithoutExtension$Test.php in the arguments passed to your command line.
I personally had to set up two watchers. The first detected modifications to project files, and the second detected changes to test files (the second did not append Test.php to the filename) I also created a new project scope to exclude the tests directories from the first watcher.
You may also want to turn off immediate synchronisation, as this causes PHPUnit to run when PHPStorm auto-saves files.
My other settings are like:
File Type: PHP files (PHP)
Scope: Project excluding tests
Program: /path/to/php
Arguments: /path/to/phpunit --configuration /path/to/phpunit.xml.dist /path/to/tests/$FileNameWithoutExtension$Test.php
Working directory: $FileDir$
Output paths: $FileDir$
No output filters set, syntax error checks enabled, and console showing errors.
PHPUnit watcher named as hot phpunit runner
https://github.com/slavahatnuke/hot-phpunit-runner
You can also have a look at TDDRunner
It is console tool that execute PHPUnit autmaticly on file changes. You can also configure PHPUnit by excuting only one file ot whatever.
/usr/bin/tddrunner --group=test
There's a a German article providing further detail.
In 2017 Jetbrains release a feature that allow auto-run for tests.
It's located in the Run console, therefore it's linked to the run settings, easing the setup of this autorun.
See https://blog.jetbrains.com/phpstorm/2017/07/autorun-phpunit-tests-in-phpstorm-2017-2/

Is it possible to run HTML test suite from PHPUnit?

I want to measure code coverage of an HTML test suite for selenium. Therefore I want to use PHPUnit in order to execute the suite, because PHPUnit has nice support for code coverage analysis.
Therefore: Is it possible to run an HTML test suite from PHPUnit?
Short answer
Running individual HTML test files is not a problem, running HTML suites files however does not seem to work. As long as you put all the HTML test files from a suit in a directory by themselves you can just run runSelenese($folderName)
Long answer
I had no idea that running Selenium HTML files directly was even possible until I did some more digging.
What I used to do is convert/export them first with the Selenium IDE PHP Formatter plugin for Firefox.
Apparently this is not necessary. All the way at the bottom of Chapter 17. PHPUnit and Selenium the manual states:
Using the runSelenese($filename) method, you can also run a Selenium test from its Selenese/HTML specification. Furthermore, using the static attribute $seleneseDirectory, you can automatically create test objects from a directory that contains Selenese/HTML files. The specified directory is recursively searched for .htm files that are expected to contain Selenese/HTML. Example 17.5 shows an example.
Example 17.5: Use a directory of Selenese/HTML files as tests
<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class SeleneseTests extends PHPUnit_Extensions_SeleniumTestCase
{
public static $seleneseDirectory = '/path/to/files';
}
?>
Once you have your Tests in PHPUnit you can use it to get code coverage from the Selenium Server.
Again, from the manual:
PHPUnit_Extensions_SeleniumTestCase can collect code coverage
information for tests run through Selenium:
Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
In your webserver's php.ini configuration file, configure PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.
In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use
protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';
to configure the URL for the phpunit_coverage.php script.
As this can be a bit of a hassle to set up, I've always just used the Page Coverage plugin to get an insight into HTML page coverage.
Unit testing PHP HTML output is a great idea. More people should do it, but turns out it has not been that simple in the past.
I had the same problem and can up with this: https://packagist.org/packages/phpfui/html-unit-tester It can validate HTML and CSS from a URL, file, or string snippet.
To do what you want, you should set up a local web server that your tests can hit for your application. You probably have this already, but make sure it works for your local env and any server you run tests on.
Then you can use the assertValidUrl method with a path to your local server. Since you are not comparing to a template, but just testing that the HTML is valid, you can quickly find recently introduced HTML errors.
Hope this helps.

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