You are here

public function FractionEntityTestCase::testEntityIntegration in Fraction 7

File

./fraction.test, line 164
Fraction tests

Class

FractionEntityTestCase

Code

public function testEntityIntegration() {

  // Initial data.
  $fraction_first = fraction_from_decimal('20.15');
  $fraction_second = fraction_from_decimal('14.28');

  // Create an entity.
  $entity = entity_create('fraction_entity_test', array(
    'title' => t('Title of Fraction Entity Test'),
    'type' => 'fraction_entity_test_bundle',
  ));
  $entity_wrapped = entity_metadata_wrapper('fraction_entity_test', $entity);
  $entity_wrapped->fraction_field = array(
    'numerator' => $fraction_first
      ->getNumerator(),
    'denominator' => $fraction_first
      ->getDenominator(),
  );
  $entity_wrapped
    ->save();

  // Check the value of fraction field.
  $entities = entity_load('fraction_entity_test', array(
    $entity_wrapped->id
      ->value(),
  ));
  $entity_created = reset($entities);
  $entity_created_wrapped = entity_metadata_wrapper('fraction_entity_test', $entity_created);
  $fraction_first_created = fraction()
    ->setNumerator($entity_created_wrapped->fraction_field->numerator
    ->value())
    ->setDenominator($entity_created_wrapped->fraction_field->denominator
    ->value());
  $this
    ->assertEqual($fraction_first
    ->toDecimal(), $fraction_first_created
    ->toDecimal(), t('Expected value !expected equals real !real', array(
    '!expected' => $fraction_first
      ->toDecimal(),
    '!real' => $fraction_first_created
      ->toDecimal(),
  )));

  // Update the value of fraction field.
  $entity_wrapped->fraction_field = array(
    'numerator' => $fraction_second
      ->getNumerator(),
    'denominator' => $fraction_second
      ->getDenominator(),
  );
  $entity_wrapped
    ->save();

  // Check the value of fraction field.
  $entities = entity_load('fraction_entity_test', array(
    $entity_wrapped->id
      ->value(),
  ));
  $entity_updated = reset($entities);
  $entity_updated_wrapped = entity_metadata_wrapper('fraction_entity_test', $entity_updated);
  $fraction_second_updated = fraction()
    ->setNumerator($entity_updated_wrapped->fraction_field->numerator
    ->value())
    ->setDenominator($entity_updated_wrapped->fraction_field->denominator
    ->value());
  $this
    ->assertEqual($fraction_second
    ->toDecimal(), $fraction_second_updated
    ->toDecimal(), t('Expected value !expected equals real !real', array(
    '!expected' => $fraction_second
      ->toDecimal(),
    '!real' => $fraction_second_updated
      ->toDecimal(),
  )));
}