1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
<?php
namespace TheSeer\fXSL;
use PHPUnit\Framework\TestCase;
class fxsltprocessorTest extends TestCase {
public function testLoadingStylesheetFromFile() {
$dom = new \DOMDocument();
$xsl = new fXSLTProcessor();
$xsl->loadStylesheetFromFile(__DIR__ . '/_data/basic.xsl');
$res = $xsl->transformToDoc($dom);
$this->assertEquals('test', $res->documentElement->nodeName);
}
public function testLoadingStylesheetFromXML() {
$dom = new \DOMDocument();
$xsl = new fXSLTProcessor();
$xsl->loadStylesheetFromXML( file_get_contents(__DIR__ . '/_data/basic.xsl'));
$res = $xsl->transformToDoc($dom);
$this->assertEquals('test', $res->documentElement->nodeName);
}
public function testLoadingInvalidStylesheetFromFileThrowsException() {
$xsl = new fXSLTProcessor();
$this->expectException('\TheSeer\fXSL\fXSLTProcessorException');
$xsl->loadStylesheetFromFile(__DIR__ . '/_data/broken.xsl');
}
public function testTransform() {
$tpl = new \DOMDocument();
$tpl->load(__DIR__ . '/_data/test.xsl');
$dom = new \DOMDocument();
$xsl = new fXSLTProcessor($tpl);
$xsl->registerPHPFunctions("TheSeer\\fXSL\\fxsltprocessorTest::demo");
$test = new fXSLCallback('test:only', 'test');
$test->setObject($this);
$xsl->registerCallback($test);
$result = $xsl->transformToXml($dom);
$testDoc = new \DOMDocument();
$testDoc->loadXML($result);
$testXPath = new \DOMXPath($testDoc);
$demos = $testXPath->evaluate("/root/demo");
/* @var $demos \DOMNodeList */
$this->assertEquals(1, $demos->length);
$this->assertEquals(
self::demo(),
$demos->item(0)->textContent
);
$roots = $testXPath->evaluate("/root/test/root");
/* @var $roots \DOMNodeList */
$this->assertEquals(1, $roots->length);
$this->assertEquals(
$this->bar('hallo', 'welt')->textContent,
$roots->item(0)->textContent
);
}
public static function demo() {
return 'Demo reply';
}
public function bar($a, $b) {
$x = new \DOMDocument();
$x->loadXML('<?xml version="1.0" ?><root />');
$p = $x->createTextNode($a . ' -> ' . $b);
$x->documentElement->appendChild($p);
return $x->documentElement;
}
}
|