You are here

class FractionTargetTest in Fraction 2.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/Feeds/Target/FractionTargetTest.php \Drupal\Tests\fraction\Unit\Feeds\Target\FractionTargetTest

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

Hierarchy

  • class \Drupal\Tests\fraction\Unit\Feeds\Target\FractionTargetTest extends \Drupal\Tests\feeds\Unit\Feeds\Target\FieldTargetTestBase

Expanded class hierarchy of FractionTargetTest

File

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

Namespace

Drupal\Tests\fraction\Unit\Feeds\Target
View source
class FractionTargetTest extends FieldTargetTestBase {

  /**
   * {@inheritdoc}
   */
  protected function getTargetClass() {
    return FractionTarget::class;
  }

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

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

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

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

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

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

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

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

  /**
   * @covers ::prepareValue
   */
  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'], '');
  }

  /**
   * @covers ::buildConfigurationForm
   */
  public function testBuildConfigurationForm() {
    $method = $this
      ->getMethod('Drupal\\fraction\\Feeds\\Target\\FractionTarget', 'prepareTarget')
      ->getClosure();
    $configuration = [
      'feed_type' => $this
        ->createMock('Drupal\\feeds\\FeedTypeInterface'),
      'target_definition' => $method($this
        ->getMockFieldDefinition()),
    ];
    $target = new FractionTarget($configuration, 'fraction', []);
    $target
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $form_state = new FormState();
    $form = $target
      ->buildConfigurationForm([], $form_state);
    $this
      ->assertSame(count($form), 1);
    $this
      ->assertArrayHasKey('type', $form);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FractionTargetTest::getTargetClass protected function
FractionTargetTest::testBuildConfigurationForm public function @covers ::buildConfigurationForm
FractionTargetTest::testPrepareValueDecimal public function @covers ::prepareValue
FractionTargetTest::testPrepareValueFraction public function @covers ::prepareValue