Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save ngyuki/3ef425d42a6743c9a67e to your computer and use it in GitHub Desktop.

Select an option

Save ngyuki/3ef425d42a6743c9a67e to your computer and use it in GitHub Desktop.
Run phpunit via ssh in phpstorm7 on windows
/vendor
/composer.lock
/*.local.php
/tests
<?php
require_once 'PHPUnit/Framework/Assert/Functions.php';
{
"require-dev": {
"phpunit/phpunit": "~3.7",
"piece/stagehand-testrunner": "*"
}
}
<?xml version="1.0" encoding="utf-8" ?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/sebastianbergmann/phpunit/master/phpunit.xsd"
bootstrap="bootstrap.php"
>
<testsuites>
<testsuite name="tests">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
<?php
$remote_host = '192.0.2.123';
$remote_dir = '/home/your/work/project';
$local_dir = __DIR__;
$preload_script = '';
$phpunit_config = '';
///
$fn = __DIR__ . DIRECTORY_SEPARATOR . basename(__FILE__, '.php') . '.local.php';
if (file_exists($fn)) {
/** @noinspection PhpIncludeInspection */
require $fn;
}
$args = array(
'--no-ansi',
'phpunit',
);
if (strlen($preload_script)) {
$args[] = "--preload-script=$preload_script";
}
if (strlen($phpunit_config)) {
$args[] = "--phpunit-config=$phpunit_config";
}
$win = DIRECTORY_SEPARATOR !== '/';
$prefix = $local_dir . DIRECTORY_SEPARATOR;
if ($win) {
$prefix = str_replace(DIRECTORY_SEPARATOR, '/', strtolower($prefix));
}
$junit = null;
$argv = $_SERVER['argv'];
$args = array_reduce($argv, function ($r, $arg) use ($prefix, $win, &$junit) {
if ($junit === null) {
if (strncmp('--log-junit=', $arg, strlen('--log-junit=')) === 0) {
$junit = substr($arg, strlen('--log-junit='));
$arg = '--log-junit=makegood://';
}
}
if ($junit !== null) {
if (file_exists($arg)) {
$mat = $arg;
if ($win) {
$mat = str_replace(DIRECTORY_SEPARATOR, '/', strtolower($mat));
}
if (strncmp($mat, $prefix, strlen($prefix)) === 0) {
$arg = './' . substr($arg, strlen($prefix));
if ($win) {
$arg = str_replace(DIRECTORY_SEPARATOR, '/', $arg);
}
}
}
$r[] = $arg;
}
return $r;
}, $args);
$params = array(
'args' => $args,
'remote_dir' => $remote_dir,
'local_dir' => $local_dir,
);
$file = ltrim(substr(file_get_contents(__FILE__), __COMPILER_HALT_OFFSET__));
$file = str_replace('__IDE_PHPUNIT_PARAMS__', var_export($params, true), $file);
$cmd = sprintf('plink %s -batch php 2> %s',
escapeshellarg($remote_host),
escapeshellarg($junit)
);
$cmd = sprintf('plink %s -batch php', escapeshellarg($remote_host));
$stdin = fopen('php://temp', 'r+');
fwrite($stdin, $file);
rewind($stdin);
$desc = array(
0 => $stdin,
1 => STDOUT,
2 => array('file', $junit, 'w'),
);
exit(proc_close(proc_open($cmd, $desc, $pipes)));
__halt_compiler();
<?php
/** @noinspection PhpUnreachableStatementInspection */
/** @noinspection PhpUndefinedConstantInspection */
$params = __IDE_PHPUNIT_PARAMS__;
$args = $params['args'];
$remote_dir = $params['remote_dir'];
$local_dir = $params['local_dir'];
MakeGood_StreamWrapper::register();
class MakeGood_StreamWrapper
{
private $stream;
public static function register()
{
stream_wrapper_register('makegood', __CLASS__);
}
public function stream_open(/* $path */)
{
$this->stream = fopen('php://stderr', 'wb');
return true;
}
public function stream_write($data)
{
global $remote_dir, $local_dir;
$data = str_replace($remote_dir, $local_dir, $data);
return fwrite($this->stream, $data);
}
}
chdir($remote_dir);
$testrunner = 'vendor/piece/stagehand-testrunner/bin/testrunner';
if (file_exists("$testrunner.php") == false) {
exec("tail -n +2 $testrunner > $testrunner.php");
}
array_unshift($args, "$testrunner.php");
$argv = $args;
$argc = count($argv);
$_SERVER['argv'] = $argv;
$_SERVER['argc'] = $argc;
/** @noinspection PhpIncludeInspection */
require "$testrunner.php";
<?php
$remote_host = '192.0.2.123';
$remote_dir = '/home/your/work/project';
$local_dir = __DIR__;
/*
* IDE [Run -> Edit Configurations]
* - Interpreter options: -d include_path=. -d auto_prepend_file=remote.php
* - Custom working directory: {project_root}
*/
///
$fn = __DIR__ . DIRECTORY_SEPARATOR . basename(__FILE__, '.php') . '.local.php';
if (file_exists($fn)) {
/** @noinspection PhpIncludeInspection */
require $fn;
}
$win = DIRECTORY_SEPARATOR !== '/';
$prefix = $local_dir . DIRECTORY_SEPARATOR;
if ($win) {
$prefix = str_replace(DIRECTORY_SEPARATOR, '/', strtolower($prefix));
}
$argv = $_SERVER['argv'];
$argv = array_map(function ($arg) use ($prefix, $win) {
if (file_exists($arg)) {
$mat = $arg;
if ($win) {
$mat = str_replace(DIRECTORY_SEPARATOR, '/', strtolower($mat));
}
if (strncmp($mat, $prefix, strlen($prefix)) === 0) {
$arg = './' . substr($arg, strlen($prefix));
if ($win) {
$arg = str_replace(DIRECTORY_SEPARATOR, '/', $arg);
}
}
}
return $arg;
}, $argv);
$params = array(
'remote_dir' => $remote_dir,
'local_dir' => $local_dir,
'argv' => $argv,
);
$file = ltrim(substr(file_get_contents(__FILE__), __COMPILER_HALT_OFFSET__));
$file = str_replace('__IDE_PHPUNIT_PARAMS__', var_export($params, true), $file);
$file .= '?' . '>';
$file .= file_get_contents($_SERVER['SCRIPT_FILENAME']);
$cmd = sprintf('plink %s -batch env IDE_DEBUG=%d bash',
escapeshellarg($remote_host),
isset($_SERVER['XDEBUG_CONFIG'])
);
$pp = popen($cmd, "wb");
fwrite($pp, $file);
stream_copy_to_stream($pp, STDOUT);
pclose($pp);
exit;
__halt_compiler();
#!/bin/bash
if [ "$IDE_DEBUG" -ne 0 ]; then
export XDEBUG_CONFIG="remote_host=$(echo $SSH_CLIENT | awk '{print $1}')"
export PHP_IDE_CONFIG="serverName=$(hostname -s)"
fi
exec php
<?php
$params = __IDE_PHPUNIT_PARAMS__;
$remote_dir = $params['remote_dir'];
$local_dir = $params['local_dir'];
$argv = $params['argv'];
$argc = count($argv);
$_SERVER['argv'] = $argv;
$_SERVER['argc'] = $argc;
chdir($remote_dir);
if (file_exists("$remote_dir/vendor/autoload.php")) {
/** @noinspection PhpIncludeInspection */
require "$remote_dir/vendor/autoload.php";
}
ob_implicit_flush(true);
ob_start(function ($out) use ($remote_dir, $local_dir) {
return str_replace($remote_dir, $local_dir, $out);
}, 2);
<?php
namespace Ore;
class SampleTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
$this->assertEquals('Linux', php_uname('s'));
$this->assertEquals('cli', php_sapi_name());
$this->assertEquals(8, PHP_INT_SIZE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment