In a GUI-driven operating system like Windows, where you don't hang around the command line that much, it is sometimes useful to run a PHP file from its context menu in Windows Explorer.
That's easy to set up. But a reasonably smart handler should perhaps not hand the file over to PHP blindly. PHPUnit test cases need a different treatment, so the handler could try to detect them and pass them on to PHPUnit instead.
Here's a simple script, php-cli.bat, that does just that. Test files are identified through convention, not introspection: If the filename ends in "...test.php", PHPUnit will get to handle it. All other files are passed on to PHP. Both PHP and PHPUnit are expected to be in your PATH.
This is what the script does:
@REM PHP CLI runner, incorporating a PHPUnit test runner.
@REM o Expects a filepath as first argument.@REM o An ordinary file will be passed on to PHP. If the file is a test, it@REM will be run through PHPUnit.@REM o Test files are identified by their basename ending in 'Test'.@REM o PHP and PHPUnit must be found in the PATH.@echo off
@REM Get the filename as basename without extensionset filename=%~n1set filepath=%~f1set scriptdir=%~dp1cd /d %scriptdir%
@REM check if the filename ends in "Test"set suffix=%filename:~-4,4%if "%suffix%" == "Test" GOTO :IS_TESTFILE
@REM not a testfile, run through PHPecho Running %filepath% ...
@REM keep the window open after PHP has run
@REM this is a testfile, use PHPUnitecho Running tests in%filepath% ...
START /B phpunit %*
There are several ways to add the batch script to the context menu of .php files.
In the "Open with ..." flyout menu (Windows 7)
Just put the batch file wherever you like it to be "installed". Next, right-click on any PHP file, select "Open with ..." > "Choose default program" > "Browse" and pick the script. Before you hit OK, don't forget to uncheck "Always use the selected program to open this kind of file" unless that's really what you want.
As soon as this is done, php-cli.bat will become a permanent option in the "Open with" list for PHP files.
At the top level of the context menu (Windows 7)
You'll have to edit the Registry for that. (Disclaimer: Don't do that unless you know what you are doing, are aware of the risks involved and take adequate precautions.)
Assuming you have registered .php files before as a PHPFile type (or some app has done it for you),
Add a subkey named "Run with PHP".
Set the default value to the text you want to appear in the context menu, e.g. "Run with PHP"
Inside the new key, add a subkey named "Command"
Set the default value to "C:\path\to\php-cli.bat" "%1" %* (including the quotes), e.g. "C:\Program Files (x86)\php-extras\php-cli.bat" "%1" %*
And you are done. If you are interested, a generic description of registering a context menu handler is available at MSDN in a basic and more elaborate version.
Other Windows flavours
In Windows Vista, the procedure should be roughly the same (I have not tried it, though). If you are still using Windows XP, adding an entry to the context menu works a little differently.
"cooool thx for sharing!!!!!", haha. Yeah this is a nice script. I don't have any need for phpunit, but it's nice to use for debugging php cron scripts(especially long ones).
The reason I find this much more useful than the browser, is because in the browser it buffers the stdout until the page is done, then flushes it all to the page. If you run it from the command line you get every echo as it happens.
You can use flush(), but if you plan on adding header data down the road in the script, it'll break that.
Anyways thanks, this helps.
Your comment is most welcome - and it will be even more if it is somehow related to the topic ;-) However, we probably all agree that spam and similar nonsense ("cooool thx for sharing!!!!!") shouldn't make it online.