DestinationTest.php 13 KB

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