You are here

public function UnitsWebTestCase::testCrud in Units of Measurement 7.2

Same name and namespace in other branches
  1. 7 units.test \UnitsWebTestCase::testCrud()

Conduct testing of CRUD operations.

To be specific, the following gets tested:

  • loading measure by its ID
  • loading measure by its machine name
  • saving measure (upon update of machine name, underlying units measure must be updated)
  • deleting measure (underlying units must be deleted)
  • loading unit by its ID
  • loading unit by its machine name
  • loading multiple units by their measure
  • saving unit
  • deleting unit

File

./units.test, line 44
Tests for Units module.

Class

UnitsWebTestCase
@file Tests for Units module.

Code

public function testCrud() {
  $measure = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => 'test_measure',
    'converter' => 'linear',
  ), 'units_measure');
  $measure
    ->save();
  $load = units_measure_load($measure->mid);
  $this
    ->assertEqualMeasure($load, $measure, 'Successfully loaded measure by its ID.');
  $load = units_measure_machine_name_load($measure->measure);
  $this
    ->assertEqualMeasure($load, $measure, 'Successfully loaded measure by its machine name.');
  $unit = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => $measure->measure,
    'machine_name' => 'test_unit',
    'symbol' => $this
      ->randomName(2),
    'factor' => 1,
  ), 'units_unit');
  $unit
    ->save();
  $measure->label = $this
    ->randomName();
  $measure->measure = 'updated_test_measure';
  $measure
    ->save();
  $load = units_measure_machine_name_load($measure->measure);
  $this
    ->assertEqualMeasure($load, $measure, 'Successfully updated measure.');
  $load = units_unit_load($unit->umid);
  $this
    ->assertEqual($load->measure, $measure->measure, 'Updating machine name of a measure, updates the measure of all underlying units.');
  units_measure_delete($measure);
  $select = db_select('units_measure');
  $select
    ->addExpression('COUNT(1)', 'count');
  $this
    ->assertEqual($select
    ->execute()
    ->fetchField(), 0, 'Successfully removed measure.');
  $select = db_select('units_unit');
  $select
    ->addExpression('COUNT(1)', 'count');
  $this
    ->assertEqual($select
    ->execute()
    ->fetchField(), 0, 'Removing measure removes all underlying units.');
  $measure = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => 'test_measure',
    'converter' => 'linear',
  ), 'units_measure');
  $measure
    ->save();
  $unit1 = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => $measure->measure,
    'machine_name' => 'test_unit1',
    'symbol' => $this
      ->randomName(2),
    'factor' => 1,
  ), 'units_unit');
  $unit1
    ->save();
  $load = units_unit_load($unit1->umid);
  $this
    ->assertEqualUnit($load, $unit1, 'Successfully loaded unit by its ID.');
  $load = units_unit_machine_name_load($unit1->machine_name);
  $this
    ->assertEqualUnit($load, $unit1, 'Successfully loaded unit by its machine name.');
  $unit2 = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => $measure->measure,
    'machine_name' => 'test_unit2',
    'symbol' => $this
      ->randomName(2),
    'factor' => 100,
  ), 'units_unit');
  $unit2
    ->save();
  $load = units_unit_by_measure_load_multiple($measure);
  $this
    ->assertEqualUnit(array_shift($load), $unit1, 'Successfully loaded units by their measure.');
  $this
    ->assertEqualUnit(array_shift($load), $unit2, 'Successfully loaded units by their measure.');
  $unit1->label = $this
    ->randomName();
  $unit1->symbol = $this
    ->randomName(3);
  $unit1->factor = 2;
  $unit1->machine_name = 'updated_test_unit1';
  $unit1
    ->save();
  $load = units_unit_load($unit1->umid);
  $this
    ->assertEqualUnit($load, $unit1, 'Successfully updated unit.');
  units_unit_delete($unit1);
  $select = db_select('units_unit');
  $select
    ->addExpression('COUNT(1)', 'count');
  $select
    ->condition('umid', $unit1->umid);
  $this
    ->assertEqual($select
    ->execute()
    ->fetchField(), 0, 'Successfully removed unit.');
}