DestinationTest.php 12 KB

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