diff --git a/test/PHPWord/TestHelper.php b/test/PHPWord/TestHelper.php new file mode 100644 index 00000000..db626428 --- /dev/null +++ b/test/PHPWord/TestHelper.php @@ -0,0 +1,113 @@ +save(__DIR__ . '/Tests/_files/test.docx'); + + $zip = new \ZipArchive; + $res = $zip->open(__DIR__ . '/Tests/_files/test.docx'); + if ($res === true) { + $zip->extractTo(__DIR__ . '/Tests/_files/test/'); + $zip->close(); + } + + return new Doc(__DIR__ . '/Tests/_files/test/'); + } + + public static function clear() + { + unlink(__DIR__ . '/Tests/_files/test.docx'); + self::deleteDir(__DIR__ . '/Tests/_files/test/'); + } + + /** + * @param string $dir + */ + public static function deleteDir($dir) + { + foreach (scandir($dir) as $file) { + if ($file === '.' || $file === '..') { + continue; + } else if (is_file($dir . "/" . $file)) { + unlink($dir . "/" . $file); + } else if (is_dir($dir . "/" . $file)) { + self::deleteDir($dir . "/" . $file); + } + } + + rmdir($dir); + } +} + +class Doc +{ + /** @var string $path */ + private $path; + + /** @var \DOMDocument $dom */ + private $dom; + + /** @var \DOMXpath $xpath */ + private $xpath; + + /** @var string $file */ + private $file; + + /** + * @param string $path + */ + public function __construct($path) + { + $this->path = realpath($path); + } + + /** + * @param string $file + * @return \DOMDocument + */ + public function getFileDom($file = 'word/document.xml') + { + if (null !== $this->dom && $file === $this->file) { + return $this->dom; + } + + $this->xpath = null; + $this->file = $file; + + $file = $this->path . '/' . $file; + $this->dom = new DOMDocument(); + $this->dom->load($file); + return $this->dom; + } + + /** + * @param string $path + * @param string $file + * @return \DOMElement + */ + public function getElement($path, $file = 'word/document.xml') + { + if ($this->dom === null || $file !== $this->file) { + $this->getFileDom($file); + } + + if (null === $this->xpath) { + $this->xpath = new \DOMXpath($this->dom); + + } + + $elements = $this->xpath->query($path); + return $elements->item(0); + } +} \ No newline at end of file diff --git a/test/PHPWord/Tests/ImageTest.php b/test/PHPWord/Tests/ImageTest.php new file mode 100644 index 00000000..c1ee9afd --- /dev/null +++ b/test/PHPWord/Tests/ImageTest.php @@ -0,0 +1,38 @@ +createSection(12240, 15840, 0, 0, 0, 0); + + $section->addImage( + __DIR__ . '/_files/images/earth.jpg', + array( + 'marginTop' => -1, + 'marginLeft' => -1, + 'wrappingStyle' => 'behind' + ) + ); + + $doc = TestHelper::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape'); + + $style = $element->getAttribute('style'); + + $this->assertRegExp('/z\-index:\-[0-9]*/', $style); + $this->assertRegExp('/position:absolute;/', $style); + } +} \ No newline at end of file diff --git a/test/PHPWord/Tests/_files/.gitkeep b/test/PHPWord/Tests/_files/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/PHPWord/Tests/_files/images/earth.jpg b/test/PHPWord/Tests/_files/images/earth.jpg new file mode 100644 index 00000000..28537b06 Binary files /dev/null and b/test/PHPWord/Tests/_files/images/earth.jpg differ diff --git a/test/PHPWord/Tests/_files/images/mars.jpg b/test/PHPWord/Tests/_files/images/mars.jpg new file mode 100644 index 00000000..584d3171 Binary files /dev/null and b/test/PHPWord/Tests/_files/images/mars.jpg differ diff --git a/test/bootstrap.php b/test/bootstrap.php index 564f3f76..0ca799f6 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -1,4 +1,8 @@