DictionaryTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /**
  23. * Zend_Pdf_Element_Dictionary
  24. */
  25. require_once 'Zend/Pdf/Element/Dictionary.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Pdf
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Pdf
  33. */
  34. class Zend_Pdf_Element_DictionaryTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function testPDFDictionary1()
  37. {
  38. $dictionaryObj = new Zend_Pdf_Element_Dictionary();
  39. $this->assertTrue($dictionaryObj instanceof Zend_Pdf_Element_Dictionary);
  40. }
  41. public function testPDFDictionary2()
  42. {
  43. $srcArray = array();
  44. $srcArray['Bool'] = new Zend_Pdf_Element_Boolean(false);
  45. $srcArray['Number'] = new Zend_Pdf_Element_Numeric(100.426);
  46. $srcArray['Name'] = new Zend_Pdf_Element_Name('MyName');
  47. $srcArray['Text'] = new Zend_Pdf_Element_String('some text');
  48. $srcArray['BinaryText'] = new Zend_Pdf_Element_String_Binary('some text');
  49. $dictionaryObj = new Zend_Pdf_Element_Dictionary($srcArray);
  50. $this->assertTrue($dictionaryObj instanceof Zend_Pdf_Element_Dictionary);
  51. }
  52. public function testPDFDictionaryBadInput1()
  53. {
  54. try {
  55. $arrayObj = new Zend_Pdf_Element_Dictionary(346);
  56. } catch (Zend_Pdf_Exception $e) {
  57. $this->assertRegExp('/must be an array/i', $e->getMessage());
  58. return;
  59. }
  60. $this->fail('Expected Zend_Pdf_Exception to be thrown');
  61. }
  62. public function testPDFDictionaryBadInput2()
  63. {
  64. try {
  65. $srcArray = array();
  66. $srcArray['Bool'] = new Zend_Pdf_Element_Boolean(false);
  67. $srcArray['Number'] = new Zend_Pdf_Element_Numeric(100.426);
  68. $srcArray['Name'] = new Zend_Pdf_Element_Name('MyName');
  69. $srcArray['Text'] = new Zend_Pdf_Element_String('some text');
  70. $srcArray['BinaryText'] = new Zend_Pdf_Element_String_Binary('some text');
  71. $srcArray['bad value'] = 24;
  72. $dictionaryObj = new Zend_Pdf_Element_Dictionary($srcArray);
  73. } catch (Zend_Pdf_Exception $e) {
  74. $this->assertRegExp('/must be Zend_Pdf_Element/i', $e->getMessage());
  75. return;
  76. }
  77. $this->fail('Expected Zend_Pdf_Exception to be thrown');
  78. }
  79. public function testPDFDictionaryBadInput3()
  80. {
  81. try {
  82. $srcArray = array();
  83. $srcArray['Bool'] = new Zend_Pdf_Element_Boolean(false);
  84. $srcArray['Number'] = new Zend_Pdf_Element_Numeric(100.426);
  85. $srcArray['Name'] = new Zend_Pdf_Element_Name('MyName');
  86. $srcArray['Text'] = new Zend_Pdf_Element_String('some text');
  87. $srcArray['BinaryText'] = new Zend_Pdf_Element_String_Binary('some text');
  88. $srcArray[5] = new Zend_Pdf_Element_String('bad name');
  89. $dictionaryObj = new Zend_Pdf_Element_Dictionary($srcArray);
  90. } catch (Zend_Pdf_Exception $e) {
  91. $this->assertRegExp('/keys must be strings/i', $e->getMessage());
  92. return;
  93. }
  94. $this->fail('Expected Zend_Pdf_Exception to be thrown');
  95. }
  96. public function testGetType()
  97. {
  98. $dictionaryObj = new Zend_Pdf_Element_Dictionary();
  99. $this->assertEquals($dictionaryObj->getType(), Zend_Pdf_Element::TYPE_DICTIONARY);
  100. }
  101. public function testToString()
  102. {
  103. $srcArray = array();
  104. $srcArray['Bool'] = new Zend_Pdf_Element_Boolean(false);
  105. $srcArray['Number'] = new Zend_Pdf_Element_Numeric(100.426);
  106. $srcArray['Name'] = new Zend_Pdf_Element_Name('MyName');
  107. $srcArray['Text'] = new Zend_Pdf_Element_String('some text');
  108. $srcArray['BinaryText'] = new Zend_Pdf_Element_String_Binary("\x01\x02\x00\xff");
  109. $dictionaryObj = new Zend_Pdf_Element_Dictionary($srcArray);
  110. $this->assertEquals($dictionaryObj->toString(),
  111. '<</Bool false /Number 100.426 /Name /MyName /Text (some text) /BinaryText <010200FF> >>');
  112. }
  113. public function testAdd()
  114. {
  115. $dictionaryObj = new Zend_Pdf_Element_Dictionary();
  116. $dictionaryObj->add(new Zend_Pdf_Element_Name('Var1'), new Zend_Pdf_Element_Boolean(false));
  117. $dictionaryObj->add(new Zend_Pdf_Element_Name('Var2'), new Zend_Pdf_Element_Numeric(100.426));
  118. $dictionaryObj->add(new Zend_Pdf_Element_Name('Var3'), new Zend_Pdf_Element_Name('MyName'));
  119. $dictionaryObj->add(new Zend_Pdf_Element_Name('Var4'), new Zend_Pdf_Element_String('some text'));
  120. $this->assertEquals($dictionaryObj->toString(),
  121. '<</Var1 false /Var2 100.426 /Var3 /MyName /Var4 (some text) >>');
  122. }
  123. }