Class 'PHPUnit_Framework_TestCase' not found in Symfony2 project - php

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.

Related

Access phpunit inside my project folder. phpunit: command not found

I have installed phpunit by opening gitbash of my windows system and running the following command.
composer require --dev phpunit/phpunit ^9
It installed successfully and when I type following command
./vendor/bin/phpunit --version
it returns PHPUnit 9.5.21 . I think phpunit is installed in this location
C:\Users\ERIKSON\vendor\phpunit
Now I have a project in my xampp folder
C:\xampp\htdocs\erikson-project\wp-content\plugins\youtube-plugin
when I open GitBash in this project location and run the command PHPUnit it says
bash: phpunit: command not found
. How can I solve this ? Please help.
I have composer.json file inside youtube-plugin folder and the code is
C:\xampp\htdocs\erikson-project\wp-content\plugins\youtube-plugin\composer.json
{
"require-dev": {
"phpunit/phpunit": "9.5"
}
}
Do I need to add anything in my windows system environment variable?

Execute PHPUnit when is installed as part of a project

I have added composer to a standalone project as:
{
"require-dev": {
"phpunit/phpunit": "5.4.*"
}
}
After I run composer require "phpunit/phpunit=5.4.*" --dev the libraries gets installed under vendor/. I have write a small test case and I put it under tests/CollectionTest.php and I want to run it but ....
# phpunit
bash: phpunit: command not found
# phpunit --bootstrap vendor/autoload.php tests
bash: phpunit: command not found
I have also added this:
~/.composer/vendor/bin/
to ~/.bash_rc file. Did I miss something? How do I execute the test case?
PHPUnit is installed in vendor folder in your project directory rather than in global directory. Try to cd into your projects directory and run the PHPUnit by:
$ cd /{project-directory}
$ ./vendor/bin/phpunit
Detailed test setup can be configured with the phpunit.xml file.
You should find an alias in the bin directory to the ./vendor/phpunit/phpunit/phpunit path. So try:
>bin/phpunit
or
>vendor/phpunit/phpunit/phpunit
Hope this help

PHPUnit fails on HHVM because of Fatal error: Class undefined: PHP_Token_HASHBANG

On my Travis CI builds ive phpunit failures when testing in HHVM. This is the full exception:
PHPUnit_Framework_Exception: Fatal error: Class undefined: PHP_Token_HASHBANG in phar://phpunit-4.5.0.phar/php-token-stream/Token/Stream.php on line 185
Running PHPUnit 4.8.14 with the following commandline:
phpunit --verbose --coverage-clover build/logs/clover.xml
How to fix this?
The issue isn't very clear to me, anyway it was related to the PHPUnit version in HHVM. In order to fix this i've added phpunit as a dependency to my project
"require-dev": {
"phpunit/phpunit": "4.*"
},
so that it is updated to the latest 4.x version, than updated my travis script
script:
- vendor/bin/phpunit
This fixed.

PHP Unit test Error - PHPUnit_Util_DeprecatedFeature_Logger

I have installed PHPUnit with the composer. I have below line in composer.json
"require-dev": {
"phpunit/phpunit": "~4.3"
},
When I run my test case I am getting below error. I have searched for this error but I could not find possible related solutions in web:
Vishal#VISHAL-PC c:\xampp\htdocs\V4\test\Core
phpunit ResponseTest
PHPUnit 4.3.5 by Sebastian Bergmann.
Fatal error: Class PHPUnit_Util_DeprecatedFeature_Logger contains 1 abstract met
hod and must therefore be declared abstract or implement the remaining methods (
PHPUnit_Framework_TestListener::addRiskyTest) in C:\xampp\php\pear\PHPUnit\Util\
DeprecatedFeature\Logger.php on line 201
I have no clue where this is coming from. Any suggestions, link or comment will be much appreciated guys.
The PHPUnit_Util_DeprecatedFeature_Logger class was removed in PHPUnit 4.2. If your installation of PHPUnit 4.3 tries to use that class this means that you're installation is messed up somehow.
You still continue to run your phpunit from previously installed location. To run it from composer add following allias to your .bashrc file
sudo nano ~/.bashrc
alias phpunit="/{path_to_project}/vendor/bin/phpunit"

PHPUnit: Failed opening required `PHPUnit_Extensions_Story_TestCase.php`

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)

Categories