public function testStringToFile()
 {
     $this->assertTrue(Filesystem::stringToFile('a/test.txt', 'test content', 0666));
     $this->assertSame('test content', file_get_contents('a/test.txt'));
     $this->assertTrue(is_dir('a'));
     Filesystem::removeDir('a');
 }
Esempio n. 2
0
 /**
  * Transforms XML document using XSLT.
  * @param DOMDocument $xmlDom source XML
  * @param DOMDocument $xsltDom source XSLT
  * @param string $outputFilenameBase base of output file with transformed XML
  *		to be created
  * @return bool true on success
  */
 protected function checkXsltValidity(DOMDocument $xmlDom, DOMDocument $xsltDom, $outputFilenameBase)
 {
     $this->useLibxmlErrors();
     $proc = new XSLTProcessor();
     $proc->importStylesheet($xsltDom);
     if (!$this->failGoalOnLibxmlErrors(self::goalValidXslt, null)) {
         return false;
     }
     $this->useLibxmlErrors();
     $newXmlDom = $proc->transformToDoc($xmlDom);
     if (!$this->reachGoalOnNoLibxmlErrors(self::goalValidXslt, null)) {
         return false;
     }
     $output = $newXmlDom->saveHTML();
     $suffix = '.html';
     if (!$output) {
         $output = $newXmlDom->saveXML();
         $suffix = '.xml';
     }
     Filesystem::stringToFile($outputFilenameBase . $suffix, $output);
     return true;
 }
Esempio n. 3
0
    /**
     * Evaluates XPath expressions on XML and outputs results to files.
     * @param DOMDocument $xmlDom source XML
     * @param array $expressions XPath expressions
     * @param array $comments XPath comments belonging to @a $expressions
     * @return bool true on success
     */
    protected function evaluateXpathExpressions($xmlDom, $expressions, $comments)
    {
        $evaluator = new DOMXpath($xmlDom);
        $exprNumber = 0;
        for ($i = 0; $i < count($expressions); ++$i) {
            $expr = $expressions[$i];
            $this->useLibxmlErrors();
            $result = $evaluator->query($expr);
            $errors = $this->getLibxmlErrors();
            if ($errors) {
                return $this->failGoal(self::goalValidXpath, "XPath expression is invalid ({$expr}).");
            }
            $exprComments = !isset($comments[$i]) || empty($comments[$i]) ? '' : <<<COMMENTS
#\tComments from expression file:

{$comments[$i]}

COMMENTS;
            $output = <<<HEADER
#\tResults for XPath expression: {$expr}
{$exprComments}
#\tFound {$result->length} matching DOM nodes.
HEADER;
            $delimiter = "\n\n#\t%s result:\n\n";
            for ($j = 0; $j < $result->length; ++$j) {
                $output .= sprintf($delimiter, "Result " . ($j + 1)) . $xmlDom->saveXML($result->item($j));
            }
            Filesystem::stringToFile(sprintf($this->params[self::outputXpathMask], ++$exprNumber), $output);
        }
        return $this->reachGoal(self::goalValidXpath);
    }