PdfTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Pdf
  24. */
  25. require_once 'Zend/Pdf.php';
  26. /**
  27. * @see Zend_Pdf_Exception
  28. */
  29. require_once 'Zend/Pdf/Exception.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Pdf
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Pdf
  37. */
  38. class Zend_PdfTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function testGetJavasriptNull()
  41. {
  42. // getting JavaScript without setting it returns NULL
  43. $pdf = new Zend_Pdf();
  44. $this->assertNull($pdf->getJavaScript());
  45. }
  46. public function testSetAndGetJavasriptArray()
  47. {
  48. // getting JavaScript after setting it returns array
  49. $pdf = new Zend_Pdf();
  50. $pdf->setJavaScript('print();');
  51. $this->assertTrue(is_array($pdf->getJavaScript()));
  52. }
  53. public function testSetJavaScriptString()
  54. {
  55. // setting string value is possible
  56. $pdf = new Zend_Pdf();
  57. $javaScriptString = 'print();';
  58. $pdf->setJavaScript($javaScriptString);
  59. $javaScript = $pdf->getJavaScript();
  60. $this->assertEquals($javaScriptString, $javaScript[0]);
  61. }
  62. public function testSetJavaScriptArray()
  63. {
  64. // setting string value is possible
  65. $pdf = new Zend_Pdf();
  66. $javaScriptArray = array('print();', 'alert();');
  67. $pdf->setJavaScript($javaScriptArray);
  68. $this->assertEquals($javaScriptArray, $pdf->getJavaScript());
  69. }
  70. public function testResetJavaScript()
  71. {
  72. // reset removes the added JavaScript
  73. $pdf = new Zend_Pdf();
  74. $pdf->setJavaScript('print();');
  75. $pdf->resetJavaScript();
  76. $this->assertNull($pdf->getJavaScript());
  77. }
  78. public function testAddJavaScript()
  79. {
  80. // adding JavaScript appends previously added JavaScript
  81. $pdf = new Zend_Pdf();
  82. $javaScriptArray = array('print();', 'alert();');
  83. $pdf->addJavaScript($javaScriptArray[0]);
  84. $pdf->addJavaScript($javaScriptArray[1]);
  85. $this->assertEquals($javaScriptArray, $pdf->getJavaScript());
  86. }
  87. public function testSetJavaScriptEmptyString()
  88. {
  89. // setting empty JavaScript string throws exception
  90. $pdf = new Zend_Pdf();
  91. try {
  92. $pdf->setJavaScript('');
  93. $this->fail('Expected exception when trying to set empty string.');
  94. } catch (Zend_Pdf_Exception $e) {
  95. $this->assertContains('JavaScript must be a non empty string or array of strings', $e->getMessage());
  96. }
  97. }
  98. public function testSetJavaScriptEmptyArray()
  99. {
  100. // setting empty JavaScript string throws exception
  101. $pdf = new Zend_Pdf();
  102. try {
  103. $pdf->setJavaScript(array());
  104. $this->fail('Expected exception when trying to set empty array.');
  105. } catch (Zend_Pdf_Exception $e) {
  106. $this->assertContains('JavaScript must be a non empty string or array of strings', $e->getMessage());
  107. }
  108. }
  109. public function testSetAndSaveLoadAndGetJavaScript()
  110. {
  111. $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile');
  112. $javaScript = array('print();', 'alert();');
  113. $pdf = new Zend_Pdf();
  114. $pdf->setJavaScript($javaScript);
  115. $pdf->save($tempFile);
  116. unset($pdf);
  117. $pdf = Zend_Pdf::load($tempFile);
  118. unlink($tempFile);
  119. $this->assertEquals($javaScript, $pdf->getJavaScript());
  120. }
  121. public function testSetAndSaveLoadAndResetAndSaveLoadAndGetJavaScript()
  122. {
  123. $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile');
  124. $javaScript = array('print();', 'alert();');
  125. $pdf = new Zend_Pdf();
  126. $pdf->setJavaScript($javaScript);
  127. $pdf->save($tempFile);
  128. unset($pdf);
  129. $pdf = Zend_Pdf::load($tempFile);
  130. unlink($tempFile);
  131. $pdf->resetJavaScript();
  132. $pdf->save($tempFile);
  133. unset($pdf);
  134. $pdf = Zend_Pdf::load($tempFile);
  135. unlink($tempFile);
  136. $this->assertNull($pdf->getJavaScript());
  137. }
  138. }