PhpPresentationTestC 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace PhpOffice\PhpPresentation\Tests;
  3. use PhpOffice\PhpPresentation\IOFactory;
  4. use PhpOffice\PhpPresentation\PhpPresentation;
  5. class PhpPresentationTestCase extends \PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var PhpPresentation
  9. */
  10. protected $oPresentation;
  11. /**
  12. * @var string
  13. */
  14. protected $filePath;
  15. /**
  16. * @var string
  17. */
  18. protected $workDirectory;
  19. /**
  20. * @var string
  21. */
  22. protected $writerName;
  23. /**
  24. * DOMDocument object
  25. *
  26. * @var \DOMDocument
  27. */
  28. private $xmlDom;
  29. /**
  30. * DOMXpath object
  31. *
  32. * @var \DOMXpath
  33. */
  34. private $xmlXPath;
  35. /**
  36. * File name
  37. *
  38. * @var string
  39. */
  40. private $xmlFile;
  41. /**
  42. * Executed before each method of the class
  43. */
  44. public function setUp()
  45. {
  46. $this->workDirectory = sys_get_temp_dir() . '/PhpPresentation_Unit_Test/';
  47. $this->oPresentation = new PhpPresentation();
  48. $this->filePath = tempnam(sys_get_temp_dir(), 'PhpPresentation');
  49. $this->resetPresentationFile();
  50. }
  51. /**
  52. * Executed after each method of the class
  53. */
  54. public function tearDown()
  55. {
  56. $this->oPresentation = null;
  57. $this->resetPresentationFile();
  58. }
  59. /**
  60. * Delete directory
  61. *
  62. * @param string $dir
  63. */
  64. private function deleteDir($dir)
  65. {
  66. foreach (scandir($dir) as $file) {
  67. if ($file === '.' || $file === '..') {
  68. continue;
  69. } elseif (is_file($dir . "/" . $file)) {
  70. unlink($dir . "/" . $file);
  71. } elseif (is_dir($dir . "/" . $file)) {
  72. $this->deleteDir($dir . "/" . $file);
  73. }
  74. }
  75. rmdir($dir);
  76. }
  77. protected function getXmlDom($file)
  78. {
  79. $baseFile = $file;
  80. if (null !== $this->xmlDom && $file === $this->xmlFile) {
  81. return $this->xmlDom;
  82. }
  83. $this->xmlXPath = null;
  84. $this->xmlFile = $file;
  85. $file = $this->workDirectory . '/' . $file;
  86. $this->xmlDom = new \DOMDocument();
  87. $strContent = file_get_contents($file);
  88. // docProps/app.xml
  89. if ($baseFile == 'docProps/app.xml') {
  90. $strContent = str_replace(' xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"', '', $strContent);
  91. }
  92. // docProps/custom.xml
  93. if ($baseFile == 'docProps/custom.xml') {
  94. $strContent = str_replace(' xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"', '', $strContent);
  95. }
  96. // _rels/.rels
  97. if (strpos($baseFile, '_rels/') !== false && strpos($baseFile, '.rels') !== false) {
  98. $strContent = str_replace(' xmlns="http://schemas.openxmlformats.org/package/2006/relationships"', '', $strContent);
  99. }
  100. $this->xmlDom->loadXML($strContent);
  101. return $this->xmlDom;
  102. }
  103. private function getXmlNodeList($file, $xpath)
  104. {
  105. if ($this->xmlDom === null || $file !== $this->xmlFile) {
  106. $this->getXmlDom($file);
  107. }
  108. if (null === $this->xmlXPath) {
  109. $this->xmlXPath = new \DOMXpath($this->xmlDom);
  110. }
  111. return $this->xmlXPath->query($xpath);
  112. }
  113. /**
  114. * @param PhpPresentation $oPhpPresentation
  115. * @param string $writerName
  116. */
  117. protected function writePresentationFile(PhpPresentation $oPhpPresentation, $writerName)
  118. {
  119. if (is_file($this->filePath)) {
  120. return;
  121. }
  122. $xmlWriter = IOFactory::createWriter($oPhpPresentation, $writerName);
  123. $xmlWriter->save($this->filePath);
  124. $zip = new \ZipArchive;
  125. $res = $zip->open($this->filePath);
  126. if ($res === true) {
  127. $zip->extractTo($this->workDirectory);
  128. $zip->close();
  129. }
  130. }
  131. protected function resetPresentationFile()
  132. {
  133. $this->xmlFile = null;
  134. $this->xmlDom = null;
  135. $this->xmlXPath = null;
  136. if (is_file($this->filePath)) {
  137. unlink($this->filePath);
  138. }
  139. if (is_dir($this->workDirectory)) {
  140. $this->deleteDir($this->workDirectory);
  141. }
  142. if (!is_dir($this->workDirectory)) {
  143. mkdir($this->workDirectory);
  144. }
  145. }
  146. /**
  147. * @param string $filePath
  148. */
  149. public function assertZipFileExists($filePath)
  150. {
  151. $this->writePresentationFile($this->oPresentation, $this->writerName);
  152. self::assertThat(is_file($this->workDirectory . $filePath), self::isTrue());
  153. }
  154. /**
  155. * @param string $filePath
  156. */
  157. public function assertZipFileNotExists($filePath)
  158. {
  159. $this->writePresentationFile($this->oPresentation, $this->writerName);
  160. self::assertThat(is_file($this->workDirectory . $filePath), self::isFalse());
  161. }
  162. /**
  163. * @param string $filePath
  164. * @param string $xPath
  165. */
  166. public function assertZipXmlElementExists($filePath, $xPath)
  167. {
  168. $this->writePresentationFile($this->oPresentation, $this->writerName);
  169. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  170. self::assertThat(!($nodeList->length == 0), self::isTrue());
  171. }
  172. /**
  173. * @param string $filePath
  174. * @param string $xPath
  175. */
  176. public function assertZipXmlElementNotExists($filePath, $xPath)
  177. {
  178. $this->writePresentationFile($this->oPresentation, $this->writerName);
  179. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  180. self::assertThat(!($nodeList->length == 0), self::isFalse());
  181. }
  182. /**
  183. * @param string $filePath
  184. * @param string $xPath
  185. * @param mixed $value
  186. */
  187. public function assertZipXmlElementEquals($filePath, $xPath, $value)
  188. {
  189. $this->writePresentationFile($this->oPresentation, $this->writerName);
  190. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  191. self::assertEquals($nodeList->item(0)->nodeValue, $value);
  192. }
  193. /**
  194. * @param string $filePath
  195. * @param string $xPath
  196. * @param int $num
  197. */
  198. public function assertZipXmlElementCount($filePath, $xPath, $num)
  199. {
  200. $this->writePresentationFile($this->oPresentation, $this->writerName);
  201. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  202. self::assertEquals($nodeList->length, $num);
  203. }
  204. /**
  205. * @param string $filePath
  206. * @param string $xPath
  207. * @param string $attribute
  208. * @param mixed $value
  209. */
  210. public function assertZipXmlAttributeEquals($filePath, $xPath, $attribute, $value)
  211. {
  212. $this->writePresentationFile($this->oPresentation, $this->writerName);
  213. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  214. self::assertEquals($value, $nodeList->item(0)->getAttribute($attribute));
  215. }
  216. /**
  217. * @param string $filePath
  218. * @param string $xPath
  219. * @param string $attribute
  220. * @param mixed $value
  221. */
  222. public function assertZipXmlAttributeStartsWith($filePath, $xPath, $attribute, $value)
  223. {
  224. $this->writePresentationFile($this->oPresentation, $this->writerName);
  225. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  226. self::assertStringStartsWith($value, $nodeList->item(0)->getAttribute($attribute));
  227. }
  228. /**
  229. * @param string $filePath
  230. * @param string $xPath
  231. * @param string $attribute
  232. * @param mixed $value
  233. */
  234. public function assertZipXmlAttributeEndsWith($filePath, $xPath, $attribute, $value)
  235. {
  236. $this->writePresentationFile($this->oPresentation, $this->writerName);
  237. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  238. self::assertStringEndsWith($value, $nodeList->item(0)->getAttribute($attribute));
  239. }
  240. /**
  241. * @param string $filePath
  242. * @param string $xPath
  243. * @param string $attribute
  244. * @param mixed $value
  245. */
  246. public function assertZipXmlAttributeContains($filePath, $xPath, $attribute, $value)
  247. {
  248. $this->writePresentationFile($this->oPresentation, $this->writerName);
  249. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  250. self::assertContains($value, $nodeList->item(0)->getAttribute($attribute));
  251. }
  252. /**
  253. * @param string $filePath
  254. * @param string $xPath
  255. * @param string $attribute
  256. */
  257. public function assertZipXmlAttributeExists($filePath, $xPath, $attribute)
  258. {
  259. $this->writePresentationFile($this->oPresentation, $this->writerName);
  260. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  261. self::assertTrue($nodeList->item(0)->hasAttribute($attribute));
  262. }
  263. /**
  264. * @param string $filePath
  265. * @param string $xPath
  266. * @param string $attribute
  267. */
  268. public function assertZipXmlAttributeNotExists($filePath, $xPath, $attribute)
  269. {
  270. $this->writePresentationFile($this->oPresentation, $this->writerName);
  271. $nodeList = $this->getXmlNodeList($filePath, $xPath);
  272. self::assertFalse($nodeList->item(0)->hasAttribute($attribute));
  273. }
  274. }