How to use phpunit with php4 code - php

I have the following example test code (PHP4) I need to run with phpunit:
<?php
require_once 'PHPUnit/Framework.php';
class RemoteConnectTest extends PHPUnit_Framework_TestCase
{
public function setUp(){ }
public function tearDown(){ }
public function testConnectionIsValid()
{
$this->assertTrue(true);
}
}
?>
which does not run with phpunit:
PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Framework.php' ...
How do I need to setup my environment to make this work (Linux, Ubuntu 12.04)? Do I need to set a search path? Change php.ini? I AM NOT ABLE TO CHANGE THE TEST CODE ITSELF.

The answer is an incompatibility between the used versions. Newer versions of phpunit do have a different internal setup (see example for 3.7 here) which are different from older phpunit versions.
One need to 'downgrade' phpunit, as example as follows:
sudo pear uninstall phpunit/PHPUnit
sudo pear install phpunit/PHPUnit-3.2.8
to install version 3.2.8, for example. It can be verified the existance of the file Framework.php:
> ls /usr/share/php/PHPUnit
Extensions Framework Framework.php Runner TextUI Util
The example code should work now when called as
phpunit SimpleTest.php
(assuming identical name of class and file [excluding the php of course]).

Since you can run phpunit from command line, you most likely have the source code installed with PEAR already (or composer, which is less likely). But path to framework is not in global include paths (or in system variable PATH).
The path to PHPUnit is usually inside PEAR. And PEAR is usually near php installation. You can always use global filesearch in terminal with
find / -name 'Framework.php' 2>/dev/null
You either need to either
edit php.ini and add path to PHPUnit in include_path directive
require_once absolute path
use autoloading (but thats only from PHP 5.3)

Related

Namespace that seems it doesn't exist is working but why?

I have this exercice folder that gave me instructions on how to install the web application:
So this folder contains a .sh file that installs the necessary things:
npm install
curl -o phpunit -L https://phar.phpunit.de/phpunit-7.phar
chmod +x phpunit
./phpunit --version
So basically this adds a file called phpunit inside root folder. So by far I kind of understand what's going on, so this installs node dependencies and also installs phpunit (I think so), but this project has a test example unit:
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class HelloWorldTest extends TestCase
{
public function testWhenGetDataFromHelloWorld(): void
{
$this->assertEquals('Hello World', 'Hello World');
}
}
At this point is where I don't get how is use PHPUnit\Framework\TestCase; working, since I don't have any folder called PHPUnit, I get that use is something like importing a class, but still, how is this working (when I run the test it does not give me any kind of error)?, also, it appears that vscode detects the import as an error because it seems that it doesn't exist.
My file tree is as follows:
Can someone explain me, please?
This is because all the dependencies are in the phpunit-7.phar file, which was named phpunit by that script.
"The easiest way to obtain PHPUnit is to download a PHP Archive (PHAR) that has all required (as well as some optional) dependencies of PHPUnit bundled in a single file."
PHPUnit - Installing PHPUnit - PHP Archive (PHAR)

PHPUnit autocompletion on Windows

NetBeans does not seem to locate the PHPUnit sources, therefore I can not use autocompletion on for instance PHPUnit_Framework_TestCase.
I run PHPUnit from within NetBeans using a cmd, on Windows using EasyPHP, generated with:
echo #php "%~dp0phpunit.phar" %* > phpunit.cmd
Note that this is the recommended way, older variants are not longer supported as I noticed.
Where are the PHPUnit sources located and how can I tell NetBeans to account for support?
I came up with a workaround:
Install the sources of PHPUnit globally using Composer:
composer global require "phpunit/phpunit=4.7.7"
And finally add the following path to the include directory of the project:
%APPDATA%\Composer\vendor\phpunit\phpunit\src
This will do the trick. However make sure the installation of PHPUnit and the sources are of the same version to prevent complications.
Note that the sources can be installed using a lot of different ways, but keep in mind to use the right include directory.

How to set INCLUDE_PATH variable to PHPUnit libraries under Composer directory for Behat

I have some test under features directory. But when I try to run behat I get the following error:
$ ls
behat.yml features
$ behat
PHP Warning: Module 'memcache' already loaded in Unknown on line 0
PHP Warning: require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory in $HOME/workspace/ums/api/trunk/tests/functional/features/bootstrap/FeatureContext.php on line 24
PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in $HOME/workspace/ums/api/trunk/tests/functional/features/bootstrap/FeatureContext.php on line 24
Since PHPUnit is no longer available to install using PEAR, I finally use Composer. So have my PHPUnit libraries installed under
$ sudo find / -name PHPUnit
/HOME/.composer/vendor/phpunit/phpunit/PHPUnit
/HOME/.composer/vendor/phpunit/phpunit-mock-objects/PHPUnit
/tmp/ZendFramework-1.11.0/library/Zend/Test/PHPUnit
/tmp/ZendFramework-1.11.0/tests/Zend/Test/PHPUnit
/usr/share/php/Zend/Test/PHPUnit
/usr/share/php/Zend-2.0/Test/PHPUnit
Which is the best way to set include_path without changing php.ini file and not use set_include_path() method in each FeatureContext.php example type class ?
Any help would be appreciated!
I ran into the same error using phpunit along with Behat, I did manage to overcome it with the following code:
require_once __DIR__.'/../../vendor/autoload.php';
require_once __DIR__.'/../../vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';
Given that the 'bootstrap' folder is only one two folders away from the root of the project. Hopes it helps.
Usually I have PHPUnit installed globally (via composer) on my dev and test (CI server) machines so I wont run into any trouble when running the tests. Also, in order to have the auto completion properly working of my favorite IDE (PhpStorm) I include the library in the composer.json of the project as a dev requirement. In the case of a project that uses Behat and is developed by a team the best way is to include it in the project so no developer would run into trouble when running the test.
You want to use the autoloader that comes with composer. Try adding
require_once 'path/to/vendor/autoload.php'
before using PHPUnit

Cannot find PHPUnit in include path phpstorm

Cannot find PHPUnit in include path is the error message I get when I try to run my code in phpstorm.
In the PHP Settings, my PHP level language is 5.4 (traits, short array syntax, etc.) and interpreter is Name(5.4.7) where Name is user defined.
The error appears when I try to run the code and the exact message is this:
C:\xampp2\php\php.exe C:\Users\DELL\AppData\Local\Temp\ide-phpunit.php --no-configuration
C:\xampp2\htdocs\ft-website
Testing started at 2:34 PM ...
Process finished with exit code 1
Cannot find PHPUnit in include path (.;C:\xampp2\php\PEAR)`
Just encountered this problem myself.
I'm unsure why it is unable to find PHPUnit in the include path (despite the fact that it is there, albeit in all lowercase).
I got around this by changing my IntelliJ Preferences for PHPUnit (under PHP --> PHPUnit). I changed the PHPUnit library settings to Use custom loader and then specified the path to the phpunit executable. On my mac, that was /usr/local/Cellar/php54/5.4.26/bin/phpunit.
UPDATE:
I just discovered that pear now installs PHPUnit as a phar named phpunit. It previously installed the PHPUnit source, which was really nice for reference and code completion in PHPStorm. I think that this is the reason things aren't working any longer with PHPStorm, because it's expecting a php executable and not a php archive.
So, I'm moving away from using pear to install PHPUnit, and I'm using composer instead. This bundles PHPUnit directly as a dependency of my project, which makes it more portable than a system dependency. You'll need to add vendor/phpunit/phpunit as a PHP include path in your PHPStorm preferences. This will serve two purposes:
PHPStorm can find the phpunit executable now
PHPStorm will index all the PHPUnit classes now, so you'll get auto-complete. Yay!
To do this, go to Languages and Frameworks in the PhpStorm settings.
If you click on PHP, on the right you have your include paths
I had this problem after adding PHPUnit via composer.
I fixed this by choosing use custom autoloader in Settings -> Languages and Frameworks -> PHP -> PHPUnit, then adding /vendor/autoload.php as the location of the custom autoloader.
In addition to Ben's answer, in phpStorm 7.1.3 it works by specifying the phpunit.phar location under the "use custom loader" option, for example:
"Path to script: /usr/share/php/phpunit.phar"
This is the way to do it without using composer, and using your global phpunit.
Phpunit now comes with a phar file. My path was /usr/local/Cellar/phpunit/5.0.0/libexec/phpunit-5.0.0.phar
PhpStorm Preferences -> Path to phpunit.phar -> select the phpunit phar
That's all, good stuff.
I had this same problem for PHPStorm 2017 using Vagrant. First go to Settings -> Languages and Frameworks -> PHP and Add a remote interpreter, then go to Settings -> Languages and Frameworks -> PHP -> PHPUnit click the + on top and click by Remote Interpreter. If you're using Composer autoloader, then enter your full Vagrant path to your autoloader file.
For future readers, to fix the same issue when using PHPStorm + Vagrant + PHPUnit by defining path to the global phpunit.phar (not composer)
While the tests runned successfully, I was missing the code completion. I had confirmed that phpunit was in the PATH on my local machine, and to make sure, I had added /usr/local/bin to the PHP Include paths in PHP Settings. Still no code completion.
The reason: PHPstorm was looking for phpunit.phar, not just phpunit. To fix, on my local machine I established a symlink phpunit.phar pointing to phpunit:
$ cd /usr/local/bin/
$ sudo ln -s phpunit phpunit.phar
Then I reindexed the project, and code completion started working.
For those that are looking at it in 2020, starting from PHPStorm 2019 this is under:
Languages and Frameworks->PHP->Test Frameworks
Choose to add a new library
Select "PHPUnit"
Select "Path to phpunit.phar" radio
You'll have an automatic option to download PHPUnit

How can I get IDE autocomplete for PHPUnit?

I am using Symfony2, everything is installed, my tests work so good so far.
I'd like to get an autocompletion of PHPUnit's methods.
Symfony's WebTestCase class extends from PHPUnit_Framework_TestCase just like below:
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
The parent class is highlighted as not existing although.
How can I tell my IDE to use PHPUnit's library?
I am using PHPStorm
PHPUnit is available by path /Users/myUser/pear/share/pear/PHPUnit/
Add it as a library... in the project that you are editing add it to 'External Libraries'.
It should then be included.
For me (Ubuntu 12.04) it was adding this folder as external library:
/usr/share/php/PHPUnit
For PHPStorm users, go to File -> Settings -> Project Settings ->PHP and add the path in there.
PhpStorm 2016.2 introduces a feature which - in this case - is also a bug.
Autocompletion now no longer includes static methods as an option when in $this-> context. (https://blog.jetbrains.com/phpstorm/2016/07/completion-changes-in-phpstorm/#more-10425)
As phpunit tests are defined as static methods but called via $this->, autocomplete for phpunit is now effectively broken.
They've rolled back this change for phpunit in the next EAP (https://youtrack.jetbrains.com/issue/WI-32530).
Workaround until the next stable release: Press CTRL-Space twice; this will then show static methods in the autocomplete field.
You can add package phpunit/phpunit to the require-dev section of your composer.json file.
After running composer install, PHPStorm will know about the PHPUnit classes.
I have OSX, phpunit installed by homebrew and phpstorm 9.0
So how it works for me: open preferences or press cmd+, -> Languages&Frameworks -> PHP -> Include path -> add "/usr/local/Cellar/phpunit/4.7.6/libexec/"
I have PHPStorm 2017.1.4 and my OS is Ubuntu 16.04. I already have phpunit.phar installed in my /usr/local/bin.
I will use ~/WORK/.. for the example paths but you should use the full path /home/myname/WORK/..
What I did is just go to a folder inside my home(like: ~/WORK/) and run:
composer require phpunit/phpunit
After composer was done downloading phpunit I added a new project include path to:
~/WORK/vendor/phpunit/phpunit/src
Now I have all of the PHPUnit autocompletion, I can jump into PHPUnit's source code directly and I can keep the PHPUnit's code updated with composer. I also removed the phpunit.phar from /usr/local/bin and replaced it with a link to ~/WORK/vendor/bin/phpunit

Categories