Sunteți pe pagina 1din 4

Unit-Tests for the web form

Before I start working on getting data out of the form and into the database, I will first add some unit tests for form ProductAdd, just to be sure, its configured correctly. I create a new directory tests in the application root and add the file phpunit.xml to configure the directories holding test cases:
1 <phpunit bootstrap="./bootstrap.php"> 2 <testsuites> 3 <testsuite name="AllTests"> 4 <directory>./ZfDealsTest/FormTest</directory> 5 </testsuite> 6 </testsuites> 7 </phpunit>

As I need to bootstrap the application with all of its services to actually run tests, I add a proper bootstrap.php file to the tests directory as well: Developers Dairy 212
1 <?php 2 use Zend\Loader\StandardAutoloader; 3 4 chdir(dirname(__DIR__)); 5 6 include 'init_autoloader.php'; 7 8 $loader = new StandardAutoloader(); 9 $loader->registerNamespace('ZfDealsTest', __DIR__ . '/ZfDealsTest'); 10 $loader->register(); 11 12 Zend\Mvc\Application::init(include 'config/application.config.php');

Listing 26.14 Its executed automatically by PHPUnit, when running tests. In bootstrap.php I configure autoloading of test classes stored in directory ZfDealsTest. The test file ProductAddTest goes into its subdirectory FormTest:
1 <?php 2 namespace ZfDealsTest\FormTest; 3 4 use ZfDeals\Form\ProductAdd; 5 6 class ProductAddTest extends \PHPUnit_Framework_TestCase 7{ 8 private $form; 9 private $data; 10 11 public function setUp() 12 { 13 $this->form = new ProductAdd();

14 $this->data = array( 15 'product' => array( 16 'id' => '', 17 'name' => '', 18 'stock' => '' 19 ) 20 ); 21 } 22 23 public function testEmptyValues() 24 {

Developers Dairy 213 25 $form = $this->form; 26 $data = $this->data; 27 28 $this->assertFalse($form->setData($data)->isValid()); 29 30 $data['product']['id'] = 1; 31 $this->assertFalse($form->setData($data)->isValid()); 32 33 $data['product']['name'] = 1; 34 $this->assertFalse($form->setData($data)->isValid()); 35 36 $data['product']['stock'] = 1; 37 $this->assertTrue($form->setData($data)->isValid()); 38 } 39 40 public function testStockElement() 41 { 42 $form = $this->form; 43 $data = $this->data; 44 $data['product']['id'] = 1; 45 $data['product']['name'] = 1; 46 47 $data['product']['stock'] = -1; 48 $this->assertFalse($form->setData($data)->isValid()); 49 50 $data['product']['stock'] = "test"; 51 $this->assertFalse($form->setData($data)->isValid()); 52 53 $data['product']['stock'] = 12.3; 54 $this->assertFalse($form->setData($data)->isValid()); Developers Dairy 205 1 <?php 2 namespace ZfDeals\Form; 3 4 use Zend\Form\Fieldset; 5 use Zend\InputFilter\InputFilterInterface; 6 use Zend\InputFilter\InputFilterProviderInterface; 7 8 class ProductFieldset extends Fieldset 9 implements InputFilterProviderInterface 10 { 11 public function __construct() 12 { 13 parent::__construct('product');

14 15 $this->add(array( 16 'name' => 'id', 17 'attributes' => array( 18 'type' => 'text', 19 ), 20 'options' => array( 21 'label' => 'Produkt-ID:', 22 ) 23 )); 24 25 26 $this->add(array( 27 'name' => 'name', 28 'attributes' => array( 29 'type' => 'text', 30 ), 31 'options' => array( 32 'label' => 'Produktbezeichnung:', 33 ) 34 )); 35 36 $this->add(array( 37 'name' => 'stock', 38 'attributes' => array( 39 'type' => 'number', 40 ), 41 'options' => array( 42 'label' => '# Bestand:'

Developers Dairy 206 43 ), 44 )); 45 } 46 47 public function getInputFilterSpecification() 48 { 49 return array( 50 'id' => array ( 51 'required' => true, 52 'filters' => array( 53 array( 54 'name' => 'StringTrim' 55 ) 56 ), 57 'validators' => array( 58 array( 59 'name' => 'NotEmpty', 60 'options' => array( 61 'message' => 62 "Bitte geben Sie die Produkt-ID an." 63 ) 64 ) 65 ) 66 ), 67 'name' => array ( 68 'required' => true,

69 'filters' => array( 70 array( 71 'name' => 'StringTrim' 72 ) 73 ), 74 'validators' => array( 75 array( 76 'name' => 'NotEmpty', 77 'options' => array( 78 'message' => 79 "Bitte geben Sie eine Produktbezeichnung an." 80 ), 81 ) 82 ) 83 ), 84 'stock' => array (

Developers Dairy 207 85 'required' => true, 86 'filters' => array( 87 array( 88 'name' => 'StringTrim' 89 ) 90 ), 91 'validators' => array( 92 array( 93 'name' => 'NotEmpty', 94 'options' => array( 95 'message' => 96 "Bitte geben Sie die Lagerbestand an." 97 ) 98 ), 99 array( 100 'name' => 'Digits', 101 'options' => array( 102 'message' => 103 "Bitte geben Sie einen ganzzahligen Wert an." 104 ) 105 ), 106 array( 107 'name' => 'GreaterThan', 108 'options' => array( 109 'min' => 0, 110 'message' => 111 "Bitte geben Sie Wert >= 0 an." 112 ) 113 ) 114 ) 115 ) 116 ); 117 } 118 }

Listing 26.9

S-ar putea să vă placă și