Created
February 20, 2014 15:52
-
-
Save justinrainbow/9116789 to your computer and use it in GitHub Desktop.
Create PhpSpec base specs from existing code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
$class = $argv[1]; | |
$class = str_replace('/', '\\', $class); | |
$refl = new ReflectionClass($class); | |
$constructor = $refl->getConstructor(); | |
$mocks = []; | |
$uses = [ | |
'PhpSpec\ObjectBehavior', | |
'Prophecy\Argument' | |
]; | |
$vars = []; | |
$args = []; | |
if ($constructor instanceof ReflectionMethod) { | |
$parameters = $constructor->getParameters(); | |
foreach ($parameters as $param) { | |
$paramClass = $param->getClass(); | |
if ($paramClass instanceof ReflectionClass) { | |
$uses[] = $paramClass->getName(); | |
$mocks[] = sprintf('%s $%s', $paramClass->getShortName(), $param->getName()); | |
} else { | |
$vars[] = sprintf( | |
'$%s = %s', | |
$param->getName(), | |
$param->isArray() ? 'array()' : 'null' | |
); | |
} | |
$args[] = sprintf('$%s', $param->getName()); | |
} | |
} | |
$tmpl = ltrim(substr(file_get_contents(__FILE__), __COMPILER_HALT_OFFSET__)); | |
$tokens = [ | |
'{uses}' => 'use ' . join(";\nuse ", array_unique($uses)) . ';', | |
'{namespace}' => $refl->getNamespaceName(), | |
'{class}' => $refl->getShortName(), | |
'{vars}' => '', | |
'{mocks}' => join(', ', $mocks), | |
'{args}' => join(', ', $args), | |
]; | |
if (count($vars)) { | |
$tokens['{vars}'] = join("\n; ", $vars) . ";\n\n "; | |
} | |
$file = 'spec/' . str_replace('\\', '/', $refl->getName()) . 'Spec.php'; | |
$dir = dirname($file); | |
if (! is_dir($dir)) { | |
mkdir($dir, 0755, true); | |
} | |
file_put_contents($file, strtr($tmpl, $tokens)); | |
__halt_compiler(); | |
<?php | |
namespace {namespace}; | |
{uses} | |
class {class}Spec extends ObjectBehavior | |
{ | |
function let({mocks}) | |
{ | |
{vars}$this->beConstructedWith({args}); | |
} | |
function it_is_initializable() | |
{ | |
$this->shouldHaveType('{namespace}\{class}'); | |
} | |
} |
You stupid fuck
Gonna get u. And ur lil circle jerkin group
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey what's up. thieving piece of shit bitch mother fucker
I should come to Arizona and beat ur fucking face in. And curb stomp on your head