I installed PHPUnit with:
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
Trying to run a simple test, but getting:
Fatal error: require_once(): Failed opening required 'PHPUnit_Extensions_Story_TestCase.php'
How do I install PHPUnit_Extensions_Story_TestCase?
The test is simply:
class TestFunctions extends PHPUnit_Framework_TestCase {
public function test_str() {
$this->assertEquals('foo', 'bar');
}
}
Unfortunately none of the suggested fixes worked for me. The typical response is to install the phpunit/PHPUnit_Story module. While that will put you in the right direction, it did not solve my problem.
I registered an autoload function in my boostrap.php file. This most likely replaced the autoload function, registered by PHPUnit, used to autoload PHPUnit's classes. I commented my autoload function implementation and the issue went away.
EDIT
In response to #user3265472; It's been a while since I've worked on this, but I want to say that the "fix" was to set the include paths at the beginning of the bootstrap.php file and then manually loading the classes as you would normally:
/**
* Configure include paths used by the unit tests.
*
* #return void
*/
function configure_include_paths()
{
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . "/mylib");
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . "/mylib2");
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(__FILE__)) . "/lib");
}
configure_include_paths();
This allowed me to do something like the following at the beginning of every file:
require_once("MyClass.php");
instead of having to determine where the class was in relation to the current class file.
I also want to say that no matter what I did I couldn't get class auto-loading to work as I would have liked. I hope this helps.
You need to install phpunit/PHPUnit_Story package:
sudo pear channel-discover pear.phpunit.de
sudo pear install phpunit/PHPUnit_Story
Or manually from github repository.
Like Bakyt Abdrasulov mentioned, this seems an autoloader issue.
See his comment:
"I fixed it by using #include_once instead of require_once in my autoload function."
If you install phpunit with composer these errors will not come
step 1:
Create a composer.json file in your project root:
{
"require-dev": {
"phpunit/phpunit": "4.6.*",
"phpunit/phpunit-selenium": ">=1.4",
"phpunit/dbunit": ">=1.3",
"phpunit/phpunit-story": "*",
"phpunit/php-invoker": "*"
},
"autoload": {
"psr-0": {"": "src"}
},
"config": {
"bin-dir": "bin/"
}
}
step 2:
Install composer into your project using:
curl -sS https://getcomposer.org/installer | php
Ensure composer is executable:
chmod +x composer.phar
Let composer install the dependencies:
./composer.phar install --dev
Check you have a project specific phpunit version installed:
bin/phpunit --version
the above specified is a softlink
ls -la bin/phpunit
bin/phpunit -> ../vendor/phpunit/phpunit/phpunit
Afterwords you can make softlink of 'phpunit' from vendor directory into directory of php in use.
This will remove all warnings related to
PHP Warning: include(classes/PHPUnit_Extensions_Story_TestCase.php)
PHP Warning: include(): Failed opening 'classes/PHPUnit_Extensions_Story_TestCase.php'
PHP Warning: include(classes/Composer\Autoload\ClassLoader.php)
Related
I have already tried searching for this question and seen a couple of answers, but no luck...
I have composer installed with Slim Framework v3.
I am using autoload for my files using PSR-4 in the composer.json file like this:
"autoload": {
"psr-4": {
"App\\": "App"
}
}
And this is my folder structure:
I am running it on a localhost Mac OS X El-Capitan using Apache 2.4 and everything works like magic.
But when I upload it to my Production Linux server (also with Apache 2.4), the autoload seems to be extremely confused and I am getting errors like these:
Warning: include(/home/friendsapp/public_html/vendor/composer/../../app/Middleware/AuthMiddleware.php): failed to open stream: No such file or directory in /home/friendsapp/public_html/vendor/composer/ClassLoader.php on line 412
Warning: include(): Failed opening '/home/friendsapp/public_html/vendor/composer/../../app/Middleware/AuthMiddleware.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/friendsapp/public_html/vendor/composer/ClassLoader.php on line 412
Fatal error: Class 'App\Middleware\AuthMiddleware' not found in /home/friendsapp/public_html/public/index.php on line 5
I have namespaced my classes exactly according to my folder structure.
<?php
namespace App\Middleware;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \App\Middleware\Middleware;
use \App\Share\ErrorCode;
use \App\Models\ResultMessage;
use \App\Mappers\AccessTokenMapper;
class AuthMiddleware extends Middleware {
Any help would be most appreciated! :)
Looking at the path in the errors /app/Middleware/AuthMiddleware.php
It appears the issue is caused by a namespace conflict of App\\ being pointed to /app in your production environment as opposed to your PSR-4 declaration pointing to /App.
To avoid conflicts and map all of the namespaces of a specified directory you can use the autoload classmap or config optimize-autoloader (optional) options in composer.json in order to define the physical path of all the files and objects in the specified directories for composer to load. Additionally with the PSR-4 declaration, any files not found in the classmap paths will be attempted to be loaded from the App namespace path declaration(s). For example when using the exclude-from-classmap option.
"config": {
"optimize-autoloader": true
},
"autoload": {
"psr-4": {
"App\\": "App/"
},
"classmap": [
"App/",
],
}
After making the change in your composer.json, be sure to run php composer.phar update --lock in your development environment.
Then after uploading the composer.lock and composer.json files to your production environment, run php composer.phar install --no-dev -o or php composer.phar dump-autoload --no-dev -o from the production environment.
The -o option will force the optimize-autoloader classmapping to run and --no-dev will prevent the development packages (require-dev) from being installed. Using optimize-autoloader is recommended for production environments.
As a general practice, anytime you deploy your development changes to your production environment you need to run php composer.phar install --no-dev -o See How to deploy correctly when using Composer's develop / production switch?. This way the changes applied from your development environment using php composer.phar update are installed in your production environment correctly.
For my production server the following worked:
composer install --no-dev -o
then restart php
on serverpilot:
rm -rf vendor/*
composer5.6-sp install --no-dev -o
sudo service php5.6-fpm-sp restart
I'm trying to make a little CLI tool and package it up with composer.
Below is an extremely simplified version of the program, but it's enough to demonstrate the problem I'm encountering.
The project has one dependency, and one "binary" file
composer.json
{
"name": "alice/yamldump",
"version": "0.2.0",
"bin": [
"bin/yamldump"
],
"require": {
"symfony/yaml": "2.5.3"
}
}
bin/yamldump
#!/usr/bin/env php
<?php
// use Yaml namespace
use Symfony\Component\Yaml as Yaml;
// autoload
require_once "vendor/autoload.php";
// read yaml
$yaml = file_get_contents(sprintf("%s/%s", getcwd(), $argv[1]));
// create parser
$parser = new Yaml\Parser();
// parse the yaml
var_dump($parser->parse($yaml));
So when I install it globally, I get this
$ composer global require alice/yamldump=dev-master
Files are installed to
~/.composer/vendor/bin/yamldump -> ../alice/yamldump/bin/yamldump
~/.composer/vendor/alice/yamldump/
~/.composer/vendor/symfony/yaml/
This is a problem, because I did not intend to globally install symfony/yaml and my package's vendor/autoload.php can no longer find the Yaml package in the proper location.
I don't mind that symfony/yaml was installed globally, but it would make sense to me that composer global require would install the package like this:
~/.composer/vendor/bin/yamldump -> ../alice/yamldump/bin/yamldump
~/.composer/vendor/alice/yamldump/
~/.composer/vendor/alice/yamldump/vendor/symfony/yaml/
After all, what if I have Package A that depends on symfony/yaml=2.5.3 and Package B that requires symfony/yaml=2.6.x?
If the composer global require installs dependencies to ~/.composer/vendor/*, each globally required package can't maintain it's own version requirement of its dependency...
I know this is sort of a convoluted problem, but I really don't know how to begin fixing it.
The goal
A user should be able to
$ composer global require alice/yamldump=dev-master
$ yamldump sample.yml
The error
$ yamldump sample.yml
Warning: require_once(vendor/autoload.php): failed to open stream: No such file or directory in /Users/alice/.composer/vendor/alice/yamldump/bin/yamldump on line 8
Fatal error: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.:') in /Users/alice/.composer/vendor/alice/yamldump/bin/yamldump on line 8
The question
Here it is in black & white:
How am I intended to write the require "vendor/autoload.php" line and have it work for both locally installed packages and globally installed packages?
Targeting vendor/autoload.php is generally not a good idea and only works if you run the script from the correct directory. The following should serve you better:
require_once __DIR__.'/../vendor/autoload.php';
However, this still might be an issue if your application is installed as a dependency. In that case, you might need something more substantial:
if (
(!$classLoader = includeIfExists(__DIR__.'/../vendor/autoload.php')) &&
(!$classLoader = includeIfExists(__DIR__.'/../../../autoload.php'))
) {
echo 'You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -sS https://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL;
exit(1);
}
This first looks for the autoloader in the location you would expect it to be if you are working directly on your application. If that does not exist, it looks where the autoloader would be if your application is installed as a dependency.
I have project on Symfony 2 and i would like use PHPUNIT on Windows 7.
On githut phpunit is:
Composer
Simply add a dependency on phpunit/phpunit to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on PHPUnit 3.7:
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
For a system-wide installation via Composer, you can run:
composer global require 'phpunit/phpunit=3.7.*'
Make sure you have ~/.composer/vendor/bin/ in your path.
First i use system-wide installation but i dont know when this installed.
Next i add to my composer.json require-dev.
This installed phpunit in C:/wamp/www/myproject/vendor/symfony. Next i try commands:
composer install --dev
And i can't use phpunit. In cmd.exe i enter "phpunit" and i have error:
'phpunit' is not recognized as an internal or external command operable program or batch file
How can i use phpunit? I have Windows 7, Wamp server and php 5.4.12.
When you install PHP-Unit in windows via composer, the global installation will create files in
C:\Users\YOUR_USERNAME\AppData\Roaming\Composer
To execute phpunit easily via command line you need to add path of phpunit.bat file in windows Environment Variables. For this:
Right click My Computer
Go to Properties -> Advance system settings and
Click Environment variables from the Advance tab.
Now add C:\Users\YOUR_USERNAME\AppData\Roaming\Composer\vendor\bin to the windows PATH.
You can now run the phpunit from command. Note that you may need to restart your command prompt for the changes to take effect.
The bin file of packages are put in the configured bin directory. By default, this is vendor/bin and when you use the symfony standard edition, this is the bin folder.
To execute this bin file, run ./bin/phpunit (or ./vendor/bin/phpunit when not using the Symfony Standard Edition)
Windows users have to put this in double quotes: "bin/phpunit" (or "vendor/bin/phpunit")
composer require --dev phpunit/phpunit ^7
The above example assumes, composer is already on your $PATH variable.
You composer.json should look similar to;
{
"name": "vendor_name/package_name",
"description": "This project is for practicing writing php unit tests",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Umair Anwar",
"email": "umair.anwar#gmail.com"
}
],
"autoload": {
"classmap": [
"src/"
]
},
"require-dev": {
"phpunit/phpunit": "^7",
"phpunit/dbunit": "^4.0"
}
}
Easiest way to install phpunit via composer is to run from project root.
$ composer require phpunit/phpunit
What this would do is, it will create a phpunit folder inside vendor/bin
and you can run unit tests like this..
$ ./vendor/bin/phpunit
Too simple operation on Windows with composer and works for me following way:
Install composer
https://getcomposer.org/doc/00-intro.md#installation-windows Go to
your symphony folder e.g C:\wamp64\www\symfony\UserManagement where is
composer.json and run this command.
Should be register with global to not have issue $phpunit bash: phpunit: command not found
//old version is 5.7 new 6.4 or put newest version.
composer global require --dev phpunit/phpunit ^5.7
I remember futzing around with the composer dependency stuff for phpunit and never could get it to work.
Instead, from your git bash shell:
mkdir ~/bin
cd ~/bin
curl https://phar.phpunit.de/phpunit.phar > phpunit
chmod +x phpunit
exit out of bash and then start a new bash session.
And you should be good to go. You can echo $PATH to verify you have a path to ~/bin but one seems to added by default.
https://phar.phpunit.de/phpunit.phar
I also came across the same issue and find out the solution by following steps
To run PHPUnit in Windows 7 under WAMP installation
Composer Install
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
Simply Set Environment Variable
The php unit will be install in a vendor dir in vendor/bin
Path : C:\wamp\www\myproject\vendor\bin;
Open a new Command Prompt
C:\Users\guny >phpunit --version
PHPUnit 3.7.30 by Sebastain Bergmann
i install composer for windows using this link http://getcomposer.org/download/ > http://getcomposer.org/Composer-Setup.exe
my web server is WAMP php 5.4 with openssl enabled.
i created composer.json with this code
{
"require": {
"doctrine/orm": "*"
}
}
and run with this code in .php file
<?php
// bootstrap.php
// Include Composer Autoload (relative to project root).
require_once "vendor/autoload.php";
and i got error Warning: require_once(vendor/autoload.php): failed to open stream
how to get composer's autoload.php?
why composer does not generate it?
how to use doctrine without composer?
Did you actually look to see if a vendor/autoload.php was created?
Did composer throw any error messages? Unless you got an error then I'm willing to bet that a vendor/autoload files was made. Is there anything in vendor?
I'm guessing that your bootstrap.php is not in your root directory (same directory as composer.json). If so you need to adjust the path in your require statement.
Remove the autoload part from your composer.json, then run composer install
This will generate an updated autoload object, good luck!
"autoload": {
"classmap": [
"..."
]
}
I'm developing a project in symfony2 and I'm new with unit testing.
I have installed PHPUnit 3.6.10 via PEAR and it works from the terminal when I digit the phpunit command.
I wrote my first test class following the SensioLab suggestions (http://symfony.com/doc/current/book/testing.html) but when I use the command
php -c app src/My/CalendarBundle/Tests/Calendar/CalendarTest.php
I got
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /Library/WebServer/Documents/calendar/src/My/CalendarBundle/Tests/Calendar/CalendarTest.php on line 7
Here you are my test class:
<?php
namespace My\CalendarBundle\Tests\Calendar;
use My\CalendarBundle\Calendar\Calendar;
class CalendarTest extends \PHPUnit_Framework_TestCase
{
public function testGetNextMonth()
{
$calendar = new Calendar('09', '2012', null);
$result = $calendar->getNextMonth();
$this->assertEquals(10, $result);
}
}
I read this discussion Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...? but the symfony documentation doesn't say to include PHPUnit...
What I'm doing wrong?
Thank you
I just had a similar issue (with DoctrineFixturesBundle), and solved it by adding PHPUnit to Symfony (as opposed to installing PHPUnit via PEAR).
What worked for me was:
1) add "phpunit/phpunit": "4.0.*" to the require section of composer.json:
{
"require": {
...
"phpunit/phpunit": "4.0.*"
}
}
2) running from the commandline:
php composer.phar update phpunit/phpunit
In case someone runs into a similar issue.
1- Install PHPUnit following this procedure:
$ pear config-set auto_discover 1
$ pear install pear.phpunit.de/PHPUnit
2- Run your tests as described here:
$ phpunit -c app/ src/My/CalendarBundle/Tests/Calendar/CalendarTest.php
The -c app/ option will be looking for a configuration file in the app/ directory. This configuration file is app/phpunit.xml.dist.