Quantcast
Viewing all articles
Browse latest Browse all 6

Generador de pruebas para modulos symfony

Si hay algo que años atrás me hizo decidirme por symfony como framework principal para trabajar, eso fue sus generadores. No voy a hablar mucho de ellos, eso da para un post muy largo seguramente encontrarán información al respecto en otros lugares.

En este articulo quería compartir con Ud. una plantilla que utilizo para poder generar pruebas para los modulos generados con el comando propel:generate-module. Son un conjunto de pruebas básicas, (nada excepcional) hechas para testear lo básico de los modulos generados que por alguna razón sólo tienen una prueba.

Si bien me gustaría que detectará sólo los campos de los formularios, que validara más cosas, etc. Esto es un buen punto de partida para empezar a escribir tus pruebas.

Se logra con un archivo .sh llamado desde la terminal y un archivo .php con la plantilla de las pruebas. Para mayor información lea el código del comando.

Saludos.

Usage example

$ ./your-sh-file.sh blog Blog BlogProject

Bash file

#!/bin/bash

# first parameter MODULE_NAME
# second parameter MODEL_CLASS
# symfony project name

cat symfony/actionTest.php | sed "s/##MODULE_NAME##/$1/" | sed "s/##MODEL_CLASS##/$2/" > $3/test/functional/frontend/$1ActionsTest.php

actionTest.php

<?php

include(dirname(__FILE__).'/../../bootstrap/functional.php');

$browser = new sfTestFunctional(new sfBrowser());

$browser->
  info('##MODEL_CLASS##s Tests')->
  info('  1.0 Index')->
  get('/##MODULE_NAME##/index')->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'index')->
  end()->

  with('response')->begin()->
    isStatusCode(200)->
    checkElement('h1:contains("##MODEL_CLASS##s List")')->
    checkElement('a:contains("New")')->
  end()
;

$browser->
  info('  1.1 New & Create')->
  click('New')->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'new')->
  end()->

  with('response')->begin()->
    isStatusCode(200)->
    checkElement('h1:contains("New ##MODEL_CLASS##")')->
    checkElement('a:contains("Back to list")')->
  end()->

  // TODO your wrong data should be here
  click('Save', array('##MODULE_NAME##' => array(
    'your-field'    => 'your-wrong-data',
    'another-field' => 'another-wrong-data',
  )))->

  with('form')->begin()->
    hasErrors(2)->
    // TODO any other check?
  end()->
 
  // TODO put your correct data here
  click('Save', array('##MODULE_NAME##' => array(
    'your-field'    => 'your-data',
    'another-field' => 'another-data',
  )))->

  with('form')->begin()->
    hasErrors(false)->
    // TODO any other check?
  end()->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'create')->
  end()->

  with('response')->isRedirected()->followRedirect()->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'edit')->
  end()->

  with('response')->begin()->
    checkElement('h1:contains("Edit ##MODEL_CLASS##")')->
    checkElement('a:contains("Back to list")')->
    checkElement('a:contains("Delete")')->
  end()
;

// XXX if you didnt use --with-show option, sorry for this :-)
$browser->
  info('  1.2 List & Show')->
  click('Back to list')->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'index')->
  end()->

  with('response')->begin()->
    isStatusCode(200)->
    checkElement('h1:contains("##MODEL_CLASS##s List")')->
    checkElement('a:contains("New")')->
    checkElement('table tbody tr td a')->
  end()->

  // XXX clicking the ##MODEL_CLASS## id
  click('1')->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'show')->
  end()->

  with('response')->begin()->
    isStatusCode(200)->
    checkElement('a:contains("Back to list")')->
    checkElement('a:contains("Edit")')->
  end()->

  // for our next set of test
  click('Edit')
;

$browser->
  info('  1.3 Edit & Update')->

  // TODO again? maybe, your wrong data should be here "if you want"
  click('Save', array('##MODULE_NAME##' => array(
    'your-field'    => 'your-wrong-data',
    'another-field' => 'another-wrong-data',
  )))->

  with('form')->begin()->
    hasErrors(2)->
    // TODO any other check?
  end()->
 
  // TODO put your correct data here
  click('Save', array('##MODULE_NAME##' => array(
    'your-field'    => 'your-data',
    'another-field' => 'another-data',
  )))->

  with('form')->begin()->
    hasErrors(false)->
    // TODO any other check?
  end()->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'update')->
  end()->

  with('response')->isRedirected()->followRedirect()->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'edit')->
  end()->

  with('response')->begin()->
    checkElement('h1:contains("Edit ##MODEL_CLASS##")')->
    checkElement('a:contains("Back to list")')->
    checkElement('a:contains("Delete")')->
  end()
;

$browser->
  info('  1.4 Delete')->
 
  click('Delete', array(), array('method' => 'delete', '_with_csrf' => true))->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'delete')->
    isMethod('delete')->
  end()->

  with('response')->begin()->
    isStatusCode(302)->
  end()->
 
  with('response')->isRedirected()->followRedirect()->

  with('request')->begin()->
    isParameter('module', '##MODULE_NAME##')->
    isParameter('action', 'index')->
  end()->

  with('response')->begin()->
    checkElement('h1:contains("##MODEL_CLASS##s List")')->
    checkElement('a:contains("New")')->
  end()
;

Viewing all articles
Browse latest Browse all 6