DestinationTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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-2015 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. /** Zend_Pdf_Destination */
  23. require_once 'Zend/Pdf/Destination.php';
  24. /** Zend_Pdf_Action */
  25. require_once 'Zend/Pdf/ElementFactory.php';
  26. /** Zend_Pdf */
  27. require_once 'Zend/Pdf.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Pdf
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Pdf
  35. */
  36. class Zend_Pdf_DestinationTest extends PHPUnit_Framework_TestCase
  37. {
  38. public function setUp()
  39. {
  40. date_default_timezone_set('GMT');
  41. }
  42. public function testLoad()
  43. {
  44. $pdf = new Zend_Pdf();
  45. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  46. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  47. // Zend_Pdf_Destination_Zoom
  48. $destArray = new Zend_Pdf_Element_Array();
  49. $destArray->items[] = $page2->getPageDictionary();
  50. $destArray->items[] = new Zend_Pdf_Element_Name('XYZ');
  51. $destArray->items[] = new Zend_Pdf_Element_Numeric(0); // left
  52. $destArray->items[] = new Zend_Pdf_Element_Numeric(842); // top
  53. $destArray->items[] = new Zend_Pdf_Element_Numeric(1); // zoom
  54. $destination = Zend_Pdf_Destination::load($destArray);
  55. $this->assertTrue($destination instanceof Zend_Pdf_Destination_Zoom);
  56. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /XYZ 0 842 1 ]');
  57. // Zend_Pdf_Destination_Fit
  58. $destArray = new Zend_Pdf_Element_Array();
  59. $destArray->items[] = $page2->getPageDictionary();
  60. $destArray->items[] = new Zend_Pdf_Element_Name('Fit');
  61. $destination = Zend_Pdf_Destination::load($destArray);
  62. $this->assertTrue($destination instanceof Zend_Pdf_Destination_Fit);
  63. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /Fit ]');
  64. // Zend_Pdf_Destination_FitHorizontally
  65. $destArray = new Zend_Pdf_Element_Array();
  66. $destArray->items[] = $page2->getPageDictionary();
  67. $destArray->items[] = new Zend_Pdf_Element_Name('FitH');
  68. $destArray->items[] = new Zend_Pdf_Element_Numeric(842); // top
  69. $destination = Zend_Pdf_Destination::load($destArray);
  70. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitHorizontally);
  71. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitH 842 ]');
  72. // Zend_Pdf_Destination_FitVertically
  73. $destArray = new Zend_Pdf_Element_Array();
  74. $destArray->items[] = $page2->getPageDictionary();
  75. $destArray->items[] = new Zend_Pdf_Element_Name('FitV');
  76. $destArray->items[] = new Zend_Pdf_Element_Numeric(0); // left
  77. $destination = Zend_Pdf_Destination::load($destArray);
  78. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitVertically);
  79. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitV 0 ]');
  80. // Zend_Pdf_Destination_FitRectangle
  81. $destArray = new Zend_Pdf_Element_Array();
  82. $destArray->items[] = $page2->getPageDictionary();
  83. $destArray->items[] = new Zend_Pdf_Element_Name('FitR');
  84. $destArray->items[] = new Zend_Pdf_Element_Numeric(0); // left
  85. $destArray->items[] = new Zend_Pdf_Element_Numeric(10); // bottom
  86. $destArray->items[] = new Zend_Pdf_Element_Numeric(595); // right
  87. $destArray->items[] = new Zend_Pdf_Element_Numeric(842); // top
  88. $destination = Zend_Pdf_Destination::load($destArray);
  89. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitRectangle);
  90. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitR 0 10 595 842 ]');
  91. // Zend_Pdf_Destination_FitBoundingBox
  92. $destArray = new Zend_Pdf_Element_Array();
  93. $destArray->items[] = $page2->getPageDictionary();
  94. $destArray->items[] = new Zend_Pdf_Element_Name('FitB');
  95. $destination = Zend_Pdf_Destination::load($destArray);
  96. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitBoundingBox);
  97. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitB ]');
  98. // Zend_Pdf_Destination_FitBoundingBoxHorizontally
  99. $destArray = new Zend_Pdf_Element_Array();
  100. $destArray->items[] = $page2->getPageDictionary();
  101. $destArray->items[] = new Zend_Pdf_Element_Name('FitBH');
  102. $destArray->items[] = new Zend_Pdf_Element_Numeric(842); // top
  103. $destination = Zend_Pdf_Destination::load($destArray);
  104. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitBoundingBoxHorizontally);
  105. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitBH 842 ]');
  106. // Zend_Pdf_Destination_FitBoundingBoxVertically
  107. $destArray = new Zend_Pdf_Element_Array();
  108. $destArray->items[] = $page2->getPageDictionary();
  109. $destArray->items[] = new Zend_Pdf_Element_Name('FitBV');
  110. $destArray->items[] = new Zend_Pdf_Element_Numeric(0); // left
  111. $destination = Zend_Pdf_Destination::load($destArray);
  112. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitBoundingBoxVertically);
  113. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitBV 0 ]');
  114. }
  115. public function testGettersSetters()
  116. {
  117. $pdf = new Zend_Pdf();
  118. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  119. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  120. // Zend_Pdf_Destination_Zoom
  121. $destArray = new Zend_Pdf_Element_Array();
  122. $destArray->items[] = $page2->getPageDictionary();
  123. $destArray->items[] = new Zend_Pdf_Element_Name('XYZ');
  124. $destArray->items[] = new Zend_Pdf_Element_Numeric(0); // left
  125. $destArray->items[] = new Zend_Pdf_Element_Numeric(842); // top
  126. $destArray->items[] = new Zend_Pdf_Element_Numeric(1); // zoom
  127. $destination = Zend_Pdf_Destination::load($destArray);
  128. $this->assertEquals($destination->getLeftEdge(), 0);
  129. $destination->setLeftEdge(5);
  130. $this->assertEquals($destination->getLeftEdge(), 5);
  131. $this->assertEquals($destination->getTopEdge(), 842);
  132. $destination->setTopEdge(825);
  133. $this->assertEquals($destination->getTopEdge(), 825);
  134. $this->assertEquals($destination->getZoomFactor(), 1);
  135. $destination->setZoomFactor(0.5);
  136. $this->assertEquals($destination->getZoomFactor(), 0.5);
  137. // Zend_Pdf_Destination_FitHorizontally
  138. $destArray = new Zend_Pdf_Element_Array();
  139. $destArray->items[] = $page2->getPageDictionary();
  140. $destArray->items[] = new Zend_Pdf_Element_Name('FitH');
  141. $destArray->items[] = new Zend_Pdf_Element_Numeric(842); // top
  142. $destination = Zend_Pdf_Destination::load($destArray);
  143. $this->assertEquals($destination->getTopEdge(), 842);
  144. $destination->setTopEdge(825);
  145. $this->assertEquals($destination->getTopEdge(), 825);
  146. // Zend_Pdf_Destination_FitVertically
  147. $destArray = new Zend_Pdf_Element_Array();
  148. $destArray->items[] = $page2->getPageDictionary();
  149. $destArray->items[] = new Zend_Pdf_Element_Name('FitV');
  150. $destArray->items[] = new Zend_Pdf_Element_Numeric(0); // left
  151. $destination = Zend_Pdf_Destination::load($destArray);
  152. $this->assertEquals($destination->getLeftEdge(), 0);
  153. $destination->setLeftEdge(5);
  154. $this->assertEquals($destination->getLeftEdge(), 5);
  155. // Zend_Pdf_Destination_FitRectangle
  156. $destArray = new Zend_Pdf_Element_Array();
  157. $destArray->items[] = $page2->getPageDictionary();
  158. $destArray->items[] = new Zend_Pdf_Element_Name('FitR');
  159. $destArray->items[] = new Zend_Pdf_Element_Numeric(0); // left
  160. $destArray->items[] = new Zend_Pdf_Element_Numeric(10); // bottom
  161. $destArray->items[] = new Zend_Pdf_Element_Numeric(595); // right
  162. $destArray->items[] = new Zend_Pdf_Element_Numeric(842); // top
  163. $destination = Zend_Pdf_Destination::load($destArray);
  164. $this->assertEquals($destination->getLeftEdge(), 0);
  165. $destination->setLeftEdge(5);
  166. $this->assertEquals($destination->getLeftEdge(), 5);
  167. $this->assertEquals($destination->getBottomEdge(), 10);
  168. $destination->setBottomEdge(20);
  169. $this->assertEquals($destination->getBottomEdge(), 20);
  170. $this->assertEquals($destination->getRightEdge(), 595);
  171. $destination->setRightEdge(590);
  172. $this->assertEquals($destination->getRightEdge(), 590);
  173. $this->assertEquals($destination->getTopEdge(), 842);
  174. $destination->setTopEdge(825);
  175. $this->assertEquals($destination->getTopEdge(), 825);
  176. // Zend_Pdf_Destination_FitBoundingBoxHorizontally
  177. $destArray = new Zend_Pdf_Element_Array();
  178. $destArray->items[] = $page2->getPageDictionary();
  179. $destArray->items[] = new Zend_Pdf_Element_Name('FitBH');
  180. $destArray->items[] = new Zend_Pdf_Element_Numeric(842); // top
  181. $destination = Zend_Pdf_Destination::load($destArray);
  182. $this->assertEquals($destination->getTopEdge(), 842);
  183. $destination->setTopEdge(825);
  184. $this->assertEquals($destination->getTopEdge(), 825);
  185. // Zend_Pdf_Destination_FitBoundingBoxVertically
  186. $destArray = new Zend_Pdf_Element_Array();
  187. $destArray->items[] = $page2->getPageDictionary();
  188. $destArray->items[] = new Zend_Pdf_Element_Name('FitBV');
  189. $destArray->items[] = new Zend_Pdf_Element_Numeric(0); // left
  190. $destination = Zend_Pdf_Destination::load($destArray);
  191. $this->assertEquals($destination->getLeftEdge(), 0);
  192. $destination->setLeftEdge(5);
  193. $this->assertEquals($destination->getLeftEdge(), 5);
  194. }
  195. public function testCreate()
  196. {
  197. $pdf = new Zend_Pdf();
  198. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  199. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  200. $destination = Zend_Pdf_Destination_Zoom::create($page2, 0, 842, 0.5);
  201. $this->assertTrue($destination instanceof Zend_Pdf_Destination_Zoom);
  202. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /XYZ 0 842 0.5 ]');
  203. $destination = Zend_Pdf_Destination_Fit::create($page2);
  204. $this->assertTrue($destination instanceof Zend_Pdf_Destination_Fit);
  205. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /Fit ]');
  206. $destination = Zend_Pdf_Destination_FitHorizontally::create($page2, 842);
  207. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitHorizontally);
  208. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitH 842 ]');
  209. $destination = Zend_Pdf_Destination_FitVertically::create(2, 0);
  210. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitVertically);
  211. $this->assertEquals($destination->getResource()->toString(), '[2 /FitV 0 ]');
  212. $destination = Zend_Pdf_Destination_FitRectangle::create($page1, 0, 10, 595, 842);
  213. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitRectangle);
  214. $this->assertEquals($destination->getResource()->toString(), '[3 0 R /FitR 0 10 595 842 ]');
  215. $destination = Zend_Pdf_Destination_FitBoundingBox::create(1);
  216. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitBoundingBox);
  217. $this->assertEquals($destination->getResource()->toString(), '[1 /FitB ]');
  218. $destination = Zend_Pdf_Destination_FitBoundingBoxHorizontally::create($page2, 842);
  219. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitBoundingBoxHorizontally);
  220. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitBH 842 ]');
  221. $destination = Zend_Pdf_Destination_FitBoundingBoxVertically::create($page2, 0);
  222. $this->assertTrue($destination instanceof Zend_Pdf_Destination_FitBoundingBoxVertically);
  223. $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitBV 0 ]');
  224. }
  225. }