I installed the latest release of PHPUnit using the phar according to the documentation. I have some scripts to compile a code coverage report using the PHP_CodeCoverage object. With the update, when I try to include phpunit.phar phpunit is actually run. The script outputs the list of options for running PHPUnit and then exits.
How can I include the file so that I have the PHPUnit objects available in my script?
The line that I tried was
require '/usr/local/bin/phpunit.phar';
PHPUnit creates and registers an autoload function for including its files in phpunit.phar. I was able to use the PHPUnit classes in my script by extracting this function.
The code is available on Github
Basically, all the class paths are mapped and then included via require phar:///user/local/bin/phpunit.phar/<class path>
Related
I wan't to run php classes scripts from my IDE with PHP CLI.
When I launch files in which there are other classess specified, PHP shows error - because it can't find this classes. What should I add to command-line to automatically include my autoloader into the launching script?
I nedded to just add an Interpreter option:
-d auto_prepend_file="path to autoloader"
I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.
I'm hoping someone can spot what I've forgotten to do. Here are my steps:
Downloaded and unpacked the ZendFramework-2.3.5 into /usr/share.
Updated include_path in my php.ini file to include '/usr/share/ZendFramework-2.3.5/library' per the INSTALL.md, and restarted Apache to confirm the path is set (now ".:/usr/share/php:/usr/share/ZendFramework-2.3.5/library").
Created a test script in my web document root (using the class 'CamelCaseToUnderscore' as an example):
use Zend\Filter\Word\CamelCaseToUnderscore;
$filter = new CamelCaseToUnderscore();
echo $filter->filter('BigsAndLittles');
...and I get the fatal error "class 'zend\filter\word\camelcasetoseparator' not found".
In order to do use Zend classes like this, do I need to do some additional configuration or create an autoloader or something to find them? Seems like this should have worked. If I include the CamelCaseToUnderscore.php file in a require_once statement, then I get a fatal error that it's parent class doesn't exist (CamelCaseToSeparator.php). What am I missing?
You can use require 'Zend/Mvc/Application.php' to test if your include path is correct, but you will need an autoloader:
http://framework.zend.com/manual/current/en/modules/zend.loader.standard-autoloader.html.
You can find an example here (lines 18-20):
https://github.com/zendframework/zf2/blob/master/demos/Zend/Feeds/consume-feed.php
I strongly suggest using composer as it will save you a lot of time troubleshooting your include paths, but it also allows you manage version better. It makes it easier for other developers and to deploy your code.
Starting with composer is very easy, just install it and create composer.json:
https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup
Run:
composer require zendframework/zendframework
Composer will download all libraries to vendor folder and will generate an autoloader, all you have to do is to include
require 'vendor/autoload.php';
https://getcomposer.org/doc/01-basic-usage.md#autoloading
Most popular PHP frameworks use composer for managing dependencies:
https://github.com/zendframework/zf2/blob/master/composer.json
https://github.com/symfony/symfony/blob/2.7/composer.json
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.
I have a directoy structure, and all the classes of the business logic are placed in the app_dir/lib/ directory. I would like to generate unit tests for all the classes from this lib/ folder.
The problem is, that I haven't found any option to specify the source directory, only the source file:
from app_dir:
$ phpunit --skeleton-class lib/
Error: "lib/.php" could not be opened.
Is it the only solution to write my own php script, which iterates through the /lib folder
and calls the skeleton generator for every file found? And how can I specify the output folder, where all the generated test files are placed?
To generate skeleton tests, you want --skeleton-test not --skeleton-class. This will extract the filename without the extension and pass it to phpunit.
for file in *.php; do phpunit --skeleton-test "${file%.*}"; done;
I have no idea how to change the output directory which you would need if you want to run the command multiple times. I suppose a better one-liner would only select files not ending with "Test.php".
From Sebastian Bergmann's blog:
As of changeset 2764, PHPUnit 3.3's
command-line test runner accepts a
directory as its argument.
Given a directory, the test runner
will recursively scan the directory
for *Test.php files, build a test
suite out of the *Test classes, and
run it.
With PHPUnit >= 3.3 you should be able to execute just:
phpunit lib