public function getAllDepencies(Tokenize $tokenize) { $code = array(); foreach ($this->getDependentServices($tokenize) as $id => $methods) { $service = $this->container->get($id); $refl = new ReflectionClass($service); $mockVariable = '$mock' . $refl->getLastName(); foreach ($methods as $methodName) { $code[] = "\${$methodName}Value = '';"; } $code[] = "//{$mockVariable} = new {$refl->getName()}();"; $code[] = "{$mockVariable} = \$this->getMockBuilder('{$refl->getName()}')->disableOriginalConstructor()->setMethods(array('" . join("', '", $methods) . "'))->getMock();"; foreach ($methods as $methodName) { $code[] = "{$mockVariable}->expects(\$this->any())->method('{$methodName}')->will(\$this->returnValue(\${$methodName}Value));"; } $code[] = "\$container->expects(\$this->any())->method('get')->with(\$this->equalTo('{$id}'))->will(\$this->returnValue({$mockVariable}));"; //$code[] = "\$container->register('$id', $mockVariable);"; $code[] = ''; } echo join("\n", $code); }
public function getCommands() { $commands = array(); foreach (new \DirectoryIterator(__DIR__ . '/Command') as $file) { /* @var $file \SplFileObject */ list($name, $ext) = explode('.', $file->getBasename()); if ('php' !== $ext) { continue; } $refl = new ReflectionClass("Pugooroo\\Command\\{$name}"); if (!$refl->getDocComment()) { continue; } $docblock = $refl->getDocblock(); if (!$docblock->hasTag('command')) { continue; } $commands[$docblock->getTag('command')->getDescription()] = array('shortDescription' => $docblock->getShortDescription(), 'className' => $refl->getName(), 'reflection' => $refl); } return $commands; }
/** * Generate class initialization code * @param Cogen\Code $code * @param string $instanceVariable * @return string */ public function getClassInitialization(Code $code, $instanceVariable, $isMock = false) { if ($isMock) { $lines = array(); foreach ($this->class->getGetters() as $method) { /* @var $method ReflectionMethod */ $lines[] = "{$instanceVariable}->expects(\$this->once())->method('{$method->getName()}')"; $lines[] = "\t->will(\$this->returnCallback(function() {"; $lines[] = "\t\treturn null;"; $lines[] = "\t}));"; } $code->after($lines); return "{$instanceVariable} = \$this->getMock('{$this->class->getName()}');"; } $lines = array(); $arguments = ''; $lines = array(); foreach ($this->class->getSetters() as $method) { /* @var $method ReflectionMethod */ $lines[] = $this->generateTestMethodCall($method, $instanceVariable, $code, true); } $code->after($lines); return "{$instanceVariable} = new {$this->class->getName()}({$arguments});"; }