Sunteți pe pagina 1din 6

a controller should instruct other objects to do the heavy lifting.

What I should test here is that the controller instructs the right objects at the right time to do the right things. Mainly, we have the following cases our controller has to deal with: 1. The form is requested using GET: The web form must be shown. 2. The form is submitted using POST but validation failed: Show the form again with error messages. 3. The form is submitted using POST and validated successfully: A new entity has to be created based on the data given and stored in the database using the mapper. And this is how the tests could look like:
1 <?php 2 namespace ZfDeals\ControllerTest; 3 4 use ZfDeals\Controller\AdminController; 5 use Zend\Http\Request; 6 use Zend\Http\Response; 7 use Zend\Mvc\MvcEvent; 8 use Zend\Mvc\Router\RouteMatch; 9 10 class AdminControllerTest extends \PHPUnit_Framework_TestCase 11 { 12 private $controller; 13 private $request; 14 private $response; 15 private $routeMatch; 16 private $event; 17 18 public function setUp() 19 { 20 $this->controller = new AdminController(); 21 $this->request = new Request(); 22 $this->response = new Response(); 23 $this->routeMatch = new RouteMatch(array('controller' => 'admin')); 24 $this->routeMatch->setParam('action', 'add-product'); 25 $this->event = new MvcEvent(); 26 $this->event->setRouteMatch($this->routeMatch); 27 $this->controller->setEvent($this->event); 28 } 29

Developers Dairy 222 30 public function testShowFormOnGetRequest() 31 { 32 $fakeForm = new \Zend\Form\Form('fakeForm'); 33 $this->controller->setProductAddForm($fakeForm); 34 $this->request->setMethod('get'); 35 $response = $this->controller->dispatch($this->request); 36 $viewModelValues = $response->getVariables(); 37 $formReturned = $viewModelValues['form'];

38 $this->assertEquals($formReturned->getName(), $fakeForm->getName()); 39 } 40 41 public function testShowFormOnValidationError() 42 { 43 $fakeForm = $this->getMock('Zend\Form\Form', array('isValid')); 44 45 $fakeForm->expects($this->once()) 46 ->method('isValid') 47 ->will($this->returnValue(false)); 48 49 $this->controller->setProductAddForm($fakeForm); 50 $this->request->setMethod('post'); 51 $response = $this->controller->dispatch($this->request); 52 $viewModelValues = $response->getVariables(); 53 $formReturned = $viewModelValues['form']; 54 $this->assertEquals($formReturned->getName(), $fakeForm->getName()); 55 } 56 57 public function testCallMapperOnFormValidationSuccess() 58 { 59 $fakeForm = $this->getMock( 60 'Zend\Form\Form', array('isValid', 'getData') 61 ); 62 63 $fakeForm->expects($this->once()) 64 ->method('isValid') 65 ->will($this->returnValue(true)); 66 67 $fakeForm->expects($this->once()) 68 ->method('getData') 69 ->will($this->returnValue(new \stdClass())); 70 71 $fakeMapper = $this->getMock('ZfDeals\Mapper\Product',

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