Skip to content

Instantly share code, notes, and snippets.

@brtriver
Last active October 16, 2018 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brtriver/5179397 to your computer and use it in GitHub Desktop.
Save brtriver/5179397 to your computer and use it in GitHub Desktop.
testrunner with humcrest and doccomments

testrunner with humcrest and doccomments

  1. get and install hamcrest https://code.google.com/p/hamcrest/downloads/detail?name=hamcrest-php-1.1.0.zip

  2. install guard $ gem install guard guard-shell

  3. get humcrester get these 2 files in your project

  • hamcrester.php
  • Guardfile
  1. run testrunner $ guird -i

NOTE:

  • if you want to change watched directries, change the setting in Gardfile, the default value is "/src/(.*).php/"
  • if you want to change hamcrest lib path "PATH", change the setting in hamcrester.php. the default value is " DIR . '/vendor/Hamcrest-1.1.0'"
# More info at https://github.com/guard/guard#readme
guard 'shell' do
watch(/src\/(.*).php/) {|m| `php hamcrester.php #{m[0]}` }
end
<?php
// gem install guard guard-shell
// make test
/**
* without composer, directly set the lib path
*/
if (!function_exists('assertThat')) {
define('PATH', __DIR__ . '/vendor/Hamcrest-1.1.0');
set_include_path(get_include_path() . ':' . PATH);
require_once PATH . '/Hamcrest/Hamcrest.php';
}
// parsed
$src = $argv[1];
$tokens = token_get_all(file_get_contents($src));
$parsed = "";
foreach($tokens as $token) {
if(is_array($token)) {
$parsed .= getParsedCommentBlock($token);
} else {
$parsed .= $token;
}
}
// reset color
echo "\033[0m";
// eval src
try {
if (false === eval($parsed)) throw new Exception();
} catch(Exception $e) {
die("\033[41;37m" . $e . "\033[41;37m");
}
// result
$error = error_get_last();
if ($error) {
die("\033[41;37m" . $error . "\033[41;37m");
} else {
echo "\033[42;37m". "OK (All Tests Green)" ."\033[42;37m";
}
exit;
function getParsedCommentBlock($token)
{
$parsed = "";
if($token[0] == T_OPEN_TAG || $token[0] == T_CLOSE_TAG){
return;
}
if($token[0] == T_DOC_COMMENT) {
foreach (explode(PHP_EOL, $token[1]) as $line) {
if (preg_match("/\*\s*?@t\((.*)$/", $line, $matches)) {
$parsed .= "assertThat(" . $matches[1] . PHP_EOL;
}
}
} else {
$parsed .= $token[1];
}
return $parsed;
}
all: test
test:
guard -i
<?php
/**
* "@t" means that this line is assertThat annotation and replace and executed as assertThat method
* "@t(true, is(true));" equals "assertThat(true, is(true));"
* NOTICE: docblock is only chekced if this includes "t" annotation.
*/
$a =[
1 => true,
2 => false,
3 => true,
];
print_r($a);
$b = array_filter($a, function($item){
return ($item === true)?: false;
});
/**
* @t($b, is(arrayValue()));
* @t($b, not(hasValue(false)));
*/
function filter($items)
{
return array_filter($items, function($item){
return ($item === true)?: false;
});
}
/**
* @t(filter($a), not(hasValue(false)));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment