This space is archived

For current information please use the current ExamSys documentation

Writing new data generators

Our behat tests should use data generators to create data that is assumed by tests directly in the database.

All data generators:

  • should be located in a sub-directory of testing/datagenerator/generators
  • must extend the \testing\datagenerator\generator class
  • the file name must be the lowercase classname
  • the file must declasre itself part of the testing\datagenerator namespace
Template data generator (example.php)
 <?php
// This file is part of Rogō
//
// Rogō is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Rogō is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rogō.  If not, see <http://www.gnu.org/licenses/>.

namespace testing\datagenerator;
 
/**
 * Generates Rogo users.
 *
 * @author Your Name <youremail@example.com>
 * @copyright Copyright message
 * @package testing
 * @subpackage datagenerator
 */
class example extends generator {
  /**
   * To be usable in the main behat datageneration step the methods
   * must accept a single array or stdClass parameter only.
   *
   * @param array|stdClass $parameters
   * @return array Contains the values that were inserted into the database.
   * @throws data_error 
   */
  public function create_example($parameters) {
    // Your code goes here
  }
}

Using data generators in behat

The main exists step

Given the following "([^"]*)" exist:

If the data generator can be used by passing a single set of data in an array or object then the it can be used in the main behat exists step.

To make a data generator usable in this step you must add a new entry to the \testing\behat\steps\database\datageneration::$datagenerator_map array. It has the following form:

'myexample' => array('example', 'core', 'create_example'),

The array key is the name that will be used to call the data generator in the step. The values of the sub array have the following meanings

  • The first value must be the name of the class that should be used as the data generator
  • The second value is the sub-directory that the data generator is located in
  • The third value must be the name of the method that should be called

Using the data generator will now look like:

Given the following "myexample" exist:

Details about the required and optional fields for the data generator should be added to Writing automatic functional tests for Rogo

In a custom step

Data generators should be loaded using the \testing\datagenerator\loader::get()

To load the user data generator you would use:

\testing\datagenerator\loader::get('users', 'core')