Testing old PHP versions with PHPUnit 7 - php

I am using Composer for dependencies.
My project's dependencies require PHP 5.5 or later, so I want to test my project on PHP 5.5 - 7.2 (latest) with PHPUnit 7.
I set up Travis CI tests with different PHP versions:
language: php
php:
- '5.5'
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- hhvm # on Trusty only
- nightly
The problem is that the PHPUnit 7 requires PHP 7.1, so I can't test project on PHP 5.5 - 7.0.
I could test old PHP versions with old PHPUnit versions, but the problem is that after PHPUnit 6, PHPUnit's units of code are namespaced, so I will have to write different tests for PHPUnit 4 - 5 and PHPUnit 6 - 7.
How can I test my project on old PHP versions with PHPUnit 7 with Travis CI?

Sebastian Bergmann noticed me that the PHPUnit 4.8 has forward compatibility, so I can use namespace syntax with PHPUnit 4.8, PHPUnit 5.6, PHPUnit 6, and PHPUnit 7.
"the problem is that after PHPUnit 6, PHPUnit's units of code are namespaced, so I will have to write different tests for PHPUnit 4 - 5 and PHPUnit 6 - 7" is not true as PHPUnit 4.8 has the same forward compatibility layer as PHPUnit 5.6. TL;DR: PHPUnit\Framework\TestCase can be used with PHPUnit 4.8, PHPUnit 5.6, PHPUnit 6, and PHPUnit 7. – Sebastian Bergmann
See PHPUnit 4.8 Changelog for details about forward compatibility.

Related

Unit tests with CakePHP 3 and PHPUnit 8 outside project

Is there someway to do unit testing in CakePHP 3.x with the latest version of PHPUnit?
Using composer inside the project is limited to PHPUnit version 6.

TestCase not found in KernelTestCase.php with Symfony 3.2.7

I'm having problems when running Symfony (PHPUnit integrated) tests in my server, it works as expected in my build host but when I upload them to Gitlab CI via Git it throws me the next error:
Class 'PHPUnit\Framework\TestCase' not found in /project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php on line 24
I tried executing phpunit, phpunit ., phpunit tests all with the same results
It was working until a few weeks ago.
My server Symfony version is 3.2.7 and the PHPUnit version is 4.2.6
PHPUnit version is 4.2.6 is very old, and does not contain any Namespaced classes. The Symfony KernelTestCase.php now uses those PHPUnit namespaces to pull on the TestCase class.
There is forward compatibility in PHPUnit 5.4.3, though v5.7, if not 6.0 would be better.
So, you'll want to upgrade your composer.json file and tests to use the new namespaces.

PHPUnit - Version 3.7.21 instead of Version 6 installed

I followed the instructions on the official PHPUnit page to install PHPUnit 6.
composer require --dev phpunit/phpunit ^6.0
However, If I go to the project folder and execute phpunit --version then I get PHPUnit 3.7.21 by Sebastian Bergmann..
Why is PHPUnit 3.7.21 installed instead of PHPUnit 6?
I suppose you have xampp installed? It comes with PHPUnit 3.x.x preinstalled with PEAR which strangely can not be uninstalled with pear uninstall. And since its config is in php root folder, that 3-ish version has priority when running phpunit command (even if you have phpunit installed globally) in CMD or PS. How to fix:
Go to xampp/php folder and delete two files phpunit (with no extension) and phpunit.bat. Usually it's enough to prevent older PHPUnit version from being run but let's be on the safer side:
Go to xampp/php/PEAR dir and delete two folders PHPUnit and PHPUnit2.
Go to Control Panel, then navigate System and Security->System and click "Advanced system settings" link on the left. This will open Advanced tab in System Properties window. Click "Environment Variables" button, then under System Variables select "Path" variable and click "Edit..." button. In the new window, click "New" button and type your path to vendor/bin folder (if you already installed PHPUnit for your project there will be phpunit.bat file). By default for xamp it looks like this: C:\xampp\htdocs\yourprojectname\vendor\bin.
Restart your Command Prompt (or PowerShell) window, then type phpunit --version to see the that it's PHPUnit 6.x.x now.
P.S. (If you want latest PHPUnit do not use ^6.0 in require string, since it will install v6.0.0, write it in composer.lock file that will never update PHPunit to the latest e.g. 6.2.1 version atm). Just use
composer require --dev phpunit/phpunit
to install the latest stable version for your project.
You run your global PHPUnit version which is installed in another folder. To get the installed version you have to go to the vendor/bin folder.
vendor/bin/phpunit --version
PHPUnit 6.0.8 by Sebastian Bergmann and contributors.
In newer versions you can run it with bin/phpunit there should be the executable. When you need another PHP-Version then define it before php74 bin/phpunit.
If you use XAMPP, why don't you update XAMPP version of global PHPUnit to desired.
1.
Read this post, which quickly explains what to do:
StackOverflow post
2.
You can get phar file for version you need here:
proper PHPUnit phar file
3.
Which version of PHPUnit you need depends on your PHP version and you can identify it here:
which phar file version you need
Just click on links: PHPUnit 9 - PHPUnit 8 - PHPUnit 7 - PHPUnit 6 - PHPUnit 5 - PHPUnit 4 and read description explaining which PHP version it works with.

How to update phpunit installed with symfony3

I want to start unit testing my symfony 3 application with the latest version of phpunit. I followed this tutorial. but when i runed this command
phpunit -c
According to the official site https://phpunit.de/ the installed version of php unit is not the latest version (!= 5.6):
PHPUnit 4.8.17 by Sebastian Bergmann and contributors.
the class test is :
class CalculatorTest extends \PHPUnit_Framework_TestCase
{
public function testAdd()
{
$calc = new Calculator();
$result = $calc->add(30, 12);
// assert that your calculator added the numbers correctly!
$this->assertEquals(42, $result);
}
}
I don't know that the framework use a bundle to import the PHPUnit_Framework_TestCase class or use the phpunit installed on my pc ?
And How can i update the phpunit version ?
Class PHPUnit_Framework_TestCase (and other PHPUnit_* code) will be imported from PHPUnit that are you executed.
Symfony is not require specific version of PHPUnit. For example, you can install the test framework as project (dev-)dependency and run it directly:
vendor/bin/phpunit -c app/
But
it's recommended to use the latest stable PHPUnit version, installed as PHAR.
If your PHPUnit version doesn't support your version of PHP, obviously you can use an older version of PHPUnit or update the platform.

PHPUnit won't test my files

I'm trying to test my files in PHP but I keep getting this error message:
"/Users/Nakazai/XAMPP/bin/php" "/Applications/NetBeans/phpunit-old.phar" "--colors" "--log-junit" "/var/folders/ll/twrzpc8j77d3npwp2m_137mm0000gn/T/nb-phpunit-log.xml" "/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/php/phpunit/NetBeansSuite.php" "--run=/Applications/MAMP/htdocs/bank1/Tests"
PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
unrecognized option --run
Done.
Appreciate your help, folks!
PHPUnit do not has command --run which you used in last argument. Check your NetBeans configuration, maybe your IDE try to run PHPUnit older than 4 (actually is version 5) and you have installed newer version.

Categories