public function testParseFile() { $parser = new template\parser(null, $adapter = new test\adapter()); $adapter->file_get_contents = false; $this->assert->exception(function () use($parser, &$path) { $parser->parseFile($path = uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to get contents from file \'' . $path . '\''); $adapter->file_get_contents = ''; $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->isEmpty(); $adapter->file_get_contents = $string = uniqid(); $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(1)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\data')->string($root->getChild(0)->getData())->isEqualTo($string); $adapter->file_get_contents = $string = uniqid() . "\n" . uniqid() . "\n"; $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(1)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\data')->string($root->getChild(0)->getData())->isEqualTo($string); $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' />'; $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(1)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\tag')->string($root->getChild(0)->getTag())->isEqualTo($tag)->variable($root->getChild(0)->getId())->isNull()->string($root->getChild(0)->getData())->isEmpty()->integer($root->getChild(0)->getLine())->isEqualTo(1)->integer($root->getChild(0)->getOffset())->isEqualTo(1); $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '/>'; $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(1)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\tag')->string($root->getChild(0)->getTag())->isEqualTo($tag)->variable($root->getChild(0)->getId())->isNull()->string($root->getChild(0)->getData())->isEmpty()->integer($root->getChild(0)->getLine())->isEqualTo(1)->integer($root->getChild(0)->getOffset())->isEqualTo(1); $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '" />'; $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(1)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\tag')->string($root->getChild(0)->getTag())->isEqualTo($tag)->string($root->getChild(0)->getId())->isEqualTo($id)->string($root->getChild(0)->getData())->isEmpty()->integer($root->getChild(0)->getLine())->isEqualTo(1)->integer($root->getChild(0)->getOffset())->isEqualTo(1); $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '"/>'; $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(1)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\tag')->string($root->getChild(0)->getTag())->isEqualTo($tag)->string($root->getChild(0)->getId())->isEqualTo($id)->string($root->getChild(0)->getData())->isEmpty()->integer($root->getChild(0)->getLine())->isEqualTo(1)->integer($root->getChild(0)->getOffset())->isEqualTo(1); $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '>' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>'; $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(1)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\tag')->string($root->getChild(0)->getTag())->isEqualTo($tag)->variable($root->getChild(0)->getId())->isNull()->string($root->getChild(0)->getData())->isEmpty()->integer($root->getChild(0)->getLine())->isEqualTo(1)->integer($root->getChild(0)->getOffset())->isEqualTo(1); $adapter->file_get_contents = ($string1 = uniqid()) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '>' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>' . ($string2 = uniqid()); $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(3)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\data')->string($root->getChild(0)->getData())->isEqualTo($string1)->object($root->getChild(1))->isInstanceOf('mageekguy\\atoum\\template\\tag')->variable($root->getChild(1)->getId())->isNull()->string($root->getChild(1)->getData())->isEmpty()->integer($root->getChild(1)->getLine())->isEqualTo(1)->integer($root->getChild(1)->getOffset())->isEqualTo(strlen($string1) + 1)->object($root->getChild(2))->isInstanceOf('mageekguy\\atoum\\template\\data')->string($root->getChild(2)->getData())->isEqualTo($string2); $adapter->file_get_contents = ($string1 = uniqid()) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '">' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>' . ($string2 = uniqid()); $this->assert->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\\atoum\\template')->array($root->getChildren())->hasSize(3)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\data')->string($root->getChild(0)->getData())->isEqualTo($string1)->object($root->getChild(1))->isInstanceOf('mageekguy\\atoum\\template\\tag')->string($root->getChild(1)->getId())->isEqualTo($id)->string($root->getChild(1)->getData())->isEmpty()->integer($root->getChild(1)->getLine())->isEqualTo(1)->integer($root->getChild(1)->getOffset())->isEqualTo(strlen($string1) + 1)->object($root->getChild(2))->isInstanceOf('mageekguy\\atoum\\template\\data')->string($root->getChild(2)->getData())->isEqualTo($string2); $adapter->file_get_contents = ($string = str_repeat("\n", 6)) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '/>'; $this->assert->object($root = $parser->parseFile(uniqid()))->array($root->getChildren())->hasSize(2)->object($root->getChild(0))->isInstanceOf('mageekguy\\atoum\\template\\data')->string($root->getChild(0)->getData())->isEqualTo($string)->object($root->getChild(1))->isInstanceOf('mageekguy\\atoum\\template\\tag')->variable($root->getChild(1)->getId())->isNull()->string($root->getChild(1)->getData())->isEmpty()->integer($root->getChild(1)->getLine())->isEqualTo(7)->integer($root->getChild(1)->getOffset())->isEqualTo(1); }
/** * @param array $classData * @param int $relevantLines * @param int $coveredLines * @param int $totalLines * @param int $coverage */ protected function buildClassPage($classData, $relevantLines, $coveredLines, $totalLines, $coverage) { // get template $template = $this->templateParser->parseFile($this->templatesDirectory . DIRECTORY_SEPARATOR . 'class.tpl'); // build common $this->buildCommonTemplate($template, array('totalLines' => $totalLines, 'relevantLines' => $relevantLines, 'coveredLines' => $coveredLines, 'coverage' => $coverage), $classData['coverage'], $classData['name']); $methodsTemplates = $template->methods; $methodTemplates = $methodsTemplates->method; $methodCoverageAvailableTemplates = $methodTemplates->methodCoverageAvailable; $methodCoverageUnavailableTemplates = $methodTemplates->methodCoverageUnavailable; $sourceFileTemplates = $template->sourceFile; $templates = array('lineTemplates' => $sourceFileTemplates->line, 'coveredLineTemplates' => $sourceFileTemplates->coveredLine, 'notCoveredLineTemplates' => $sourceFileTemplates->notCoveredLine); $className = $classData['className']; $template->className = $className; $methods = $this->coverage->getCoverageForClass($className); $reflectedMethods = array(); $reflectionClassMethods = $this->getReflectionClass($className)->getMethods(); foreach (array_filter($reflectionClassMethods, function ($reflectedMethod) use($className) { return $reflectedMethod->isAbstract() === false && $reflectedMethod->getDeclaringClass()->getName() === $className; }) as $reflectedMethod) { $reflectedMethods[$reflectedMethod->getName()] = $reflectedMethod; } if (sizeof($reflectedMethods) > 0) { foreach (array_intersect(array_keys($reflectedMethods), array_keys($methods)) as $methodName) { $methodCoverageValue = $this->coverage->getValueForMethod($className, $methodName); if ($methodCoverageValue === null) { $methodCoverageUnavailableTemplates->build(array('methodName' => $methodName)); } else { $methodCoverageAvailableTemplates->build(array('methodName' => $methodName, 'methodCoverageValue' => round($methodCoverageValue * 100, 2), 'methodCoverageRounded' => ceil($methodCoverageValue * 100))); } $methodTemplates->build(); $methodCoverageAvailableTemplates->resetData(); $methodCoverageUnavailableTemplates->resetData(); } $methodsTemplates->build(); $methodTemplates->resetData(); } $srcFile = $this->adapter->fopen($this->sourceDirectory->getRealPath()->__toString() . DIRECTORY_SEPARATOR . $classData['name'], 'r'); if ($srcFile !== false) { $methodLines = array(); foreach ($reflectedMethods as $reflectedMethodName => $reflectedMethod) { $methodLines[$reflectedMethod->getStartLine()] = $reflectedMethodName; } for ($currentMethod = null, $lineNumber = 1, $line = $this->adapter->fgets($srcFile); $line !== false; $lineNumber++, $line = $this->adapter->fgets($srcFile)) { if (isset($methodLines[$lineNumber]) === true) { $currentMethod = $methodLines[$lineNumber]; } switch (true) { case isset($methods[$currentMethod]) === false || (isset($methods[$currentMethod][$lineNumber]) === false || $methods[$currentMethod][$lineNumber] == -2): $lineTemplateName = 'lineTemplates'; break; case isset($methods[$currentMethod]) === true && isset($methods[$currentMethod][$lineNumber]) === true && $methods[$currentMethod][$lineNumber] == -1: $lineTemplateName = 'notCoveredLineTemplates'; break; default: $lineTemplateName = 'coveredLineTemplates'; } $templates[$lineTemplateName]->lineNumber = $lineNumber; $templates[$lineTemplateName]->code = htmlentities($line, ENT_QUOTES, 'UTF-8'); if (isset($methodLines[$lineNumber]) === true) { foreach ($templates[$lineTemplateName]->anchor as $anchorTemplate) { $anchorTemplate->resetData(); $anchorTemplate->method = $currentMethod; $anchorTemplate->build(); } } $templates[$lineTemplateName]->addToParent()->resetData(); } $this->adapter->fclose($srcFile); } $file = $this->destinationDirectory . DIRECTORY_SEPARATOR . str_replace('\\', '/', $className) . self::HTML_EXTENSION_FILE; $directory = $this->adapter->dirname($file); if ($this->adapter->is_dir($directory) === false) { $this->adapter->mkdir($directory, 0777, true); } $this->adapter->file_put_contents($file, (string) $template->build()); }