You are here

public function FractionTargetTest::testPrepareValueDecimal in Fraction 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/Feeds/Target/FractionTargetTest.php \Drupal\Tests\fraction\Unit\Feeds\Target\FractionTargetTest::testPrepareValueDecimal()

@covers ::prepareValue

File

tests/src/Unit/Feeds/Target/FractionTargetTest.php, line 82

Class

FractionTargetTest
@coversDefaultClass \Drupal\fraction\Feeds\Target\FractionTarget @group feeds

Namespace

Drupal\Tests\fraction\Unit\Feeds\Target

Code

public function testPrepareValueDecimal() {
  $method = $this
    ->getMethod('Drupal\\fraction\\Feeds\\Target\\FractionTarget', 'prepareTarget')
    ->getClosure();
  $configuration = [
    'feed_type' => $this
      ->createMock('Drupal\\feeds\\FeedTypeInterface'),
    'target_definition' => $method($this
      ->getMockFieldDefinition()),
    'type' => 'decimal',
  ];
  $target = new FractionTarget($configuration, 'fraction', []);
  $method = $this
    ->getProtectedClosure($target, 'prepareValue');

  // Test a basic decimal.
  $values = [
    'value' => 0.5,
  ];
  $method(0, $values);
  $this
    ->assertSame($values['numerator'], '5');
  $this
    ->assertSame($values['denominator'], '10');

  // Test a negative decimal.
  $values = [
    'value' => -1,
  ];
  $method(0, $values);
  $this
    ->assertSame($values['numerator'], '-1');
  $this
    ->assertSame($values['denominator'], '1');

  // Test that a zero value results in 0/1.
  $values = [
    'value' => 0,
  ];
  $method(0, $values);
  $this
    ->assertSame($values['numerator'], '0');
  $this
    ->assertSame($values['denominator'], '1');

  // Test that an empty value results in an empty fraction.
  $values = [
    'value' => '',
  ];
  $method(0, $values);
  $this
    ->assertSame($values['numerator'], '');
  $this
    ->assertSame($values['denominator'], '');

  // Test that a string value results in an empty fraction.
  $values = [
    'value' => 'test',
  ];
  $method(0, $values);
  $this
    ->assertSame($values['numerator'], '');
  $this
    ->assertSame($values['denominator'], '');
}