SlideShare ist ein Scribd-Unternehmen logo
1 von 51
Downloaden Sie, um offline zu lesen
Il testing con Zend
Framework
Enrico Zimuel
Senior Consultant & Architect
Zend Technologies




                                © All rights reserved. Zend Technologies, Inc.
Sommario
●   Introduzione allo Unit Testing in PHP
●   Le funzionalità di base di test con ZF
●   Alcuni scenari avanzati di testing con ZF




                  © All rights reserved. Zend Technologies, Inc.
Perchè testare il codice?




            © All rights reserved. Zend Technologies, Inc.
Semplificare la manutenzione
●   Il testing definisce le aspettative
●   Il testing descrive i comportamenti
       dell'applicazione
●   Il testing identifica i cambiamenti nel
       codice sorgente che violano
       (“rompono”) i comportamenti attesi del
       software




                  © All rights reserved. Zend Technologies, Inc.
Quantificare la qualità del codice
●   Copertura del codice (code coverage)
    effettuata dagli strumenti di testing
●   I metodi di test documentano i
    comportamenti attesi del software




                  © All rights reserved. Zend Technologies, Inc.
Benefici psicologici per gli sviluppatori




    Quando il test è ok, gli sviluppatori sono più
    confidenti e motivati!



6                      © All rights reserved. Zend Technologies, Inc.
Testing non è … ricaricare una pagina




7             © All rights reserved. Zend Technologies, Inc.
Testing non è … var_dump()




8            © All rights reserved. Zend Technologies, Inc.
Testing è … riproducibile




9              © All rights reserved. Zend Technologies, Inc.
Testing è … automatizzabile




10             © All rights reserved. Zend Technologies, Inc.
In un buon testing …
●   Si definiscono i comportamenti
●   Si forniscono esempi su scenari d'utilizzo
●   Si definiscono le aspettative




                  © All rights reserved. Zend Technologies, Inc.
PHP testing frameworks
●   PHPT
      ▶   Utilizzato da PHP, da PEAR e da alcune
            librerie indipendenti
●   SimpleTest
      ▶   Un framework di testing in stile JUnit
●   PHPUnit
      ▶   Un framework di testing in stile JUnit
      ▶   De facto lo standard di testing in PHP



                     © All rights reserved. Zend Technologies, Inc.
Le basi del testing




13               © All rights reserved. Zend Technologies, Inc.
Scrivere unit test
●   Creare una classe di test
●   Creare uno o più metodi che definiscono dei
    comportamenti
       ▶   Descrivere il comportamento in un
             linguaggio naturale
●   Scrivere codice che definisce il comportamento
       ▶   Scrivere codice utilizzando l'API
●   Scrivere le asserzioni per definire il
    comportamento atteso



                      © All rights reserved. Zend Technologies, Inc.
Creare una classe di test
●   Solitamente il nome termina per Test


      class EntryTest
       class EntryTest
          extends PHPUnit_Framework_TestCase
           extends PHPUnit_Framework_TestCase
      {{
      }}




                   © All rights reserved. Zend Technologies, Inc.
Scrivere un metodo che definisce il
comportamento
 ●   prefisso “test”

class EntryTest
 class EntryTest
    extends PHPUnit_Framework_TestCase
     extends PHPUnit_Framework_TestCase
{{
    public function testMaySetTimestampWithString()
     public function testMaySetTimestampWithString()
    {{
    }}
}}




                       © All rights reserved. Zend Technologies, Inc.
Scrivere il codice per il comportamento

class EntryTest
 class EntryTest
    extends PHPUnit_Framework_TestCase
     extends PHPUnit_Framework_TestCase
{{
    public function testMaySetTimestampWithString()
     public function testMaySetTimestampWithString()
    {{
        $string = 'Fri, 7 May 2010 09:26:03 -0700';
         $string = 'Fri, 7 May 2010 09:26:03 -0700';
        $ts
         $ts     = strtotime($string);
                  = strtotime($string);
        $this->entry->setTimestamp($string);
         $this->entry->setTimestamp($string);
        $setValue = $this->entry->getTimestamp();
         $setValue = $this->entry->getTimestamp();
    }}
}}




                   © All rights reserved. Zend Technologies, Inc.
Scrivere asserzioni per un
comportamento atteso

class EntryTest
 class EntryTest
    extends PHPUnit_Framework_TestCase
     extends PHPUnit_Framework_TestCase
{{
    public function testMaySetTimestampWithString()
     public function testMaySetTimestampWithString()
    {{
        $string = 'Fri, 7 May 2010 09:26:03 -0700';
         $string = 'Fri, 7 May 2010 09:26:03 -0700';
        $ts
         $ts     = strtotime($string);
                  = strtotime($string);
        $this->entry->setTimestamp($string);
         $this->entry->setTimestamp($string);
        $setValue = $this->entry->getTimestamp();
         $setValue = $this->entry->getTimestamp();
        $this->assertSame($ts, $setValue);
         $this->assertSame($ts, $setValue);
    }}
}}



                   © All rights reserved. Zend Technologies, Inc.
Eseguire il test
●   Fallimento?
      ▶   Verifica il test e le asserzioni per eventuali
            errori di battitura o casi d'uso
      ▶   Verifica la classe che si stà testando
      ▶   Eseguire le correzioni e rilanciare il test
●   Successo?
      ▶   Creare il prossimo test di comportamento
            o continuare con le modifiche sul codice
            del software


                      © All rights reserved. Zend Technologies, Inc.
Alcuni termini del testing




20               © All rights reserved. Zend Technologies, Inc.
Test scaffolding
●   Essere sicuri che l'ambiente di testing sia
    libero da pre-requisiti
●   Inizializzare le dipendenze necessarie per
    eseguire il test
●   Di solito l'inizializzazione dell'ambiente di
    test avviene nel metodo setUp()




                   © All rights reserved. Zend Technologies, Inc.
Test doubles
●   Stubs
    Sostituire un oggetto con un altro per
    continuare il test
●   Mock Objects
    Sostituire un oggetto con un altro
    forzandone le aspettative (restituendo
    valori prestabiliti per i metodi)




                  © All rights reserved. Zend Technologies, Inc.
Alcune tipologie di test
●   Testing condizionali
    Testing solo al verificarsi di alcune
    condizioni d'ambiente
●   Testing funzionali e d'integrazione
    Testing del sistema per verificare i
    comportamenti attesi; testing delle unità
    e delle loro interazioni




                   © All rights reserved. Zend Technologies, Inc.
Testing semi-funzionale
     in Zend Framework




24              © All rights reserved. Zend Technologies, Inc.
Fasi principali
●   Setup dell'ambiente phpUnit
●   Creare uno scenario di test (TestCase)
    basato su un Controller
    (ControllerTestCase)
●   Bootstrap dell'applicazione
●   Creazione di una richiesta e dispatch
●   Eseguire asserzioni sulle risposte



                  © All rights reserved. Zend Technologies, Inc.
L'ambiente PHPUnit
●   Struttura delle directory


              tests
               tests
              |-- application
               |-- application
              || `-- controllers
                   `-- controllers
              |-- Bootstrap.php
               |-- Bootstrap.php
              |-- library
               |-- library
              || `-- Custom
                   `-- Custom
              `-- phpunit.xml
               `-- phpunit.xml
              4 directories, 2 files
               4 directories, 2 files



                  © All rights reserved. Zend Technologies, Inc.
L'ambiente PHPUnit (2)
●    phpunit.xml
    <phpunit bootstrap="./Bootstrap.php">
     <phpunit bootstrap="./Bootstrap.php">
        <testsuite name="Test Suite">
         <testsuite name="Test Suite">
            <directory>./</directory>
             <directory>./</directory>
        </testsuite>
         </testsuite>
        <filter>
         <filter>
            <whitelist>
             <whitelist>
                <directory
                 <directory
                   suffix=".php">../library/</directory>
                    suffix=".php">../library/</directory>
                <directory
                 <directory
                   suffix=".php">../application/</directory>
                    suffix=".php">../application/</directory>
                <exclude>
                 <exclude>
                     <directory
                      <directory
                       suffix=".phtml">../application/</directory>
                        suffix=".phtml">../application/</directory>
                </exclude>
                 </exclude>
            </whitelist>
             </whitelist>
        </filter>
         </filter>
    </phpunit>
     </phpunit>

                          © All rights reserved. Zend Technologies, Inc.
L'ambiente PHPUnit (3)
●   Bootstrap.php
     $rootPath == realpath(dirname(__DIR__));
      $rootPath     realpath(dirname(__DIR__));
     if (!defined('APPLICATION_PATH')) {{
      if (!defined('APPLICATION_PATH'))
          define('APPLICATION_PATH',
           define('APPLICATION_PATH',
               $rootPath .. '/application');
                $rootPath    '/application');
     }}
     if (!defined('APPLICATION_ENV')) {{
      if (!defined('APPLICATION_ENV'))
          define('APPLICATION_ENV', 'testing');
           define('APPLICATION_ENV', 'testing');
     }}
     set_include_path(implode(PATH_SEPARATOR, array(
      set_include_path(implode(PATH_SEPARATOR, array(
          '.',
           '.',
          $rootPath .. '/library',
           $rootPath    '/library',
          get_include_path(),
           get_include_path(),
     )));
      )));
     require_once 'Zend/Loader/Autoloader.php';
      require_once 'Zend/Loader/Autoloader.php';
     $loader == Zend_Loader_Autoloader::getInstance();
      $loader     Zend_Loader_Autoloader::getInstance();
     $loader->registerNamespace('Custom_');
      $loader->registerNamespace('Custom_');

                      © All rights reserved. Zend Technologies, Inc.
Creare una classe di test
●   Estendere la Zend_Test_PHPUnit_ControllerTestCase


    class ExampleControllerTest
     class ExampleControllerTest
        extends Zend_Test_PHPUnit_ControllerTestCase
         extends Zend_Test_PHPUnit_ControllerTestCase
    {{
    }}




                     © All rights reserved. Zend Technologies, Inc.
Bootstrap dell'applicazione
●   Creare un'istanza di Zend_Application e
    referenziarla nel setUp()
    class ExampleControllerTest
     class ExampleControllerTest
        extends Zend_Test_PHPUnit_ControllerTestCase
         extends Zend_Test_PHPUnit_ControllerTestCase
    {{
        public function setUp()
         public function setUp()
        {{
            $this->bootstrap = new Zend_Application(
             $this->bootstrap = new Zend_Application(
                APPLICATION_ENV,
                 APPLICATION_ENV,
                APPLICATION_PATH
                 APPLICATION_PATH
                . '/configs/application.ini'
                 . '/configs/application.ini'
            );
             );
            parent::setUp();
             parent::setUp();
        }}
    }}

                     © All rights reserved. Zend Technologies, Inc.
Creazione e dispatch di una richiesta
●   Metodo semplice: dispatch di una “url”
class ExampleControllerTest
 class ExampleControllerTest
    extends Zend_Test_PHPUnit_ControllerTestCase
     extends Zend_Test_PHPUnit_ControllerTestCase
{{
    // ...
     // ...
    public function testStaticPageHasGoodStructure()
     public function testStaticPageHasGoodStructure()
    {{
        $this->dispatch('/example/page');
         $this->dispatch('/example/page');
        // ...
         // ...
    }}
}}




                   © All rights reserved. Zend Technologies, Inc.
Creazione e dispatch di una richiesta (2)
●   Avanzato: personalizzare l'oggetto della
    richiesta prima di eseguire il dispatch
    class ExampleControllerTest
     class ExampleControllerTest
        extends Zend_Test_PHPUnit_ControllerTestCase
         extends Zend_Test_PHPUnit_ControllerTestCase
    {{
        // ...
         // ...
        public function testXhrRequestReturnsJson()
         public function testXhrRequestReturnsJson()
        {{
            $this->getRequest()
             $this->getRequest()
                 ->setHeader('X-Requested-With',
                  ->setHeader('X-Requested-With',
                             'XMLHttpRequest')
                              'XMLHttpRequest')
                 ->setQuery('format', 'json');
                  ->setQuery('format', 'json');
            $this->dispatch('/example/xhr-endpoint');
             $this->dispatch('/example/xhr-endpoint');
            // ...
             // ...
        }}
    }}
                     © All rights reserved. Zend Technologies, Inc.
Creare asserzioni
●   Tipiche asserzioni:
       ▶   Verifica della struttura della risposta e dei
             markup
             Utilizzando selettori CSS o query XPath
       ▶   Verificare il codice della risposta HTTP o
             l'header di pagina
       ▶   Verificare artefatti sulla richiesta e sulla
             risposta




                       © All rights reserved. Zend Technologies, Inc.
Asserzioni con selettori CSS
●   assertQuery($path, $message = '')
●   assertQueryContentContains(
      $path, $match, $message = '')
●   assertQueryContentRegex(
      $path, $pattern, $message = '')
●   assertQueryCount($path, $count, $message = '')
●   assertQueryCountMin($path, $count, $message = '')
●   assertQueryCountMax($path, $count, $message = '')
●   ognuna ha una variante "Not"



                      © All rights reserved. Zend Technologies, Inc.
Asserzioni con selettori XPath
●   assertXpath($path, $message = '')
●   assertXpathContentContains(
      $path, $match, $message = '')
●   assertXpathContentRegex(
      $path, $pattern, $message = '')
●   assertXpathCount($path, $count, $message = '')
●   assertXpathCountMin($path, $count, $message = '')
●   assertXpathCountMax($path, $count, $message = '')
●   ognuna ha una variante "Not"



                      © All rights reserved. Zend Technologies, Inc.
Asserzioni su Redirect
●   assertRedirect($message = '')
●   assertRedirectTo($url, $message = '')
●   assertRedirectRegex($pattern, $message = '')
●   ognuna ha una variante "Not"




                    © All rights reserved. Zend Technologies, Inc.
Asserzioni sulle risposte
●   assertResponseCode($code, $message = '')
●   assertHeader($header, $message = '')
●   assertHeaderContains($header, $match,
    $message = '')
●   assertHeaderRegex($header, $pattern,
    $message = '')
●   ognuna ha una variante "Not"




                   © All rights reserved. Zend Technologies, Inc.
Asserzioni sulle richieste
●   assertModule($module, $message = '')
●   assertController($controller, $message = '')
●   assertAction($action, $message = '')
●   assertRoute($route, $message = '')
●   ognuna ha una variante "Not"




                    © All rights reserved. Zend Technologies, Inc.
Esempi di asserzioni


public function testSomeStaticPageHasGoodStructure()
 public function testSomeStaticPageHasGoodStructure()
{{
    $this->dispatch('/example/page');
     $this->dispatch('/example/page');
    $this->assertResponseCode(200);
     $this->assertResponseCode(200);
    $this->assertQuery('div#content p');
     $this->assertQuery('div#content p');
    $this->assertQueryCount('div#sidebar ul li', 3);
     $this->assertQueryCount('div#sidebar ul li', 3);
}}




                   © All rights reserved. Zend Technologies, Inc.
Esempi di asserzioni (2)


   public function testXhrRequestReturnsJson()
    public function testXhrRequestReturnsJson()
   {{
       // ...
        // ...
       $this->assertNotRedirect();
        $this->assertNotRedirect();
       $this->assertHeaderContains(
        $this->assertHeaderContains(
           'Content-Type', 'application/json');
            'Content-Type', 'application/json');
   }}




                  © All rights reserved. Zend Technologies, Inc.
Alcuni casi avanzati di testing




41               © All rights reserved. Zend Technologies, Inc.
Testing di modelli e risorse
●   Problema:
    Queste classi non vengono caricate con il
    sistema di autoloading
●   Soluzione:
    Utilizzare il bootstrap di Zend_Application
    per caricare manualmente le risorse
    durante il setUp()




                  © All rights reserved. Zend Technologies, Inc.
Esempio
 class Blog_Model_EntryTest
  class Blog_Model_EntryTest
     extends PHPUnit_Framework_TestCase
      extends PHPUnit_Framework_TestCase
 {{
     public function setUp()
      public function setUp()
     {{
         $this->bootstrap = new Zend_Application(
          $this->bootstrap = new Zend_Application(
             APPLICATION_ENV,
              APPLICATION_ENV,
             APPLICATION_PATH
              APPLICATION_PATH
             . '/configs/application.ini'
              . '/configs/application.ini'
         );
          );
         $this->bootstrap->bootstrap('modules');
          $this->bootstrap->bootstrap('modules');
           $this->model = new Blog_Model_Entry();
            $this->model = new Blog_Model_Entry();
      }}
 }}

                    © All rights reserved. Zend Technologies, Inc.
Testing con autenticazione
●   Problema:
    Alcune azioni possono richiedere
    un'autenticazione utente, come emularla
    in fase di testing?
●   Soluzione:
    Eseguire un'autenticazione manuale
    utilizzando Zend_Auth prima di eseguire il
    dispatch()



                  © All rights reserved. Zend Technologies, Inc.
Esempio

 class ExampleControllerTest
  class ExampleControllerTest
     extends Zend_Test_PHPUnit_ControllerTestCase
      extends Zend_Test_PHPUnit_ControllerTestCase
 {{
     // ...
      // ...
     public function loginUser($user)
      public function loginUser($user)
     {{
         $params = array('user' => $user);
          $params = array('user' => $user);
         $adapter = new Custom_Auth_TestAdapter(
          $adapter = new Custom_Auth_TestAdapter(
             $params);
              $params);
         $auth
          $auth   = Zend_Auth::getInstance();
                   = Zend_Auth::getInstance();
         $auth->authenticate($adapter);
          $auth->authenticate($adapter);
         $this->assertTrue($auth->hasIdentity());
          $this->assertTrue($auth->hasIdentity());
     }}
 }}


                  © All rights reserved. Zend Technologies, Inc.
Esempio (2)

 class ExampleControllerTest
  class ExampleControllerTest
     extends Zend_Test_PHPUnit_ControllerTestCase
      extends Zend_Test_PHPUnit_ControllerTestCase
 {{
     // ...
      // ...
     public function testAdminUserCanAccessAdmin()
      public function testAdminUserCanAccessAdmin()
     {{
         $this->loginUser('admin');
          $this->loginUser('admin');
         $this->dispatch('/example/admin');
          $this->dispatch('/example/admin');
         $this->assertQuery('div#content.admin');
          $this->assertQuery('div#content.admin');
     }}




                  © All rights reserved. Zend Technologies, Inc.
Testing di pagine che dipendono da
altre azioni
●   Problema:
    Alcune azioni possono dipendere
    dall'esito di altre, ad esempio una pagina
    che evidenzia I risultati di un'operazione
    di ricerca
●   Soluzione:
    Eseguire un doppio dispatch, resettando
    la richiesta tra una chiamata e l'altra



                  © All rights reserved. Zend Technologies, Inc.
Esempio
class ExampleControllerTest
 class ExampleControllerTest
    extends Zend_Test_PHPUnit_ControllerTestCase
     extends Zend_Test_PHPUnit_ControllerTestCase
{{
    // ...
     // ...
    public function testHighlightedTextAfterSearch()
     public function testHighlightedTextAfterSearch()
    {{
        $this->getRequest()->setQuery(
         $this->getRequest()->setQuery(
            'search', 'foobar');
             'search', 'foobar');
        $this->dispatch('/search');
         $this->dispatch('/search');
         $this->resetRequest();
          $this->resetRequest();
         $this->resetResponse();
          $this->resetResponse();
         $this->dispatch('/example/page');
          $this->dispatch('/example/page');
         $this->assertQueryContains(
          $this->assertQueryContains(
             'span.highlight', 'foobar');
              'span.highlight', 'foobar');
    }}
                    © All rights reserved. Zend Technologies, Inc.
Conclusioni




49                 © All rights reserved. Zend Technologies, Inc.
Eseguire sempre il test!
●   Test dei modelli, dei livelli di servizio, etc
●   Eseguire test funzionali e di accettazione
    per il workflow dell'applicazione, per la
    struttura delle pagine, etc
●   Testing = Scrivere codice migliore, più
    affidabile e di qualità




                    © All rights reserved. Zend Technologies, Inc.
Grazie!


Per maggiori informazioni:
http://www.zend.com
http://framework.zend.com




              © All rights reserved. Zend Technologies, Inc.

Weitere ähnliche Inhalte

Was ist angesagt?

Java Unit Testing - JUnit (2)
Java Unit Testing - JUnit (2)Java Unit Testing - JUnit (2)
Java Unit Testing - JUnit (2)fgianneschi
 
Lezione 4: I tool Ant e Subversion
Lezione 4: I tool Ant e SubversionLezione 4: I tool Ant e Subversion
Lezione 4: I tool Ant e SubversionAndrea Della Corte
 
Java Unit Testing - JUnit (1)
Java Unit Testing - JUnit (1)Java Unit Testing - JUnit (1)
Java Unit Testing - JUnit (1)fgianneschi
 
Baby Steps TripServiceKata
Baby Steps TripServiceKataBaby Steps TripServiceKata
Baby Steps TripServiceKataAndrea Francia
 
Closure Visto Da Vicino
Closure Visto Da VicinoClosure Visto Da Vicino
Closure Visto Da Vicinodavide ficano
 

Was ist angesagt? (8)

Java Unit Testing - JUnit (2)
Java Unit Testing - JUnit (2)Java Unit Testing - JUnit (2)
Java Unit Testing - JUnit (2)
 
Lezione 4: I tool Ant e Subversion
Lezione 4: I tool Ant e SubversionLezione 4: I tool Ant e Subversion
Lezione 4: I tool Ant e Subversion
 
Java Unit Testing - JUnit (1)
Java Unit Testing - JUnit (1)Java Unit Testing - JUnit (1)
Java Unit Testing - JUnit (1)
 
Baby Steps TripServiceKata
Baby Steps TripServiceKataBaby Steps TripServiceKata
Baby Steps TripServiceKata
 
Zend Framework 2
Zend Framework 2Zend Framework 2
Zend Framework 2
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Pro php refactoring
Pro php refactoringPro php refactoring
Pro php refactoring
 
Closure Visto Da Vicino
Closure Visto Da VicinoClosure Visto Da Vicino
Closure Visto Da Vicino
 

Andere mochten auch

Andere mochten auch (6)

Dev & Prod - PHP Applications in the Cloud
Dev & Prod - PHP Applications in the CloudDev & Prod - PHP Applications in the Cloud
Dev & Prod - PHP Applications in the Cloud
 
Zend framework: Toma el control
Zend framework: Toma el controlZend framework: Toma el control
Zend framework: Toma el control
 
How do I securely deploy Internet websites in PHP on my IBMi?
How do I securely deploy Internet websites in PHP on my IBMi?How do I securely deploy Internet websites in PHP on my IBMi?
How do I securely deploy Internet websites in PHP on my IBMi?
 
Application Deployment on IBM i
Application Deployment on IBM iApplication Deployment on IBM i
Application Deployment on IBM i
 
Code Tracing with Zend Server 5: A Flight Recorder for your PHP Applications!
Code Tracing with Zend Server 5: A Flight Recorder for your PHP Applications!Code Tracing with Zend Server 5: A Flight Recorder for your PHP Applications!
Code Tracing with Zend Server 5: A Flight Recorder for your PHP Applications!
 
Resolving problems & high availability
Resolving problems & high availabilityResolving problems & high availability
Resolving problems & high availability
 

Ähnlich wie Il testing con zend framework

Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Developmentsazilla
 
Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Developmentsazilla
 
Dominare il codice legacy
Dominare il codice legacyDominare il codice legacy
Dominare il codice legacyTommaso Torti
 
Integrare Zend Framework in Wordpress
Integrare Zend Framework in WordpressIntegrare Zend Framework in Wordpress
Integrare Zend Framework in WordpressEnrico Zimuel
 
Software Testing & Test Driven Development
Software Testing & Test Driven DevelopmentSoftware Testing & Test Driven Development
Software Testing & Test Driven DevelopmentSergio Santoro
 
Presentazione Testing automatizzato
Presentazione Testing automatizzatoPresentazione Testing automatizzato
Presentazione Testing automatizzatoangelolu
 
TDD in WordPress
TDD in WordPressTDD in WordPress
TDD in WordPresslucatume
 
Sicurezza Php (giugno 2010) Stefano Bianchini presso Ce.Se.N.A.
Sicurezza Php (giugno 2010) Stefano Bianchini presso Ce.Se.N.A.Sicurezza Php (giugno 2010) Stefano Bianchini presso Ce.Se.N.A.
Sicurezza Php (giugno 2010) Stefano Bianchini presso Ce.Se.N.A.Stefano Bianchini
 
Laboratorio Di Basi Di Dati 08 Il Web Server Apache
Laboratorio Di  Basi Di  Dati 08  Il  Web Server  ApacheLaboratorio Di  Basi Di  Dati 08  Il  Web Server  Apache
Laboratorio Di Basi Di Dati 08 Il Web Server Apacheguestbe916c
 
Netbeans e Xdebug per debugging e profiling di applicazioni PHP
Netbeans e Xdebug per debugging e profiling di applicazioni PHPNetbeans e Xdebug per debugging e profiling di applicazioni PHP
Netbeans e Xdebug per debugging e profiling di applicazioni PHPGiorgio Cefaro
 
Javaday 2006: Java 5
Javaday 2006: Java 5Javaday 2006: Java 5
Javaday 2006: Java 5Matteo Baccan
 
Simone Carletti: Zend Framework ed i Web Service
Simone Carletti: Zend Framework ed i Web ServiceSimone Carletti: Zend Framework ed i Web Service
Simone Carletti: Zend Framework ed i Web ServiceFrancesco Fullone
 
Introduzione al Test Driven Development
Introduzione al Test Driven DevelopmentIntroduzione al Test Driven Development
Introduzione al Test Driven DevelopmentEnnio Masi
 
Test double - un'introduzione (PHP)
Test double - un'introduzione (PHP)Test double - un'introduzione (PHP)
Test double - un'introduzione (PHP)Carmelantonio Zolfo
 

Ähnlich wie Il testing con zend framework (20)

Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Development
 
Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Development
 
Dominare il codice legacy
Dominare il codice legacyDominare il codice legacy
Dominare il codice legacy
 
Integrare Zend Framework in Wordpress
Integrare Zend Framework in WordpressIntegrare Zend Framework in Wordpress
Integrare Zend Framework in Wordpress
 
Software Testing & Test Driven Development
Software Testing & Test Driven DevelopmentSoftware Testing & Test Driven Development
Software Testing & Test Driven Development
 
Applicazioni native in java
Applicazioni native in javaApplicazioni native in java
Applicazioni native in java
 
Presentazione Testing automatizzato
Presentazione Testing automatizzatoPresentazione Testing automatizzato
Presentazione Testing automatizzato
 
TDD in WordPress
TDD in WordPressTDD in WordPress
TDD in WordPress
 
Sicurezza Php (giugno 2010) Stefano Bianchini presso Ce.Se.N.A.
Sicurezza Php (giugno 2010) Stefano Bianchini presso Ce.Se.N.A.Sicurezza Php (giugno 2010) Stefano Bianchini presso Ce.Se.N.A.
Sicurezza Php (giugno 2010) Stefano Bianchini presso Ce.Se.N.A.
 
Java codestyle & tipstricks
Java codestyle & tipstricksJava codestyle & tipstricks
Java codestyle & tipstricks
 
Laboratorio Di Basi Di Dati 08 Il Web Server Apache
Laboratorio Di  Basi Di  Dati 08  Il  Web Server  ApacheLaboratorio Di  Basi Di  Dati 08  Il  Web Server  Apache
Laboratorio Di Basi Di Dati 08 Il Web Server Apache
 
eZ publish - Extension
eZ publish - ExtensioneZ publish - Extension
eZ publish - Extension
 
Netbeans e Xdebug per debugging e profiling di applicazioni PHP
Netbeans e Xdebug per debugging e profiling di applicazioni PHPNetbeans e Xdebug per debugging e profiling di applicazioni PHP
Netbeans e Xdebug per debugging e profiling di applicazioni PHP
 
#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2
 
Javaday 2006: Java 5
Javaday 2006: Java 5Javaday 2006: Java 5
Javaday 2006: Java 5
 
Simone Carletti: Zend Framework ed i Web Service
Simone Carletti: Zend Framework ed i Web ServiceSimone Carletti: Zend Framework ed i Web Service
Simone Carletti: Zend Framework ed i Web Service
 
introduzione a symfony 2
introduzione a symfony 2 introduzione a symfony 2
introduzione a symfony 2
 
Introduzione al Test Driven Development
Introduzione al Test Driven DevelopmentIntroduzione al Test Driven Development
Introduzione al Test Driven Development
 
Test double - un'introduzione (PHP)
Test double - un'introduzione (PHP)Test double - un'introduzione (PHP)
Test double - un'introduzione (PHP)
 
Software Testing e TDD
Software Testing e TDDSoftware Testing e TDD
Software Testing e TDD
 

Mehr von Zend by Rogue Wave Software

Building and managing applications fast for IBM i
Building and managing applications fast for IBM iBuilding and managing applications fast for IBM i
Building and managing applications fast for IBM iZend by Rogue Wave Software
 
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018) Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018) Zend by Rogue Wave Software
 
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)Zend by Rogue Wave Software
 
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)Zend by Rogue Wave Software
 

Mehr von Zend by Rogue Wave Software (20)

Develop microservices in php
Develop microservices in phpDevelop microservices in php
Develop microservices in php
 
Speed and security for your PHP application
Speed and security for your PHP applicationSpeed and security for your PHP application
Speed and security for your PHP application
 
Building and managing applications fast for IBM i
Building and managing applications fast for IBM iBuilding and managing applications fast for IBM i
Building and managing applications fast for IBM i
 
Building web APIs in PHP with Zend Expressive
Building web APIs in PHP with Zend ExpressiveBuilding web APIs in PHP with Zend Expressive
Building web APIs in PHP with Zend Expressive
 
To PHP 7 and beyond
To PHP 7 and beyondTo PHP 7 and beyond
To PHP 7 and beyond
 
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018) Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
 
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
 
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)
 
Middleware web APIs in PHP 7.x
Middleware web APIs in PHP 7.xMiddleware web APIs in PHP 7.x
Middleware web APIs in PHP 7.x
 
Ongoing management of your PHP 7 application
Ongoing management of your PHP 7 applicationOngoing management of your PHP 7 application
Ongoing management of your PHP 7 application
 
Developing web APIs using middleware in PHP 7
Developing web APIs using middleware in PHP 7Developing web APIs using middleware in PHP 7
Developing web APIs using middleware in PHP 7
 
The Docker development template for PHP
The Docker development template for PHPThe Docker development template for PHP
The Docker development template for PHP
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
 
Unit testing for project managers
Unit testing for project managersUnit testing for project managers
Unit testing for project managers
 
The new features of PHP 7
The new features of PHP 7The new features of PHP 7
The new features of PHP 7
 
Deploying PHP apps on the cloud
Deploying PHP apps on the cloudDeploying PHP apps on the cloud
Deploying PHP apps on the cloud
 
Data is dead. Long live data!
Data is dead. Long live data! Data is dead. Long live data!
Data is dead. Long live data!
 
Optimizing performance
Optimizing performanceOptimizing performance
Optimizing performance
 
Developing apps faster
Developing apps fasterDeveloping apps faster
Developing apps faster
 
Keeping up with PHP
Keeping up with PHPKeeping up with PHP
Keeping up with PHP
 

Il testing con zend framework

  • 1. Il testing con Zend Framework Enrico Zimuel Senior Consultant & Architect Zend Technologies © All rights reserved. Zend Technologies, Inc.
  • 2. Sommario ● Introduzione allo Unit Testing in PHP ● Le funzionalità di base di test con ZF ● Alcuni scenari avanzati di testing con ZF © All rights reserved. Zend Technologies, Inc.
  • 3. Perchè testare il codice? © All rights reserved. Zend Technologies, Inc.
  • 4. Semplificare la manutenzione ● Il testing definisce le aspettative ● Il testing descrive i comportamenti dell'applicazione ● Il testing identifica i cambiamenti nel codice sorgente che violano (“rompono”) i comportamenti attesi del software © All rights reserved. Zend Technologies, Inc.
  • 5. Quantificare la qualità del codice ● Copertura del codice (code coverage) effettuata dagli strumenti di testing ● I metodi di test documentano i comportamenti attesi del software © All rights reserved. Zend Technologies, Inc.
  • 6. Benefici psicologici per gli sviluppatori Quando il test è ok, gli sviluppatori sono più confidenti e motivati! 6 © All rights reserved. Zend Technologies, Inc.
  • 7. Testing non è … ricaricare una pagina 7 © All rights reserved. Zend Technologies, Inc.
  • 8. Testing non è … var_dump() 8 © All rights reserved. Zend Technologies, Inc.
  • 9. Testing è … riproducibile 9 © All rights reserved. Zend Technologies, Inc.
  • 10. Testing è … automatizzabile 10 © All rights reserved. Zend Technologies, Inc.
  • 11. In un buon testing … ● Si definiscono i comportamenti ● Si forniscono esempi su scenari d'utilizzo ● Si definiscono le aspettative © All rights reserved. Zend Technologies, Inc.
  • 12. PHP testing frameworks ● PHPT ▶ Utilizzato da PHP, da PEAR e da alcune librerie indipendenti ● SimpleTest ▶ Un framework di testing in stile JUnit ● PHPUnit ▶ Un framework di testing in stile JUnit ▶ De facto lo standard di testing in PHP © All rights reserved. Zend Technologies, Inc.
  • 13. Le basi del testing 13 © All rights reserved. Zend Technologies, Inc.
  • 14. Scrivere unit test ● Creare una classe di test ● Creare uno o più metodi che definiscono dei comportamenti ▶ Descrivere il comportamento in un linguaggio naturale ● Scrivere codice che definisce il comportamento ▶ Scrivere codice utilizzando l'API ● Scrivere le asserzioni per definire il comportamento atteso © All rights reserved. Zend Technologies, Inc.
  • 15. Creare una classe di test ● Solitamente il nome termina per Test class EntryTest class EntryTest extends PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase {{ }} © All rights reserved. Zend Technologies, Inc.
  • 16. Scrivere un metodo che definisce il comportamento ● prefisso “test” class EntryTest class EntryTest extends PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase {{ public function testMaySetTimestampWithString() public function testMaySetTimestampWithString() {{ }} }} © All rights reserved. Zend Technologies, Inc.
  • 17. Scrivere il codice per il comportamento class EntryTest class EntryTest extends PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase {{ public function testMaySetTimestampWithString() public function testMaySetTimestampWithString() {{ $string = 'Fri, 7 May 2010 09:26:03 -0700'; $string = 'Fri, 7 May 2010 09:26:03 -0700'; $ts $ts = strtotime($string); = strtotime($string); $this->entry->setTimestamp($string); $this->entry->setTimestamp($string); $setValue = $this->entry->getTimestamp(); $setValue = $this->entry->getTimestamp(); }} }} © All rights reserved. Zend Technologies, Inc.
  • 18. Scrivere asserzioni per un comportamento atteso class EntryTest class EntryTest extends PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase {{ public function testMaySetTimestampWithString() public function testMaySetTimestampWithString() {{ $string = 'Fri, 7 May 2010 09:26:03 -0700'; $string = 'Fri, 7 May 2010 09:26:03 -0700'; $ts $ts = strtotime($string); = strtotime($string); $this->entry->setTimestamp($string); $this->entry->setTimestamp($string); $setValue = $this->entry->getTimestamp(); $setValue = $this->entry->getTimestamp(); $this->assertSame($ts, $setValue); $this->assertSame($ts, $setValue); }} }} © All rights reserved. Zend Technologies, Inc.
  • 19. Eseguire il test ● Fallimento? ▶ Verifica il test e le asserzioni per eventuali errori di battitura o casi d'uso ▶ Verifica la classe che si stà testando ▶ Eseguire le correzioni e rilanciare il test ● Successo? ▶ Creare il prossimo test di comportamento o continuare con le modifiche sul codice del software © All rights reserved. Zend Technologies, Inc.
  • 20. Alcuni termini del testing 20 © All rights reserved. Zend Technologies, Inc.
  • 21. Test scaffolding ● Essere sicuri che l'ambiente di testing sia libero da pre-requisiti ● Inizializzare le dipendenze necessarie per eseguire il test ● Di solito l'inizializzazione dell'ambiente di test avviene nel metodo setUp() © All rights reserved. Zend Technologies, Inc.
  • 22. Test doubles ● Stubs Sostituire un oggetto con un altro per continuare il test ● Mock Objects Sostituire un oggetto con un altro forzandone le aspettative (restituendo valori prestabiliti per i metodi) © All rights reserved. Zend Technologies, Inc.
  • 23. Alcune tipologie di test ● Testing condizionali Testing solo al verificarsi di alcune condizioni d'ambiente ● Testing funzionali e d'integrazione Testing del sistema per verificare i comportamenti attesi; testing delle unità e delle loro interazioni © All rights reserved. Zend Technologies, Inc.
  • 24. Testing semi-funzionale in Zend Framework 24 © All rights reserved. Zend Technologies, Inc.
  • 25. Fasi principali ● Setup dell'ambiente phpUnit ● Creare uno scenario di test (TestCase) basato su un Controller (ControllerTestCase) ● Bootstrap dell'applicazione ● Creazione di una richiesta e dispatch ● Eseguire asserzioni sulle risposte © All rights reserved. Zend Technologies, Inc.
  • 26. L'ambiente PHPUnit ● Struttura delle directory tests tests |-- application |-- application || `-- controllers `-- controllers |-- Bootstrap.php |-- Bootstrap.php |-- library |-- library || `-- Custom `-- Custom `-- phpunit.xml `-- phpunit.xml 4 directories, 2 files 4 directories, 2 files © All rights reserved. Zend Technologies, Inc.
  • 27. L'ambiente PHPUnit (2) ● phpunit.xml <phpunit bootstrap="./Bootstrap.php"> <phpunit bootstrap="./Bootstrap.php"> <testsuite name="Test Suite"> <testsuite name="Test Suite"> <directory>./</directory> <directory>./</directory> </testsuite> </testsuite> <filter> <filter> <whitelist> <whitelist> <directory <directory suffix=".php">../library/</directory> suffix=".php">../library/</directory> <directory <directory suffix=".php">../application/</directory> suffix=".php">../application/</directory> <exclude> <exclude> <directory <directory suffix=".phtml">../application/</directory> suffix=".phtml">../application/</directory> </exclude> </exclude> </whitelist> </whitelist> </filter> </filter> </phpunit> </phpunit> © All rights reserved. Zend Technologies, Inc.
  • 28. L'ambiente PHPUnit (3) ● Bootstrap.php $rootPath == realpath(dirname(__DIR__)); $rootPath realpath(dirname(__DIR__)); if (!defined('APPLICATION_PATH')) {{ if (!defined('APPLICATION_PATH')) define('APPLICATION_PATH', define('APPLICATION_PATH', $rootPath .. '/application'); $rootPath '/application'); }} if (!defined('APPLICATION_ENV')) {{ if (!defined('APPLICATION_ENV')) define('APPLICATION_ENV', 'testing'); define('APPLICATION_ENV', 'testing'); }} set_include_path(implode(PATH_SEPARATOR, array( set_include_path(implode(PATH_SEPARATOR, array( '.', '.', $rootPath .. '/library', $rootPath '/library', get_include_path(), get_include_path(), ))); ))); require_once 'Zend/Loader/Autoloader.php'; require_once 'Zend/Loader/Autoloader.php'; $loader == Zend_Loader_Autoloader::getInstance(); $loader Zend_Loader_Autoloader::getInstance(); $loader->registerNamespace('Custom_'); $loader->registerNamespace('Custom_'); © All rights reserved. Zend Technologies, Inc.
  • 29. Creare una classe di test ● Estendere la Zend_Test_PHPUnit_ControllerTestCase class ExampleControllerTest class ExampleControllerTest extends Zend_Test_PHPUnit_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {{ }} © All rights reserved. Zend Technologies, Inc.
  • 30. Bootstrap dell'applicazione ● Creare un'istanza di Zend_Application e referenziarla nel setUp() class ExampleControllerTest class ExampleControllerTest extends Zend_Test_PHPUnit_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {{ public function setUp() public function setUp() {{ $this->bootstrap = new Zend_Application( $this->bootstrap = new Zend_Application( APPLICATION_ENV, APPLICATION_ENV, APPLICATION_PATH APPLICATION_PATH . '/configs/application.ini' . '/configs/application.ini' ); ); parent::setUp(); parent::setUp(); }} }} © All rights reserved. Zend Technologies, Inc.
  • 31. Creazione e dispatch di una richiesta ● Metodo semplice: dispatch di una “url” class ExampleControllerTest class ExampleControllerTest extends Zend_Test_PHPUnit_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {{ // ... // ... public function testStaticPageHasGoodStructure() public function testStaticPageHasGoodStructure() {{ $this->dispatch('/example/page'); $this->dispatch('/example/page'); // ... // ... }} }} © All rights reserved. Zend Technologies, Inc.
  • 32. Creazione e dispatch di una richiesta (2) ● Avanzato: personalizzare l'oggetto della richiesta prima di eseguire il dispatch class ExampleControllerTest class ExampleControllerTest extends Zend_Test_PHPUnit_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {{ // ... // ... public function testXhrRequestReturnsJson() public function testXhrRequestReturnsJson() {{ $this->getRequest() $this->getRequest() ->setHeader('X-Requested-With', ->setHeader('X-Requested-With', 'XMLHttpRequest') 'XMLHttpRequest') ->setQuery('format', 'json'); ->setQuery('format', 'json'); $this->dispatch('/example/xhr-endpoint'); $this->dispatch('/example/xhr-endpoint'); // ... // ... }} }} © All rights reserved. Zend Technologies, Inc.
  • 33. Creare asserzioni ● Tipiche asserzioni: ▶ Verifica della struttura della risposta e dei markup Utilizzando selettori CSS o query XPath ▶ Verificare il codice della risposta HTTP o l'header di pagina ▶ Verificare artefatti sulla richiesta e sulla risposta © All rights reserved. Zend Technologies, Inc.
  • 34. Asserzioni con selettori CSS ● assertQuery($path, $message = '') ● assertQueryContentContains( $path, $match, $message = '') ● assertQueryContentRegex( $path, $pattern, $message = '') ● assertQueryCount($path, $count, $message = '') ● assertQueryCountMin($path, $count, $message = '') ● assertQueryCountMax($path, $count, $message = '') ● ognuna ha una variante "Not" © All rights reserved. Zend Technologies, Inc.
  • 35. Asserzioni con selettori XPath ● assertXpath($path, $message = '') ● assertXpathContentContains( $path, $match, $message = '') ● assertXpathContentRegex( $path, $pattern, $message = '') ● assertXpathCount($path, $count, $message = '') ● assertXpathCountMin($path, $count, $message = '') ● assertXpathCountMax($path, $count, $message = '') ● ognuna ha una variante "Not" © All rights reserved. Zend Technologies, Inc.
  • 36. Asserzioni su Redirect ● assertRedirect($message = '') ● assertRedirectTo($url, $message = '') ● assertRedirectRegex($pattern, $message = '') ● ognuna ha una variante "Not" © All rights reserved. Zend Technologies, Inc.
  • 37. Asserzioni sulle risposte ● assertResponseCode($code, $message = '') ● assertHeader($header, $message = '') ● assertHeaderContains($header, $match, $message = '') ● assertHeaderRegex($header, $pattern, $message = '') ● ognuna ha una variante "Not" © All rights reserved. Zend Technologies, Inc.
  • 38. Asserzioni sulle richieste ● assertModule($module, $message = '') ● assertController($controller, $message = '') ● assertAction($action, $message = '') ● assertRoute($route, $message = '') ● ognuna ha una variante "Not" © All rights reserved. Zend Technologies, Inc.
  • 39. Esempi di asserzioni public function testSomeStaticPageHasGoodStructure() public function testSomeStaticPageHasGoodStructure() {{ $this->dispatch('/example/page'); $this->dispatch('/example/page'); $this->assertResponseCode(200); $this->assertResponseCode(200); $this->assertQuery('div#content p'); $this->assertQuery('div#content p'); $this->assertQueryCount('div#sidebar ul li', 3); $this->assertQueryCount('div#sidebar ul li', 3); }} © All rights reserved. Zend Technologies, Inc.
  • 40. Esempi di asserzioni (2) public function testXhrRequestReturnsJson() public function testXhrRequestReturnsJson() {{ // ... // ... $this->assertNotRedirect(); $this->assertNotRedirect(); $this->assertHeaderContains( $this->assertHeaderContains( 'Content-Type', 'application/json'); 'Content-Type', 'application/json'); }} © All rights reserved. Zend Technologies, Inc.
  • 41. Alcuni casi avanzati di testing 41 © All rights reserved. Zend Technologies, Inc.
  • 42. Testing di modelli e risorse ● Problema: Queste classi non vengono caricate con il sistema di autoloading ● Soluzione: Utilizzare il bootstrap di Zend_Application per caricare manualmente le risorse durante il setUp() © All rights reserved. Zend Technologies, Inc.
  • 43. Esempio class Blog_Model_EntryTest class Blog_Model_EntryTest extends PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase {{ public function setUp() public function setUp() {{ $this->bootstrap = new Zend_Application( $this->bootstrap = new Zend_Application( APPLICATION_ENV, APPLICATION_ENV, APPLICATION_PATH APPLICATION_PATH . '/configs/application.ini' . '/configs/application.ini' ); ); $this->bootstrap->bootstrap('modules'); $this->bootstrap->bootstrap('modules'); $this->model = new Blog_Model_Entry(); $this->model = new Blog_Model_Entry(); }} }} © All rights reserved. Zend Technologies, Inc.
  • 44. Testing con autenticazione ● Problema: Alcune azioni possono richiedere un'autenticazione utente, come emularla in fase di testing? ● Soluzione: Eseguire un'autenticazione manuale utilizzando Zend_Auth prima di eseguire il dispatch() © All rights reserved. Zend Technologies, Inc.
  • 45. Esempio class ExampleControllerTest class ExampleControllerTest extends Zend_Test_PHPUnit_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {{ // ... // ... public function loginUser($user) public function loginUser($user) {{ $params = array('user' => $user); $params = array('user' => $user); $adapter = new Custom_Auth_TestAdapter( $adapter = new Custom_Auth_TestAdapter( $params); $params); $auth $auth = Zend_Auth::getInstance(); = Zend_Auth::getInstance(); $auth->authenticate($adapter); $auth->authenticate($adapter); $this->assertTrue($auth->hasIdentity()); $this->assertTrue($auth->hasIdentity()); }} }} © All rights reserved. Zend Technologies, Inc.
  • 46. Esempio (2) class ExampleControllerTest class ExampleControllerTest extends Zend_Test_PHPUnit_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {{ // ... // ... public function testAdminUserCanAccessAdmin() public function testAdminUserCanAccessAdmin() {{ $this->loginUser('admin'); $this->loginUser('admin'); $this->dispatch('/example/admin'); $this->dispatch('/example/admin'); $this->assertQuery('div#content.admin'); $this->assertQuery('div#content.admin'); }} © All rights reserved. Zend Technologies, Inc.
  • 47. Testing di pagine che dipendono da altre azioni ● Problema: Alcune azioni possono dipendere dall'esito di altre, ad esempio una pagina che evidenzia I risultati di un'operazione di ricerca ● Soluzione: Eseguire un doppio dispatch, resettando la richiesta tra una chiamata e l'altra © All rights reserved. Zend Technologies, Inc.
  • 48. Esempio class ExampleControllerTest class ExampleControllerTest extends Zend_Test_PHPUnit_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {{ // ... // ... public function testHighlightedTextAfterSearch() public function testHighlightedTextAfterSearch() {{ $this->getRequest()->setQuery( $this->getRequest()->setQuery( 'search', 'foobar'); 'search', 'foobar'); $this->dispatch('/search'); $this->dispatch('/search'); $this->resetRequest(); $this->resetRequest(); $this->resetResponse(); $this->resetResponse(); $this->dispatch('/example/page'); $this->dispatch('/example/page'); $this->assertQueryContains( $this->assertQueryContains( 'span.highlight', 'foobar'); 'span.highlight', 'foobar'); }} © All rights reserved. Zend Technologies, Inc.
  • 49. Conclusioni 49 © All rights reserved. Zend Technologies, Inc.
  • 50. Eseguire sempre il test! ● Test dei modelli, dei livelli di servizio, etc ● Eseguire test funzionali e di accettazione per il workflow dell'applicazione, per la struttura delle pagine, etc ● Testing = Scrivere codice migliore, più affidabile e di qualità © All rights reserved. Zend Technologies, Inc.