2
0

DictionaryTest.php 5.4 KB

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