public function testValidManagerWhenInvalidTestClassMethod() { $testClass = $this->classManager->getTestClass(); $testClassMethods = $testClass->getMethods(); $testClassMethods->add(new TestMethodManager($this->classManager)); // broken test class method $testClass->setMethods($testClassMethods); $errors = $this->getValidator()->validate($this->classManager); $this->assertEquals(1, $errors->count()); }
/** * @param string $namespace * @return ClassManager */ public static function prepareBasicClassManager($namespace = "\\AppBundle\\Entity\\User") { $classManager = new ClassManager(); $classManager->setNamespace($namespace); $classManager->setComment("User entity for tests"); $propertiesCollection = new ArrayCollection(); $propertiesCollection->add(self::prepareProperty("full_name", "string", "", ["NotBlank()"])); $propertiesCollection->add(self::prepareProperty("email", "string", "", ["Email(message = \"Invalid email!\")"])); $propertiesCollection->add(self::prepareProperty("active", "boolean", "Wether user active", ["Type(type='boolean')", "IsTrue()"])); $propertiesCollection->add(self::prepareProperty("new_posts", "Doctrine\\Common\\Collections\\ArrayCollection<AppBundle\\Entity\\Post>", "User new posts", ["NotNull()", "Valid()"])); $classManager->setProperties($propertiesCollection); return $classManager; }
/** * @param ClassManager $class * @return string * @throws RendererException */ protected function renderClass(ClassManager $class) { $template = $class->getTemplate(); $tags = $class->getTemplateTags(); $properties = []; $methods = []; $interfacePart = ''; $extendsPart = ''; foreach ($class->getProperties() as $property) { $properties[] = $this->render($property); } foreach ($class->getMethods() as $method) { $methods[] = $this->render($method); } if ($class->hasExtends()) { $extendsPart = sprintf(" extends %s", $class->getExtends()); } if ($class->hasInterface()) { $interfacePart = sprintf(" implements %s", $class->getInterface()->getNamespace()); } $args[RenderableInterface::TAG_NAMESPACE] = $class->getNamespaceWithoutNameAndBackslashPrefix(); $args[RenderableInterface::TAG_COMMENT] = empty($class->getComment()) ? "" : sprintf(" %s", $class->getComment()); $args[RenderableInterface::TAG_NAME] = $class->getName(); $args[RenderableInterface::TAG_EXTENDS] = $extendsPart; $args[RenderableInterface::TAG_INTERFACE] = $interfacePart; $args[RenderableInterface::TAG_CONSTRUCTOR] = $this->render($class->getConstructor()); $args[RenderableInterface::TAG_PROPERTIES] = Tools::implodeArrayToTemplate($properties); $args[RenderableInterface::TAG_METHODS] = Tools::implodeArrayToTemplate($methods); $args[RenderableInterface::TAG_MULTILINE_COMMENT] = $this->prepareMultilineCommentForElement($class); return $this->addNewLineAfter($this->replace($tags, $args, $template)); }
/** * @param string $content * @param ClassManager $item * @param ReflectionClass $reflectionClass * @return ClassConstructorManager */ protected function resolveConstructorToRender($content, ClassManager $item, ReflectionClass $reflectionClass) { $constructor = $item->getConstructor(); $constructorFromFile = $reflectionClass->getConstructor(); $source = Tools::explodeTemplateStringToArray($content); $constructorBody = implode("", array_slice($source, $constructorFromFile->getStartLine(), $constructorFromFile->getEndLine() - $constructorFromFile->getStartLine())); $matches = []; preg_match_all("/this->[\\S]+/", $constructorBody, $matches); $initPropertiesFromConstructor = []; if (count($matches) > 0) { foreach ($matches[0] as $match) { $initPropertiesFromConstructor[] = str_replace("this->", "", trim($match)); } } $newInitProperties = new ArrayCollection(); foreach ($constructor->getInitProperties() as $initProperty) { if (in_array($initProperty->getProperty()->getPreparedName(), $initPropertiesFromConstructor)) { continue; } $newInitProperties->add($initProperty); } $constructor->setInitProperties($newInitProperties); $item->setConstructor($constructor); return $constructor; }
/** * Generate class components * - setters and getters for Class and Interface (optional) * - method with prefix is for boolean properties * * @param \HelloWordPl\SimpleEntityGeneratorBundle\Lib\ClassManager $classManager */ protected function generateAndFillClassMethods(ClassManager $classManager) { $methodsForClass = new ArrayCollection(); // fix - jms serializer does not call ClassManager constructor during deserialization if (false == $classManager->getProperties() instanceof ArrayCollection) { $classManager->setProperties(new ArrayCollection()); } foreach ($classManager->getProperties() as $property) { if ($property->isTypeBoolean()) { $methodsForClass->add((new MethodGetterBooleanManager($classManager))->setProperty($property)); } $methodSetterManager = new MethodSetterManager($classManager); $methodSetterManager->setProperty($property); $methodGetterManager = new MethodGetterManager($classManager); $methodGetterManager->setProperty($property); $methodsForClass->add($methodSetterManager); $methodsForClass->add($methodGetterManager); } $classManager->setMethods($methodsForClass); return $classManager; }
/** * Dump TestClass to file * * @param OutputInterface $output * @param ClassManager $classManager * @param boolean $onlySimulate * @return */ protected function processTestClass(OutputInterface $output, ClassManager $classManager, $onlySimulate = false) { if (false == $classManager->hasTestClass()) { return; } $testClassManager = $classManager->getTestClass(); if (false == $onlySimulate) { $filesManager = $this->getFilesManager(); $filesManager->dump($testClassManager); } $output->writeln('<question>Processed: ' . $classManager->getTestClass()->getNamespace() . '</question>'); $this->outputProcessMethods($output, $testClassManager); if (false == $testClassManager->getMethods()->isEmpty()) { $output->writeln(sprintf('<comment>Implement missing assertions in %s</comment>', $testClassManager->getNamespace())); } $output->writeln(''); }