You are here

public function DBTNGExampleUnitTestCase::testAPIExamples in Examples for Developers 7

Test several combinations, adding entries, updating and deleting.

File

dbtng_example/dbtng_example.test, line 109
SimpleTests for dbtng_example module.

Class

DBTNGExampleUnitTestCase
Default test case for the dbtng_example module.

Code

public function testAPIExamples() {

  // Create a new entry.
  $entry = array(
    'name' => 'James',
    'surname' => 'Doe',
    'age' => 23,
  );
  dbtng_example_entry_insert($entry);

  // Save another entry.
  $entry = array(
    'name' => 'Jane',
    'surname' => 'NotDoe',
    'age' => 19,
  );
  dbtng_example_entry_insert($entry);

  // Verify that 4 records are found in the database.
  $result = dbtng_example_entry_load();
  $this
    ->assertEqual(count($result), 4, 'Found a total of four entries in the table after creating two additional entries.');

  // Verify 2 of these records have 'Doe' as surname.
  $result = dbtng_example_entry_load(array(
    'surname' => 'Doe',
  ));
  $this
    ->assertEqual(count($result), 2, 'Found two entries in the table with surname = "Doe".');

  // Now find our not-Doe entry.
  $result = dbtng_example_entry_load(array(
    'surname' => 'NotDoe',
  ));
  $this
    ->assertEqual(count($result), 1, 'Found one entry in the table with surname "NotDoe');

  // Our NotDoe will be changed to "NowDoe".
  $entry = $result[0];
  $entry->surname = "NowDoe";
  dbtng_example_entry_update((array) $entry);
  $result = dbtng_example_entry_load(array(
    'surname' => 'NowDoe',
  ));
  $this
    ->assertEqual(count($result), 1, "Found renamed 'NowDoe' surname");

  // Read only John Doe entry.
  $result = dbtng_example_entry_load(array(
    'name' => 'John',
    'surname' => 'Doe',
  ));
  $this
    ->assertEqual(count($result), 1, 'Found one entry for John Doe.');

  // Get the entry.
  $entry = (array) end($result);

  // Change age to 45
  $entry['age'] = 45;

  // Update entry in database.
  dbtng_example_entry_update((array) $entry);

  // Find entries with age = 45
  // Read only John Doe entry.
  $result = dbtng_example_entry_load(array(
    'surname' => 'NowDoe',
  ));
  $this
    ->assertEqual(count($result), 1, 'Found one entry with surname = Nowdoe.');

  // Verify it is Jane NowDoe.
  $entry = (array) end($result);
  $this
    ->assertEqual($entry['name'], 'Jane', 'The name Jane is found in the entry');
  $this
    ->assertEqual($entry['surname'], 'NowDoe', 'The surname NowDoe is found in the entry');

  // Delete the entry.
  dbtng_example_entry_delete($entry);

  // Verify that now there are only 3 records.
  $result = dbtng_example_entry_load();
  $this
    ->assertEqual(count($result), 3, 'Found only three records, a record was deleted.');
}