How to create PHP-UnitTest case for PHP_Codesniffer standards?` - php

I have created my own codesniffer rules standards.They are working fine. Now I want to test codesniffer rules using PHP UnitTest. PhpCodesniffer has already have their framework for PHPUnit test case.
So using that I have extended their AbstractSniffUnitTest and unit test class at location
Standards/TestRules/Tests/Function/FunctionUnitTest.php and script on which is to be tested kept in Standards/TestRules/Tests/Function/FunctionUnitTest.inc.
But when I am going to run the script by command phpunit PEAR\PHP\tests\AllTests.php, it gives following error.
PHPUnit 3.5.14 by Sebastian Bergmann.
......................................F
Time: 6 seconds, Memory: 10.00Mb
There was 1 failure:
1) TestRules_Tests_Function_FunctionUnitTest::getErrorList
An unexpected exception has been caught: Source file C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.inc does not exist
C:\Program Files\PHP\PEAR\PHP\tests\Standards\AbstractSniffUnitTest.php:138
C:\Program Files\PHP\PEAR\PHP\tests\TestSuite.php:48
FAILURES!
Tests: 39, Assertions: 146, Failures: 1.
Warning: Deprecated PHPUnit features are being used 2 times!
Use --verbose for more information.
It gives error file FunctionUnitTest.inc not found at given location. I have given full permission to folder also verify the path and file location, but it gives same error. I have tested it on linux machine also, but it gives same error.
Is it a my code issue or codesniffer unittest framework issue?

Are your files in the top-level tests or CodeSniffer directory? Note that the abstract test case you're extending is in tests
C:\Program Files\PHP\PEAR\PHP\tests\Standards\AbstractSniffUnitTest.php
but is trying to include the file from CodeSniffer:
C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.inc
Try moving your two files to the other directory.

I see a space near i i nc - make sure if that is mis-typo and correct it, it should work. Other than this I don't see any problem.
Change:
C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.i nc
To
C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.inc

It seems that it has path issue on windows. I tested the test cases on Linux, it works fine. When I checked the actual code, it seems that on there is file mapping from test case file to sniff file. But on window, after file being mapped , the file path contains mixing of forward & backward slahes.But codesniffer not going to allow this. Because of that it is returning that “file is not found error”. Test cases are executing fine on Linux.

Related

Using PHPUnit in PhpStorm using Docker

I have a very specific configuration that I have to hold until I can start moving feature from my old PHP 5 application to somewhere else.
I'm trying to tie my testing environment with PhpStorm but I cant figure it out. Since I'm not using Composer, I have to specify phpunit.phar path (which is /usr/local/bin/phpunit - a symlink to /usr/local/bin/phpunit.phar). I keep having this error.
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Cannot create phar '/usr/local/bin/phpunit', file extension (or combination) not recognised or the directory does not exist' in /opt/.phpstorm_helpers/phpunit.php:181
Stack trace:
#0 /opt/.phpstorm_helpers/phpunit.php(181): Phar->__construct('/usr/local/bin/...')
#1 /opt/.phpstorm_helpers/phpunit.php(250): IDE_PHPUnit_Loader::init()
#2 {main}
thrown in /opt/.phpstorm_helpers/phpunit.php on line 181
The setup on my Docker is
PHP 5.5.9 with PHPUnit 4.8.9
My remote Docker PHP interpreter works in PhpStorm, and my PHPUnit works when I run test in bash (using docker exec phpunit) - so it seems that the pieces are working, mostly the glue doesn't work.
Here are my settings
I just ran into this problem with composer and the issue is you need to set up a PHPUnit interpreter in PHPStorm with the autoload.php file.
Open Settings > Languages & Frameworks > PHP > Test Frameworks
Click the Plus + icon to add a Remote Interpreter
Select the CLI interpreter previously defined and use existing path mappings
Select Use Composer Autoloader
Path to script should be /path/to/vendor/autoload.php
Check off Default configuration file and set it to /path/to/phpunit.xml
I also needed to have the following section in my docker-compose.yml:
services:
my-api:
# Other contents omitted
environment:
- PHP_IDE_CONFIG=serverName=my-api.company.com
Where my-api.company.com is the server definition name from Settings > Languages & Frameworks > PHP > Servers

How to fix the "unrecognized option --run" on NetBeans running PHPUnit

I'm trying to run the PHPUnit into the NetBeans 8.0.2.
If I run # phpunit inside my folder tests all tests run. So it's seems been set right.
But in the NetBeans output I always got:
"C:\nginx\php\5.6.12\php.exe" "C:\nginx\php\5.6.12\phpunit.phar" "--colors" "--log-junit" "C:\Users\...\AppData\Local\Temp\nb-phpunit-log.xml" "--bootstrap" "E:\var\www\...\tests\TestHelper.php" "--configuration" "E:\var\www\...\tests\phpunit.xml" "C:\Program Files\NetBeans 8.0.2\php\phpunit\NetBeansSuite.php" "--run=E:\var\www\...\tests\app\utils\FormatUtilTest.php"
PHPUnit 4.8.2 by Sebastian Bergmann and contributors.
unrecognized option --run
Done.
Maybe the "--run message" it's right, because this command doesn't exist in the PHPUnit manual. But if is that so, how to create another script for the NetBeans execute the tests?
I ran into the same issue yesterday after a PHPUnit update. So I reverted back to PHPUnit 4.7.7 for now, until this is fixed in NB.
I too have encountered this error causing the latest version of PHPUnit not to work with NetBeans v8.0.2.
Further investigating this issue has resulted in determining that there is an incompatibility between NetBeansSuite.php and the latest version of PHPUnit.
The error 'unrecognized option --run' is being thrown by the phpunit.phar and not being thrown by NetBeansSuite.php. I also do not believe the NetBeansSuite.php is even being executed.
phpunit.phar, line 63816 or 63894, is where the exception is being thrown.
Until this is fixed, PHPUnit will not work from NetBeans v8.0.2.
#Pablo: I thank you for opening an issue and have commented on your issue.
PHPUnit v4.7.7 works correctly.
Opened a bug report: https://netbeans.org/bugzilla/show_bug.cgi?id=254276
The answer is: This was indeed a bug in NB and is now fixed in nightly.
Possible solutions:
Upgrade to latest nightly from http://bits.netbeans.org/dev/nightly/
Use PHPUnit 4.7.7 as mentioned by Luuk
"--run" option is used in the NetBeansSuite.php for running tests. So, you should submit that to NetBeans bugzilla [1] if you want to avoid that.
[1] https://netbeans.org/community/issues.html (PHP/PHPUnit)
2019 and the problem still exists. Downgrading PHPUnit is no option anymore today. So I created, as a workaround, a wrapper script for phpunit which takes care of removing the bad parameters.
It also removes the NetBeansSuite.php file completely because it does the same a call directly to the phpunit script would.
My setup is Windows Netbeans 11, Windows PHP 7.3, and a cygwin wrapper (because it's bash code). If somebody feels like porting this to native windows, please leave a comment here.
#!/usr/bin/env sh
# modified phpunit script originally from vendors dir
# 1. cygwin default search path is bad in my installation...
PATH="$PATH:/bin:/usr/bin"
# original code, only adjust the ../../ to match your installation.
# the directory this is run in is where the wrapper script resides
dir=$(cd "${0%[/\\]*}" > /dev/null; cd "../../vendor/phpunit/phpunit" && pwd)
if [ -d /proc/cygdrive ] && [[ $(which php) == $(readlink -n /proc/cygdrive)/* ]]; then
# We are in Cgywin using Windows php, so the path must be translated
dir=$(cygpath -m "$dir");
fi
# fix netbeans parameters
while [ $# -gt 0 ]; do
case "$1" in
*NetBeansSuite.php) shift;;
--run=*) PARAMS="$PARAMS ${1#--run=}"; shift;;
--) shift ;;
*) PARAMS="$PARAMS $1"; shift;;
esac
done
"${dir}/phpunit" $PARAMS
You have to adjust both occurrences of "../"s to your folder structure.
This solution works with ALT-F6 (run all tests), right clicking a test folder "Run Tests" and also single test files right click "Run".
The setting for "Netbeans: Tools/Options/Frameworks & Tools/PHPUnit" is then:
C:\cygwin\bin\bash.exe /cygdrive/c/workspace/Project/src/cli/phpunit
Adjust these path names to your needs, again.
HTH,
M.

phpStorm : wrong path returned by realpath function

I am using symfony and phpunit for my tests.
I had a project in foodmeup.dev directory which I copied into foodmeupnew.dev
Now, when making a call to phpunit from phpStorm command line, I've got something strange with the autoload file being looked for in the previous folder.
See the following, at line 8 it's changing dir from foodmeupnew.dev to foodmeup.dev, how can I correct this ?
> D:\Divers\Programmation\Web\foodmeupnew.dev\bin\phpunit.bat -c app
Fatal error: Cannot redeclare getLadybug() (previously declared in D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\raulfraile\ladybug-themes\Ladybug\Plugin\Extra\Resources\php\helpers.php:12) in D:\Divers\Programmation\Web\foodmeup.dev\vendor\raulfraile\ladybug-themes\Ladybug\Plugin\Extra\Resources\php\helpers.php on line 21
Call Stack:
0.0002 234304 1. {main}() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\phpunit:0
0.0088 1457264 2. PHPUnit_TextUI_Command::main() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\phpunit:36
0.0088 1463816 3. PHPUnit_TextUI_Command->run() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:99
0.0088 1466536 4. PHPUnit_TextUI_Command->handleArguments() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:109
0.0132 2056112 5. PHPUnit_TextUI_Command->handleBootstrap() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:575
0.0134 2065160 6. PHPUnit_Util_Fileloader::checkAndLoad() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:750
0.0136 2065352 7. PHPUnit_Util_Fileloader::load() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\Util\Fileloader.php:36
0.0183 3407040 8. include_once('D:\Divers\Programmation\Web\foodmeup.dev\app\bootstrap.php.cache') D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\Util\Fileloader.php:52
0.0185 3409304 9. require_once('D:\Divers\Programmation\Web\foodmeup.dev\app\autoload.php') D:\Divers\Programmation\Web\foodmeup.dev\app\bootstrap.php.cache:5
0.0187 3411320 10. require('D:\Divers\Programmation\Web\foodmeup.dev\vendor\autoload.php') D:\Divers\Programmation\Web\foodmeup.dev\app\autoload.php:9
0.0191 3424808 11. ComposerAutoloaderInit751ca4f7e73ded3907cdc8698c543ac6::getLoader() D:\Divers\Programmation\Web\foodmeup.dev\vendor\autoload.php:7
0.0214 3630760 12. composerRequire751ca4f7e73ded3907cdc8698c543ac6() D:\Divers\Programmation\Web\foodmeup.dev\vendor\composer\autoload_real.php:45
EDIT:
I've taken the following measure to make sure foodmeup.dev was not hardcoded :
- find and replace in content of every file including hidden and system files the string 'foodmeup.dev' by 'foodmeupnew.dev'
- delete entire directory, create new one, pull git branch, run composer install, reset bootstrap.cache
same error happens again. I'm sure there might be some misconfiguration but I can find out where it's coming from. any clue ?
EDIT2
OK, I figured out that line 5 is loading phpunit configuration that contains the hardcoded 'foodmeup.dev' value.
Where does this configuration come from... that is to find out
> $phpunit - D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:566
============================================================================================================
array(
[colors] => string(4) "auto"
[backupGlobals] => bool(false)
[bootstrap] => string(64) "D:\Divers\Programmation\Web\foodmeup.dev\app\bootstrap.php.cache"
)
EDIT 3
Digging into it, I figured out that in file D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:566 the realpath function applied to a relative path would render a different path.
var_dump($this->arguments['configuration'] . '/phpunit.xml');
string(15) "app/phpunit.xml"
var_dump(realpath($this->arguments['configuration'] . '/phpunit.xml'));
realpath($this->arguments['configuration'] . '/phpunit.xml') - D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:529
================================================================================================================================================================ string(56) "D:\Divers\Programmation\Web\foodmeup.dev\app\phpunit.xml"
How can that be that the realpath function renders a path with foodmeup.dev when the call is made from a file located in the foodmeupnew.dev directory ?
EDIT 4:
I figured out this is a phpStorm issue. there must be a global variable which sets the dir for php function. If I run the command from an external command line, the error does not arise

PHPUnit cannot open file

I've started learning how to use PHPUnit. However, I'm facing a problem which I have no clue how to solve.
I have a folder called lessons and inside there is a composer.json which I installed PHPUnit with.
The output resulted in a folder called vendor and a sub-folder called bin which has the phpunit file in it.
In the cmd I typed: cd c:/xampp/htdocs/lessons/vendor/bin. Now the cmd folder sets to the same folder as phpunit. I've created a directory in lessons which I called tests (lessons/tests) which I store all of my tests in. I've created a file called PracticeTest.php with a very simple test script in it.
When I go back to the cmd and type phpunit tests I get cannot open file tests.php When I try to type phpunit PracticeTest I get cannot open file PracticeTest.php. When I try phpunit tests/PracticeTest (with .php or without) I get the same error that the file could not be opened.
My suspicious that it has something to do with that face that the cmd is pointing at the lessons/vendor/bin directory, but I'm not sure if it is really the problem or how to fix it.
just to arrange the paths:
lessons\vendor\bin\
lessons\tests\
lessons\tests\PracticeTest.php
Thanks in advance!
Go to path project:
D:\>cd www
D:\wwww>cd lessons
And execute:
D:\www\lessons>vendor\bin\phpunit tests
PHPUnit 4.8.27 by Sebastian Bergmann and contributors.
.....E.
Time: 1.34 seconds, Memory: 5.50MB
There was 1 error:
1) UserTest::it_should_be_able_to_construct
PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert
:assertInstanceOf() must be a class or interface name
D:\www\lessons\tests\UserTest.php:10
FAILURES!
Tests: 7, Assertions: 4, Errors: 1.
It Works!!!!
This is what worked for me.
vendor/bin/phpunit ./tests/PracticeTest
I was getting the same, but if you type phpunit then tab you will see what directory you are in and it should autosuggest. Then navigate to your test file.
I had include_path sections separated by comma. It has to be semicolon on Windows.
I have created a phpunit.bat in the root folder of my project containing
#ECHO OFF SET BIN_TARGET=%~dp0/vendor/phpunit/phpunit/phpunit php "%BIN_TARGET%" %*
Now it works.

PHPUNIT error when running tests with PHPSTORM

Running phpunit tests using PhpStorm.
Receiving this error:
/usr/local/php5/bin/php
/private/var/folders/m8/k61mmmmj7g732j3pd0_91s0c0000gn/T/ide-phpunit.php
--configuration /Users/psteinheuser/unity/test/phpunit.xml DatabaseDumperTest
/Users/psteinheuser/unity/test/DatabaseDumperTest.inc Testing started
at 11:36 AM ...
Fatal error: Class IDE_PHPUnit_Framework_TestListener contains 1
abstract method and must therefore be declared abstract or implement
the remaining methods (PHPUnit_Framework_TestListener::addRiskyTest)
in
/private/var/folders/m8/k61mmmmj7g732j3pd0_91s0c0000gn/T/ide-phpunit.php
on line 504
Call Stack:
0.0013 340096 1. {main}() /private/var/folders/m8/k61mmmmj7g732j3pd0_91s0c0000gn/T/ide-phpunit.php:0
PHP Fatal error: Class IDE_PHPUnit_Framework_TestListener contains 1
abstract method and must therefore be declared abstract or implement
the remaining methods (PHPUnit_Framework_TestListener::addRiskyTest)
in
/private/var/folders/m8/k61mmmmj7g732j3pd0_91s0c0000gn/T/ide-phpunit.php
on line 504
Have found previous posts which have said this is fixed, though I seem to have the correct versions where it should work.
PHPStorm 7.1.3
phpunit 3.7.28
php 5.4.24
The OS is Mac 10.9.2
Have removed and re-installed PHPStorm, upgraded php, re-installed phpunit, restarted apache, rebooted, scratched my head, etc.
Running the phpunit tests manually from the terminal, works fine.
Looking at Preferences within PHpStorm, it seems to be pointing to phpunit correctly.
I'm thinking it's a permission or path issue, but I don't know where to look next.
Appreciate any input or direction.
In PHPStorm, the only way I was able to solve this was by manually updating the contents of the jar file where this error exists.
We're both on Mac OSX 10.9, so these instructions should work for you.
Begin by making a copy of the existing jar file:
cp /Applications/PhpStorm.app/plugins/php/lib/php.jar /Applications/PhpStorm.app/plugins/php/lib/php.jar.orig
Now, create a temporary directory to hold your jar file's contents and extract
mkdir ~/jarfiles
cd ~/jarfiles
jar xf /Applications/PhpStorm.app/plugins/php/lib/php.jar
Once extracted, you'll need to edit the phpunit.php file to add the missing method stub
vi scripts/phpunit.php
Around line 310 in my file is a class 'IDE_PHPUnit_Framework_TestListener'. Insert the following method stub:
public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time){}
Now just re-pack your jar and replace it into your PhpStorm:
jar cf0 php.jar ./*
mv php.jar /Applications/PhpStorm.app/plugins/php/lib/php.jar
That fixed it for me. Hope it fixes it for you too. Good luck!

Categories