DictionaryTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @package Zend_Pdf
  4. * @subpackage UnitTests
  5. */
  6. /**
  7. * Zend_Pdf_Element_Dictionary
  8. */
  9. require_once 'Zend/Pdf/Element/Dictionary.php';
  10. /**
  11. * PHPUnit Test Case
  12. */
  13. require_once 'PHPUnit/Framework/TestCase.php';
  14. /**
  15. * @package Zend_Pdf
  16. * @subpackage UnitTests
  17. */
  18. class Zend_Pdf_Element_DictionaryTest extends PHPUnit_Framework_TestCase
  19. {
  20. public function testPDFDictionary1()
  21. {
  22. $dictionaryObj = new Zend_Pdf_Element_Dictionary();
  23. $this->assertTrue($dictionaryObj instanceof Zend_Pdf_Element_Dictionary);
  24. }
  25. public function testPDFDictionary2()
  26. {
  27. $srcArray = array();
  28. $srcArray['Bool'] = new Zend_Pdf_Element_Boolean(false);
  29. $srcArray['Number'] = new Zend_Pdf_Element_Numeric(100.426);
  30. $srcArray['Name'] = new Zend_Pdf_Element_Name('MyName');
  31. $srcArray['Text'] = new Zend_Pdf_Element_String('some text');
  32. $srcArray['BinaryText'] = new Zend_Pdf_Element_String_Binary('some text');
  33. $dictionaryObj = new Zend_Pdf_Element_Dictionary($srcArray);
  34. $this->assertTrue($dictionaryObj instanceof Zend_Pdf_Element_Dictionary);
  35. }
  36. public function testPDFDictionaryBadInput1()
  37. {
  38. try {
  39. $arrayObj = new Zend_Pdf_Element_Dictionary(346);
  40. } catch (Zend_Pdf_Exception $e) {
  41. $this->assertRegExp('/must be an array/i', $e->getMessage());
  42. return;
  43. }
  44. $this->fail('Expected Zend_Pdf_Exception to be thrown');
  45. }
  46. public function testPDFDictionaryBadInput2()
  47. {
  48. try {
  49. $srcArray = array();
  50. $srcArray['Bool'] = new Zend_Pdf_Element_Boolean(false);
  51. $srcArray['Number'] = new Zend_Pdf_Element_Numeric(100.426);
  52. $srcArray['Name'] = new Zend_Pdf_Element_Name('MyName');
  53. $srcArray['Text'] = new Zend_Pdf_Element_String('some text');
  54. $srcArray['BinaryText'] = new Zend_Pdf_Element_String_Binary('some text');
  55. $srcArray['bad value'] = 24;
  56. $dictionaryObj = new Zend_Pdf_Element_Dictionary($srcArray);
  57. } catch (Zend_Pdf_Exception $e) {
  58. $this->assertRegExp('/must be Zend_Pdf_Element/i', $e->getMessage());
  59. return;
  60. }
  61. $this->fail('Expected Zend_Pdf_Exception to be thrown');
  62. }
  63. public function testPDFDictionaryBadInput3()
  64. {
  65. try {
  66. $srcArray = array();
  67. $srcArray['Bool'] = new Zend_Pdf_Element_Boolean(false);
  68. $srcArray['Number'] = new Zend_Pdf_Element_Numeric(100.426);
  69. $srcArray['Name'] = new Zend_Pdf_Element_Name('MyName');
  70. $srcArray['Text'] = new Zend_Pdf_Element_String('some text');
  71. $srcArray['BinaryText'] = new Zend_Pdf_Element_String_Binary('some text');
  72. $srcArray[5] = new Zend_Pdf_Element_String('bad name');
  73. $dictionaryObj = new Zend_Pdf_Element_Dictionary($srcArray);
  74. } catch (Zend_Pdf_Exception $e) {
  75. $this->assertRegExp('/keys must be strings/i', $e->getMessage());
  76. return;
  77. }
  78. $this->fail('Expected Zend_Pdf_Exception to be thrown');
  79. }
  80. public function testGetType()
  81. {
  82. $dictionaryObj = new Zend_Pdf_Element_Dictionary();
  83. $this->assertEquals($dictionaryObj->getType(), Zend_Pdf_Element::TYPE_DICTIONARY);
  84. }
  85. public function testToString()
  86. {
  87. $srcArray = array();
  88. $srcArray['Bool'] = new Zend_Pdf_Element_Boolean(false);
  89. $srcArray['Number'] = new Zend_Pdf_Element_Numeric(100.426);
  90. $srcArray['Name'] = new Zend_Pdf_Element_Name('MyName');
  91. $srcArray['Text'] = new Zend_Pdf_Element_String('some text');
  92. $srcArray['BinaryText'] = new Zend_Pdf_Element_String_Binary("\x01\x02\x00\xff");
  93. $dictionaryObj = new Zend_Pdf_Element_Dictionary($srcArray);
  94. $this->assertEquals($dictionaryObj->toString(),
  95. '<</Bool false /Number 100.426 /Name /MyName /Text (some text) /BinaryText <010200FF> >>');
  96. }
  97. public function testAdd()
  98. {
  99. $dictionaryObj = new Zend_Pdf_Element_Dictionary();
  100. $dictionaryObj->add(new Zend_Pdf_Element_Name('Var1'), new Zend_Pdf_Element_Boolean(false));
  101. $dictionaryObj->add(new Zend_Pdf_Element_Name('Var2'), new Zend_Pdf_Element_Numeric(100.426));
  102. $dictionaryObj->add(new Zend_Pdf_Element_Name('Var3'), new Zend_Pdf_Element_Name('MyName'));
  103. $dictionaryObj->add(new Zend_Pdf_Element_Name('Var4'), new Zend_Pdf_Element_String('some text'));
  104. $this->assertEquals($dictionaryObj->toString(),
  105. '<</Var1 false /Var2 100.426 /Var3 /MyName /Var4 (some text) >>');
  106. }
  107. }