public function detectPropertyMocks($this_i = 0) { $tok = $this->getTokenize(); //echo $tok->dumpAllTokens(); $propertyMap = ReflectionClass::getForClass($this->class)->getPropertyTypeMap(); $mocks = array(); while ($this_i = $tok->findAfter($this_i, T_VARIABLE)) { if (!$this_i) { break; } $objVarName = $tok->getStringAt($this_i); if ('$this' !== $objVarName) { continue; } if (T_OBJECT_OPERATOR !== $tok->getTokenTypeAt(++$this_i)) { continue; } $possiblePropertyName = $tok->getStringAt(++$this_i); if (empty($propertyMap[$possiblePropertyName])) { continue; } // and now actual method proxy detection if (T_OBJECT_OPERATOR !== $tok->getTokenTypeAt(++$this_i)) { continue; } $possibleMethodName = $tok->getStringAt(++$this_i); if ('(' !== $tok->getCharAt(++$this_i)) { continue; } $mocks[$propertyMap[$possiblePropertyName]][] = $possibleMethodName; } return $mocks; }
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});"; }
/** * Process getter methods * @param string $instanceVariable * @param Cogen\Reflection\ReflectionClass $class * @param Cogen\Code $code */ public function generateGetters($instanceVariable, ReflectionClass $class, Code $code) { foreach ($class->getGetters() as $getter) { /* @var $getter ReflectionMethod */ $invokation = $this->generateTestMethodCall($getter, $instanceVariable, $code); if ($getter->isScalarReturn()) { $actualName = $invokation->getResultVariable(); $expectedName = $getter->getNameWrapped('expected', 'Result', true); $code->expect("{$expectedName} = null;"); $code->assert("\$this->assertEquals({$expectedName}, {$actualName});"); } } }