Added test for image wrapping behind

This commit is contained in:
Gabriel Bull 2013-12-11 16:09:58 -05:00
parent 822ffc19af
commit d8ea62c50e
6 changed files with 156 additions and 1 deletions

113
test/PHPWord/TestHelper.php Normal file
View File

@ -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);
}
}

View File

@ -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

View File

@ -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";