Browse Source

Added Unit Tests for Zend_Pdf

Commit Includes:
- PdfTest.php containing unit tests for some methods of Zend_Pdf
- Integration of PdfTest.php in AllTests.php
- PdfWithFields.pdf sample PDF file to assert functionality related to form fields

Tests include unit tests for following methods:
- `Zend_Pdf::getTextFieldNames()`
- `Zend_Pdf::setTextField()`
- `Zend_Pdf::setTextFieldProperties()`
- `Zend_Pdf::markTextFieldAsReadOnly()`
Muhammad Adeel Nawaz 11 years ago
parent
commit
f86ad01494
2 changed files with 102 additions and 1 deletions
  1. BIN
      tests/Zend/Pdf/_files/PdfWithFields.pdf
  2. 102 1
      tests/Zend/PdfTest.php

BIN
tests/Zend/Pdf/_files/PdfWithFields.pdf


+ 102 - 1
tests/Zend/PdfTest.php

@@ -39,6 +39,108 @@ require_once 'Zend/Pdf/Exception.php';
  */
 class Zend_PdfTest extends PHPUnit_Framework_TestCase
 {
+    /**
+     *
+     * @var NULL|Zend_Pdf
+     */
+    private $_pdf = NULL;
+
+    protected function setUp()
+    {
+        $this->_pdf = Zend_Pdf::load(__DIR__ . '/Pdf/_files/PdfWithFields.pdf');
+    }
+
+    public function testGetTextFieldNames()
+    {
+        $fieldNames = $this->_pdf->getTextFieldNames();
+        //PDF with text fields must return array of text field names
+        $this->assertEquals(array('Field1', 'Field2'), $fieldNames);
+    }
+
+    public function testGetTextFieldNamesNoFieldsEmptyArray()
+    {
+        $pdf = new Zend_Pdf();
+        $fieldNames = $pdf->getTextFieldNames();
+        //PDF with no text fields must return empty array
+        $this->assertEquals(array(), $fieldNames);
+    }
+
+    public function testSetTextField()
+    {
+        try {
+            $this->_pdf->setTextField('Field1', 'Value1');
+            $this->assertTrue(TRUE);    //in case of --strict
+        } catch (\Exception $e) {
+            $this->fail('Failed to set an existing text field');
+        }
+    }
+
+    public function testSetTextFieldNonExistent()
+    {
+        //Setting a non-existent field shouls throw an exception
+        try {
+            $this->_pdf->setTextField('FieldNotExists', 'Value1');
+            $this->fail('Expected exception when trying to set a non-existent text field');
+        } catch (Zend_Pdf_Exception $e) {
+            $this->assertEquals("Field 'FieldNotExists' does not exist or is not a textfield", $e->getMessage());
+        }
+    }
+
+    public function testSetTextFieldProperties()
+    {
+        try {
+            $this->_pdf->setTextFieldProperties(
+                    'Field1', Zend_Pdf::PDF_FORM_FIELD_READONLY
+            );
+            $this->_pdf->setTextFieldProperties(
+                    'Field1', Zend_Pdf::PDF_FORM_FIELD_REQUIRED
+            );
+            $this->_pdf->setTextFieldProperties(
+                    'Field1', Zend_Pdf::PDF_FORM_FIELD_NOEXPORT
+            );
+            $this->_pdf->setTextFieldProperties(
+                    'Field1', Zend_Pdf::PDF_FORM_FIELD_READONLY
+                    | Zend_Pdf::PDF_FORM_FIELD_REQUIRED
+                    | Zend_Pdf::PDF_FORM_FIELD_NOEXPORT
+            );
+            $this->assertTrue(TRUE);    //in case of --strict
+        } catch (\Exception $e) {
+            $this->fail('Failed to set property of an existing text field');
+        }
+    }
+
+    public function testSetTextFieldPropertiesNonExistent()
+    {
+        //Setting property of a non-existent field shouls throw an exception
+        try {
+            $this->_pdf->setTextFieldProperties('FieldNotExists', Zend_Pdf::PDF_FORM_FIELD_REQUIRED);
+            $this->fail('Expected exception when trying to set property of a non-existent text field');
+        } catch (Zend_Pdf_Exception $e) {
+            $this->assertEquals("Field 'FieldNotExists' does not exist or is not a textfield", $e->getMessage());
+        }
+    }
+
+    public function testMarkTextFieldAsReadOnly()
+    {
+        try {
+            $this->_pdf->markTextFieldAsReadOnly('Field1');
+            $this->_pdf->markTextFieldAsReadOnly('Field2');
+            $this->assertTrue(TRUE);    //in case of --strict
+        } catch (\Exception $e) {
+            $this->fail('Failed to set an existing text field as read-only');
+        }
+    }
+
+    public function testMarkTextFieldAsReadOnlyNonExistent()
+    {
+        //Setting property of a non-existent field shouls throw an exception
+        try {
+            $this->_pdf->markTextFieldAsReadOnly('FieldNotExists');
+            $this->fail('Expected exception when trying to set a non-existent text field as read-only');
+        } catch (Zend_Pdf_Exception $e) {
+            $this->assertEquals("Field 'FieldNotExists' does not exist or is not a textfield", $e->getMessage());
+        }
+    }
 
     public function testGetJavasriptNull()
     {
@@ -155,5 +257,4 @@ class Zend_PdfTest extends PHPUnit_Framework_TestCase
 
         $this->assertNull($pdf->getJavaScript());
     }
-
 }