Added test for image wrapping behind
This commit is contained in:
parent
822ffc19af
commit
d8ea62c50e
|
|
@ -0,0 +1,113 @@
|
|||
<?php
|
||||
namespace PHPWord\Tests;
|
||||
|
||||
use PHPWord;
|
||||
use DOMDocument;
|
||||
|
||||
class TestHelper
|
||||
{
|
||||
/**
|
||||
* @param \PHPWord $PHPWord
|
||||
* @return \PHPWord\Tests\Doc
|
||||
*/
|
||||
public static function getDocument(PHPWord $PHPWord)
|
||||
{
|
||||
$objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
|
||||
$objWriter->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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
namespace PHPWord\Tests;
|
||||
|
||||
use PHPUnit_Framework_TestCase;
|
||||
use PHPWord;
|
||||
|
||||
require_once __DIR__ . '/../../../src/PHPWord.php';
|
||||
|
||||
class ImageTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function tearDown()
|
||||
{
|
||||
TestHelper::clear();
|
||||
}
|
||||
|
||||
public function testImageWrappingStyleBehind()
|
||||
{
|
||||
$PHPWord = new PHPWord();
|
||||
$section = $PHPWord->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);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 37 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
|
|
@ -1,4 +1,8 @@
|
|||
<?php
|
||||
|
||||
date_default_timezone_set('UTC');
|
||||
|
||||
require_once __DIR__ . "/../src/PHPWord/Autoloader.php";
|
||||
PHPWord_Autoloader::register();
|
||||
PHPWord_Autoloader::Register();
|
||||
|
||||
require_once __DIR__ . "/PHPWord/TestHelper.php";
|
||||
|
|
|
|||
Loading…
Reference in New Issue