Skip to content

Commit d8ea62c

Browse files
author
Gabriel Bull
committed
Added test for image wrapping behind
1 parent 822ffc1 commit d8ea62c

File tree

6 files changed

+156
-1
lines changed

6 files changed

+156
-1
lines changed

test/PHPWord/TestHelper.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPWord;
5+
use DOMDocument;
6+
7+
class TestHelper
8+
{
9+
/**
10+
* @param \PHPWord $PHPWord
11+
* @return \PHPWord\Tests\Doc
12+
*/
13+
public static function getDocument(PHPWord $PHPWord)
14+
{
15+
$objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
16+
$objWriter->save(__DIR__ . '/Tests/_files/test.docx');
17+
18+
$zip = new \ZipArchive;
19+
$res = $zip->open(__DIR__ . '/Tests/_files/test.docx');
20+
if ($res === true) {
21+
$zip->extractTo(__DIR__ . '/Tests/_files/test/');
22+
$zip->close();
23+
}
24+
25+
return new Doc(__DIR__ . '/Tests/_files/test/');
26+
}
27+
28+
public static function clear()
29+
{
30+
unlink(__DIR__ . '/Tests/_files/test.docx');
31+
self::deleteDir(__DIR__ . '/Tests/_files/test/');
32+
}
33+
34+
/**
35+
* @param string $dir
36+
*/
37+
public static function deleteDir($dir)
38+
{
39+
foreach (scandir($dir) as $file) {
40+
if ($file === '.' || $file === '..') {
41+
continue;
42+
} else if (is_file($dir . "/" . $file)) {
43+
unlink($dir . "/" . $file);
44+
} else if (is_dir($dir . "/" . $file)) {
45+
self::deleteDir($dir . "/" . $file);
46+
}
47+
}
48+
49+
rmdir($dir);
50+
}
51+
}
52+
53+
class Doc
54+
{
55+
/** @var string $path */
56+
private $path;
57+
58+
/** @var \DOMDocument $dom */
59+
private $dom;
60+
61+
/** @var \DOMXpath $xpath */
62+
private $xpath;
63+
64+
/** @var string $file */
65+
private $file;
66+
67+
/**
68+
* @param string $path
69+
*/
70+
public function __construct($path)
71+
{
72+
$this->path = realpath($path);
73+
}
74+
75+
/**
76+
* @param string $file
77+
* @return \DOMDocument
78+
*/
79+
public function getFileDom($file = 'word/document.xml')
80+
{
81+
if (null !== $this->dom && $file === $this->file) {
82+
return $this->dom;
83+
}
84+
85+
$this->xpath = null;
86+
$this->file = $file;
87+
88+
$file = $this->path . '/' . $file;
89+
$this->dom = new DOMDocument();
90+
$this->dom->load($file);
91+
return $this->dom;
92+
}
93+
94+
/**
95+
* @param string $path
96+
* @param string $file
97+
* @return \DOMElement
98+
*/
99+
public function getElement($path, $file = 'word/document.xml')
100+
{
101+
if ($this->dom === null || $file !== $this->file) {
102+
$this->getFileDom($file);
103+
}
104+
105+
if (null === $this->xpath) {
106+
$this->xpath = new \DOMXpath($this->dom);
107+
108+
}
109+
110+
$elements = $this->xpath->query($path);
111+
return $elements->item(0);
112+
}
113+
}

test/PHPWord/Tests/ImageTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord;
6+
7+
require_once __DIR__ . '/../../../src/PHPWord.php';
8+
9+
class ImageTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function tearDown()
12+
{
13+
TestHelper::clear();
14+
}
15+
16+
public function testImageWrappingStyleBehind()
17+
{
18+
$PHPWord = new PHPWord();
19+
$section = $PHPWord->createSection(12240, 15840, 0, 0, 0, 0);
20+
21+
$section->addImage(
22+
__DIR__ . '/_files/images/earth.jpg',
23+
array(
24+
'marginTop' => -1,
25+
'marginLeft' => -1,
26+
'wrappingStyle' => 'behind'
27+
)
28+
);
29+
30+
$doc = TestHelper::getDocument($PHPWord);
31+
$element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape');
32+
33+
$style = $element->getAttribute('style');
34+
35+
$this->assertRegExp('/z\-index:\-[0-9]*/', $style);
36+
$this->assertRegExp('/position:absolute;/', $style);
37+
}
38+
}

test/PHPWord/Tests/_files/.gitkeep

Whitespace-only changes.
36.8 KB
Loading
23.9 KB
Loading

test/bootstrap.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?php
22

3+
date_default_timezone_set('UTC');
4+
35
require_once __DIR__ . "/../src/PHPWord/Autoloader.php";
4-
PHPWord_Autoloader::register();
6+
PHPWord_Autoloader::Register();
7+
8+
require_once __DIR__ . "/PHPWord/TestHelper.php";

0 commit comments

Comments
 (0)