Skip to content
Create a gist now

Instantly share code, notes, and snippets.

CakePHP2 PHPUnit Runner for PHPStorm
<?php
// Clean argument values
$phpStormRunner = null;
$cleanedArgv = array();
foreach ($_SERVER['argv'] as $key => $value) {
if (strpos($value, 'ide-phpunit.php') === false) {
$cleanedArgv[] = $value;
} else {
$phpStormRunner = $value;
}
}
$_SERVER['argv'] = $cleanedArgv;
// Cleanup PhpStorm runner
if (is_null($phpStormRunner)) {
die('Not running in PhpStorm');
}
$phpStormRunnerContents = file_get_contents($phpStormRunner);
$phpStormRunnerContents = str_replace('IDE_PHPUnit_TextUI_Command::main', 'IDE_Cake_PHPUnit_TextUI_Command::main', $phpStormRunnerContents);
$phpStormRunnerContents = str_replace('IDE_Cake_PHPUnit_TextUI_Command::main()', '//IDE_Cake_PHPUnit_TextUI_Command::main()', $phpStormRunnerContents);
file_put_contents($phpStormRunner, $phpStormRunnerContents);
// Include PhpStorm runner
include($phpStormRunner);
// Let's bake some CakePHP
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('ROOT')) {
define('ROOT', dirname(__FILE__));
}
if (!defined('APP_DIR')) {
define('APP_DIR', 'app');
}
if (!defined('WEBROOT_DIR')) {
define('WEBROOT_DIR', APP_DIR . DS . 'webroot');
}
if (!defined('WWW_ROOT')) {
define('WWW_ROOT', WEBROOT_DIR . DS);
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
if (function_exists('ini_set')) {
ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
}
if (!include ('Cake' . DS . 'bootstrap.php')) {
$failed = true;
}
} else {
if (!include (CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'bootstrap.php')) {
$failed = true;
}
}
if (!empty($failed)) {
trigger_error("CakePHP core could not be found. Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php. It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
}
if (Configure::read('debug') < 1) {
die(__d('cake_dev', 'Debug setting does not allow access to this url.'));
}
// Do some reconfiguration
Configure::write('Error', array());
Configure::write('Exception', array());
// Bootstrap CakePHP
require_once CAKE . 'TestSuite' . DS . 'CakeTestSuiteDispatcher.php';
require_once CAKE . 'TestSuite' . DS . 'CakeTestSuiteCommand.php';
// Define new runner
class IDE_Cake_PHPUnit_TextUI_Command extends CakeTestSuiteCommand
{
public static function main($exit = TRUE)
{
// Parse CakePHP-specific test runner options
// And remove CakePHP-specific test runner options from ARGV
$cakePhpOptions = array('case' => 'AllTests');
$cleanedArgv = array();
foreach ($_SERVER['argv'] as $key => $value) {
if (strpos($value, '--cake-') !== false) {
$option = explode('=', $value);
$cakePhpOptions[str_replace('--cake-', '', $option[0])] = isset($option[1]) ? $option[1] : 'true';
} else {
$cleanedArgv[] = $value;
}
}
// Run!
$command = new IDE_Cake_PHPUnit_TextUI_Command('CakeTestLoader', $cakePhpOptions);
$command->run($cleanedArgv, $exit);
}
protected function handleArguments(array $argv)
{
parent::handleArguments($argv);
$this->arguments['listeners'][] = new IDE_PHPUnit_Framework_TestListener();
$this->arguments['printer'] = new IDE_PHPUnit_TextUI_ResultPrinter();
}
}
IDE_Cake_PHPUnit_TextUI_Command::main();
@tomaspavlatka

Hi,

if you are using PHPStorm 8.x, you must make some modifications into this file to make it working. You much change handleArguments(array $argv) to following

protected function handleArguments(array $argv)
{
    parent::handleArguments($argv);
    $printer = null;
    if (isset($this->arguments['printer'])) {
        $printer = $this->arguments['printer'];
    }

    $printer = new IDE_PHPUnit_TextUI_ResultPrinter($printer);
    $this->arguments['printer'] = $printer;
    $this->arguments['listeners'][] = new IDE_PHPUnit_Framework_TestListener($printer);
}
@tgaertner

in PhpStorm 8.0.3 it works for me without that file

@savedario

I am running PhpStorm 9.0.2 and the change suggested by tomas is required. Perhaps it has something to do with the version of CakePHP used...

@EOM
EOM commented Nov 10, 2015

Yes Tomas,

Run OK in PHPStrom 8, 9 and 10 + PHPUnit 3.7.38 + CakePHP 2.6.x
;-)

protected function handleArguments(array $argv)
{
    parent::handleArguments($argv);
    $printer = null;
    if (isset($this->arguments['printer'])) {
        $printer = $this->arguments['printer'];
    }

    $printer = new IDE_PHPUnit_TextUI_ResultPrinter($printer);
    $this->arguments['printer'] = $printer;
    $this->arguments['listeners'][] = new IDE_PHPUnit_Framework_TestListener($printer);
}

Link Code Full add change and testing:
https://bitbucket.org/snippets/eom/bRqy6

@kazuhiko-hotta

Hi

There's a code currently I'm using in IntelliJ IDEA 15.0 with CakePHP2.7.
it can be using by TestRunner with Test Scope Class and Method option and Coverage report option.

https://github.com/kazuhiko-hotta/IntelliJCakePHPUnitTestAdaptor/blob/master/intellijadaptor.php

@xTNTx
xTNTx commented Dec 7, 2015

Used this solution to make a plugin https://github.com/xTNTx/CakeJetbrainsTest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.