Skip to content

Commit ef40717

Browse files
committed
rewrite test to use phpunit assertions
1 parent 3673dd5 commit ef40717

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -218,58 +218,51 @@ public function testSetImageValue()
218218

219219
$docName = 'header-footer-images-test-result.docx';
220220
$templateProcessor->saveAs($docName);
221-
$docFound = file_exists($docName);
222221

223-
if ($docFound) {
224-
$expectedDocumentZip = new \ZipArchive();
225-
$expectedDocumentZip->open($docName);
226-
$expectedContentTypesXml = $expectedDocumentZip->getFromName('[Content_Types].xml');
227-
$expectedDocumentRelationsXml = $expectedDocumentZip->getFromName('word/_rels/document.xml.rels');
228-
$expectedHeaderRelationsXml = $expectedDocumentZip->getFromName('word/_rels/header1.xml.rels');
229-
$expectedFooterRelationsXml = $expectedDocumentZip->getFromName('word/_rels/footer1.xml.rels');
230-
$expectedMainPartXml = $expectedDocumentZip->getFromName('word/document.xml');
231-
$expectedHeaderPartXml = $expectedDocumentZip->getFromName('word/header1.xml');
232-
$expectedFooterPartXml = $expectedDocumentZip->getFromName('word/footer1.xml');
233-
$expectedImage = $expectedDocumentZip->getFromName('word/media/image_rId11_document.jpeg');
234-
if (false === $expectedDocumentZip->close()) {
235-
throw new \Exception("Could not close zip file \"{$docName}\".");
236-
}
237-
238-
$this->assertTrue(!empty($expectedImage), 'Embed image doesn\'t found.');
239-
$this->assertTrue(strpos($expectedContentTypesXml, '/word/media/image_rId11_document.jpeg') > 0, '[Content_Types].xml missed "/word/media/image5_document.jpeg"');
240-
$this->assertTrue(strpos($expectedContentTypesXml, '/word/_rels/header1.xml.rels') > 0, '[Content_Types].xml missed "/word/_rels/header1.xml.rels"');
241-
$this->assertTrue(strpos($expectedContentTypesXml, '/word/_rels/footer1.xml.rels') > 0, '[Content_Types].xml missed "/word/_rels/footer1.xml.rels"');
242-
$this->assertTrue(strpos($expectedMainPartXml, '${documentContent}') === false, 'word/document.xml has no image.');
243-
$this->assertTrue(strpos($expectedHeaderPartXml, '${headerValue}') === false, 'word/header1.xml has no image.');
244-
$this->assertTrue(strpos($expectedFooterPartXml, '${footerValue}') === false, 'word/footer1.xml has no image.');
245-
$this->assertTrue(strpos($expectedDocumentRelationsXml, 'media/image_rId11_document.jpeg') > 0, 'word/_rels/document.xml.rels missed "media/image5_document.jpeg"');
246-
$this->assertTrue(strpos($expectedHeaderRelationsXml, 'media/image_rId11_document.jpeg') > 0, 'word/_rels/header1.xml.rels missed "media/image5_document.jpeg"');
247-
$this->assertTrue(strpos($expectedFooterRelationsXml, 'media/image_rId11_document.jpeg') > 0, 'word/_rels/footer1.xml.rels missed "media/image5_document.jpeg"');
248-
249-
unlink($docName);
250-
} else {
251-
throw new \Exception("Generated file '{$docName}' not found!");
222+
$this->assertFileExists($docName, "Generated file '{$docName}' not found!");
223+
224+
$expectedDocumentZip = new \ZipArchive();
225+
$expectedDocumentZip->open($docName);
226+
$expectedContentTypesXml = $expectedDocumentZip->getFromName('[Content_Types].xml');
227+
$expectedDocumentRelationsXml = $expectedDocumentZip->getFromName('word/_rels/document.xml.rels');
228+
$expectedHeaderRelationsXml = $expectedDocumentZip->getFromName('word/_rels/header1.xml.rels');
229+
$expectedFooterRelationsXml = $expectedDocumentZip->getFromName('word/_rels/footer1.xml.rels');
230+
$expectedMainPartXml = $expectedDocumentZip->getFromName('word/document.xml');
231+
$expectedHeaderPartXml = $expectedDocumentZip->getFromName('word/header1.xml');
232+
$expectedFooterPartXml = $expectedDocumentZip->getFromName('word/footer1.xml');
233+
$expectedImage = $expectedDocumentZip->getFromName('word/media/image_rId11_document.jpeg');
234+
if (false === $expectedDocumentZip->close()) {
235+
throw new \Exception("Could not close zip file \"{$docName}\".");
252236
}
253237

238+
$this->assertNotEmpty($expectedImage, 'Embed image doesn\'t found.');
239+
$this->assertStringContainsString('/word/media/image_rId11_document.jpeg', $expectedContentTypesXml, '[Content_Types].xml missed "/word/media/image5_document.jpeg"');
240+
$this->assertStringContainsString('/word/_rels/header1.xml.rels', $expectedContentTypesXml, '[Content_Types].xml missed "/word/_rels/header1.xml.rels"');
241+
$this->assertStringContainsString('/word/_rels/footer1.xml.rels', $expectedContentTypesXml, '[Content_Types].xml missed "/word/_rels/footer1.xml.rels"');
242+
$this->assertStringNotContainsString('${documentContent}', $expectedMainPartXml, 'word/document.xml has no image.');
243+
$this->assertStringNotContainsString('${headerValue}', $expectedHeaderPartXml, 'word/header1.xml has no image.');
244+
$this->assertStringNotContainsString('${footerValue}', $expectedFooterPartXml, 'word/footer1.xml has no image.');
245+
$this->assertStringContainsString('media/image_rId11_document.jpeg', $expectedDocumentRelationsXml, 'word/_rels/document.xml.rels missed "media/image5_document.jpeg"');
246+
$this->assertStringContainsString('media/image_rId11_document.jpeg', $expectedHeaderRelationsXml, 'word/_rels/header1.xml.rels missed "media/image5_document.jpeg"');
247+
$this->assertStringContainsString('media/image_rId11_document.jpeg', $expectedFooterRelationsXml, 'word/_rels/footer1.xml.rels missed "media/image5_document.jpeg"');
248+
249+
unlink($docName);
250+
254251
// dynamic generated doc
255252
$testFileName = 'images-test-sample.docx';
256253
$phpWord = new \PhpOffice\PhpWord\PhpWord();
257254
$section = $phpWord->addSection();
258255
$section->addText('${Test} --- ${Test}');
259256
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
260257
$objWriter->save($testFileName);
261-
if (!file_exists($testFileName)) {
262-
throw new \Exception("Generated file '{$testFileName}' not found!");
263-
}
258+
$this->assertFileExists($testFileName, "Generated file '{$testFileName}' not found!");
264259

265260
$resultFileName = 'images-test-result.docx';
266261
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($testFileName);
267262
unlink($testFileName);
268263
$templateProcessor->setImageValue('Test', $imagePath);
269264
$templateProcessor->saveAs($resultFileName);
270-
if (!file_exists($resultFileName)) {
271-
throw new \Exception("Generated file '{$resultFileName}' not found!");
272-
}
265+
$this->assertFileExists($resultFileName, "Generated file '{$resultFileName}' not found!");
273266

274267
$expectedDocumentZip = new \ZipArchive();
275268
$expectedDocumentZip->open($resultFileName);
@@ -279,7 +272,7 @@ public function testSetImageValue()
279272
}
280273
unlink($resultFileName);
281274

282-
$this->assertTrue(strpos($expectedMainPartXml, '${Test}') === false, 'word/document.xml has no image.');
275+
$this->assertStringNotContainsString('${Test}', $expectedMainPartXml, 'word/document.xml has no image.');
283276
}
284277

285278
/**

0 commit comments

Comments
 (0)