function reflectMethodModifiers($class) { $classInfo = new reflectionClass($class); $methodArray = $classInfo->getMethods(); foreach ($methodArray as $method) { echo "Modifiers for method {$method->class}::{$method->name}():\n"; printf("0x%08x\n", $method->getModifiers()); echo "\n\n"; } }
/** *Levert een array met alle methoden van deze class op * * @param boolean If the method should also return protected functions * @param boolean If the method should also return private functions * @return IPReflectionMethod[] */ public function getMethods($alsoProtected = true, $alsoPrivate = true) { $ar = parent::getMethods(); foreach ($ar as $method) { $m = new IPReflectionMethod($this->classname, $method->name); if ((!$m->isPrivate() || $alsoPrivate) && (!$m->isProtected() || $alsoProtected) && $m->getDeclaringClass()->name == $this->classname) { $this->methods[$method->name] = $m; } } ksort($this->methods); return $this->methods; }
/** *Levert een array met alle methoden van deze class op. * * @param bool If the method should also return protected functions * @param bool If the method should also return private functions * * @return IPReflectionMethod[] */ public function getMethods($alsoProtected = true, $alsoPrivate = true, $alsoHerited = false) { $ar = parent::getMethods(); foreach ($ar as $method) { if (substr($method->name, 0, 2) == '__' || $method->isAbstract() || $method->isConstructor() || $method->isDestructor()) { continue; } $m = new IPReflectionMethod($this->classname, $method->name); if ((!$m->isPrivate() || $alsoPrivate) && (!$m->isProtected() || $alsoProtected) && ($m->getDeclaringClass()->name == $this->classname || $alsoHerited)) { $this->methods[$method->name] = $m; } } ksort($this->methods); return $this->methods; }
protected function generateInterfaceMethodCode(\reflectionClass $class, $addIteratorAggregate) { $mockedMethods = ''; $mockedMethodNames = array(); $hasConstructor = false; $methods = $class->getMethods(\reflectionMethod::IS_PUBLIC); if ($addIteratorAggregate === true) { $iteratorInterface = call_user_func($this->reflectionClassFactory, 'iteratorAggregate'); $methods = array_merge($methods, $iteratorInterface->getMethods(\reflectionMethod::IS_PUBLIC)); } foreach ($methods as $method) { $methodName = $method->getName(); $mockedMethodNames[] = strtolower($methodName); $parameters = $this->getParameters($method); switch (true) { case $method->isFinal() === false && $method->isStatic() === false: $isConstructor = $methodName === '__construct'; if ($isConstructor === true) { $hasConstructor = true; } $methodCode = "\t" . 'public function' . ($method->returnsReference() === false ? '' : ' &') . ' ' . $methodName . '(' . $this->getParametersSignature($method) . ')' . PHP_EOL; $methodCode .= "\t" . '{' . PHP_EOL; $methodCode .= "\t\t" . '$arguments = array_merge(array(' . join(', ', $parameters) . '), array_slice(func_get_args(), ' . sizeof($parameters) . ($isConstructor === false ? '' : ', -1') . '));' . PHP_EOL; if ($isConstructor === true) { $methodCode .= "\t\t" . 'if ($mockController === null)' . PHP_EOL; $methodCode .= "\t\t" . '{' . PHP_EOL; $methodCode .= "\t\t\t" . '$mockController = \\mageekguy\\atoum\\mock\\controller::get();' . PHP_EOL; $methodCode .= "\t\t" . '}' . PHP_EOL; $methodCode .= "\t\t" . 'if ($mockController !== null)' . PHP_EOL; $methodCode .= "\t\t" . '{' . PHP_EOL; $methodCode .= "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL; $methodCode .= "\t\t" . '}' . PHP_EOL; } $methodCode .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === false)' . PHP_EOL; $methodCode .= "\t\t" . '{' . PHP_EOL; $methodCode .= "\t\t\t" . '$this->getMockController()->' . $methodName . ' = function() {};' . PHP_EOL; $methodCode .= "\t\t" . '}' . PHP_EOL; $methodCode .= "\t\t" . ($isConstructor === true ? '' : 'return ') . '$this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL; $methodCode .= "\t" . '}' . PHP_EOL; break; case $method->isStatic() === true: $methodCode = "\t" . 'public static function' . ($method->returnsReference() === false ? '' : ' &') . ' ' . $methodName . '(' . $this->getParametersSignature($method) . ')' . PHP_EOL; $methodCode .= "\t" . '{' . PHP_EOL; $methodCode .= "\t\t" . '$arguments = array_merge(array(' . join(', ', $parameters) . '), array_slice(func_get_args(), ' . sizeof($parameters) . ', -1));' . PHP_EOL; $methodCode .= "\t\t" . 'return call_user_func_array(array(\'parent\', \'' . $methodName . '\'), $arguments);' . PHP_EOL; $methodCode .= "\t" . '}' . PHP_EOL; break; default: $methodCode = ''; } $mockedMethods .= $methodCode; } if ($hasConstructor === false) { $mockedMethods .= self::generateDefaultConstructor(); $mockedMethodNames[] = '__construct'; } $mockedMethods .= self::generateGetMockedMethod($mockedMethodNames); return $mockedMethods; }
protected static function generateInterfaceMethodCode(\reflectionClass $class) { $mockedMethods = ''; $hasConstructor = false; foreach ($class->getMethods(\reflectionMethod::IS_PUBLIC) as $method) { if ($method->isFinal() === false && $method->isStatic() === false) { $methodName = $method->getName(); $isConstructor = $methodName === '__construct'; if ($isConstructor === true) { $hasConstructor = true; } $parameters = array(); foreach ($method->getParameters() as $parameter) { $parameters[] = '$' . $parameter->getName(); } $parameters = join(', ', $parameters); $mockControllerParameters = $parameters == '' ? 'func_get_args()' : 'array(' . $parameters . ')'; $methodCode = "\t" . 'public function' . ($method->returnsReference() === false ? '' : ' &') . ' ' . $methodName . '(' . self::getParameters($method, $isConstructor) . ')' . PHP_EOL; $methodCode .= "\t" . '{' . PHP_EOL; if ($isConstructor === true) { $methodCode .= "\t\t" . 'if ($mockController === null)' . PHP_EOL; $methodCode .= "\t\t" . '{' . PHP_EOL; $methodCode .= "\t\t\t" . '$mockController = \\mageekguy\\atoum\\mock\\controller::get();' . PHP_EOL; $methodCode .= "\t\t" . '}' . PHP_EOL; $methodCode .= "\t\t" . 'if ($mockController !== null)' . PHP_EOL; $methodCode .= "\t\t" . '{' . PHP_EOL; $methodCode .= "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL; $methodCode .= "\t\t" . '}' . PHP_EOL; } $methodCode .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === false)' . PHP_EOL; $methodCode .= "\t\t" . '{' . PHP_EOL; $methodCode .= "\t\t\t" . '$this->mockController->' . $methodName . ' = function() {};' . PHP_EOL; $methodCode .= "\t\t" . '}' . PHP_EOL; $methodCode .= "\t\t" . ($isConstructor === true ? '' : 'return ') . '$this->mockController->invoke(\'' . $methodName . '\', ' . $mockControllerParameters . ');' . PHP_EOL; $methodCode .= "\t" . '}' . PHP_EOL; $mockedMethods .= $methodCode; } } if ($hasConstructor === false) { $mockedMethods .= self::generateDefaultConstructor(); } return $mockedMethods; }
public function generate($testClassPath) { if ($this->testedClassesDirectory === null) { throw new generator\exception('Tested classes directory is undefined'); } if ($this->testClassesDirectory === null) { throw new generator\exception('Tests directory is undefined'); } if ($this->testedClassNamespace === null) { throw new generator\exception('Tested class namespace is undefined'); } if ($this->testClassNamespace === null) { throw new generator\exception('Test class namespace is undefined'); } $testClassesDirectory = $this->pathFactory->build($this->testClassesDirectory); if ($testClassesDirectory->exists() === false) { throw new generator\exception('Test classes directory \'' . $testClassesDirectory . '\' does not exist'); } $realTestClassesDirectory = $testClassesDirectory->getRealPath(); $realTestClassPath = $this->pathFactory->build($testClassPath)->getRealPath(); $realTestClassBaseDirectory = $realTestClassPath->getRealParentDirectoryPath(); if ((string) $realTestClassesDirectory !== (string) $realTestClassBaseDirectory && $realTestClassBaseDirectory->isSubPathOf($realTestClassesDirectory) === false) { throw new generator\exception('Path \'' . $testClassPath . '\' is not in directory \'' . $this->testClassesDirectory . '\''); } $realTestClassRelativePath = substr($realTestClassPath->getRelativePathFrom($realTestClassesDirectory), 2); $fullyQualifiedTestClassName = call_user_func_array($this->fullyQualifiedTestClassNameExtractor, array($this, $realTestClassRelativePath)); $testClassTemplate = $this->templateParser->parseFile($this->templatesDirectory . DIRECTORY_SEPARATOR . 'testClass.php'); $testClassTemplate->fullyQualifiedTestClassName = $fullyQualifiedTestClassName; $testClassTemplate->testClassName = self::getShortClassName($fullyQualifiedTestClassName); $testClassTemplate->testClassNamespace = self::getClassNamespace($fullyQualifiedTestClassName); if ($this->runnerPath !== null) { $runnerPath = $this->pathFactory->build($this->runnerPath); $relativeRunnerPath = $runnerPath->relativizeFrom($realTestClassBaseDirectory); $testClassTemplate->requireRunner->relativeRunnerPath = $relativeRunnerPath; $testClassTemplate->requireRunner->build(); } $fullyQualifiedTestedClassName = call_user_func_array($this->fullyQualifiedTestedClassNameExtractor, array($this, $fullyQualifiedTestClassName)); if ($this->adapter->class_exists($fullyQualifiedTestedClassName) === false) { $testClassTemplate->testMethods->testMethod->methodName = '__construct'; $testClassTemplate->testMethods->testMethod->methodName->build(); $testClassTemplate->testMethods->testMethod->build(); $testedClassPath = $this->pathFactory->build(call_user_func_array($this->testedClassPathExtractor, array($this, $fullyQualifiedTestedClassName))); $testedClassTemplate = $this->templateParser->parseFile($this->templatesDirectory . DIRECTORY_SEPARATOR . 'testedClass.php'); $testedClassTemplate->testedClassName = self::getShortClassName($fullyQualifiedTestedClassName); $testedClassTemplate->testedClassNamespace = self::getClassNamespace($fullyQualifiedTestedClassName); $testedClassPath->putContents($testedClassTemplate->build()); } else { $testedClass = new \reflectionClass($fullyQualifiedTestedClassName); foreach ($testedClass->getMethods(\reflectionMethod::IS_PUBLIC) as $publicMethod) { $testClassTemplate->testMethods->testMethod->methodName = $publicMethod->getName(); $testClassTemplate->testMethods->testMethod->methodName->build(); $testClassTemplate->testMethods->testMethod->build(); } } $testClassTemplate->testMethods->build(); $realTestClassPath->putContents($testClassTemplate->build()); return $this; }