You are here

class CommercePriceTest in Commerce Migrate 3.1.x

Same name in this branch
  1. 3.1.x modules/magento/tests/src/Unit/Plugin/migrate/process/CommercePriceTest.php \Drupal\Tests\commerce_migrate_magento\Unit\Plugin\migrate\process\CommercePriceTest
  2. 3.1.x modules/csv_example/tests/src/Unit/Plugin/migrate/process/CommercePriceTest.php \Drupal\Tests\commerce_migrate_csv_example\Unit\Plugin\migrate\process\CommercePriceTest
  3. 3.1.x modules/shopify/tests/src/Unit/Plugin/migrate/process/CommercePriceTest.php \Drupal\Tests\commerce_migrate_shopify\Unit\Plugin\migrate\process\CommercePriceTest
  4. 3.1.x modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/CommercePriceTest.php \Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1\CommercePriceTest
Same name and namespace in other branches
  1. 8.2 modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/CommercePriceTest.php \Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1\CommercePriceTest
  2. 3.0.x modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/CommercePriceTest.php \Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1\CommercePriceTest

Tests the Commerce Price plugin.

@coversDefaultClass \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\CommercePrice

@group commerce_migrate_commerce

Hierarchy

Expanded class hierarchy of CommercePriceTest

File

modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/CommercePriceTest.php, line 16

Namespace

Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1
View source
class CommercePriceTest extends MigrateProcessTestCase {

  /**
   * Tests Commerce Price plugin.
   *
   * @dataProvider providerTestCommercePrice
   */
  public function testCommercePrice($value = NULL, $expected = NULL) {
    $configuration = [];
    $this->plugin = new CommercePrice($configuration, 'map', []);
    $value = $this->plugin
      ->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
    $this
      ->assertSame($expected, $value);
  }

  /**
   * Data provider for testSubstr().
   */
  public function providerTestCommercePrice() {

    // Test the input amount > 0 with different fraction digits.
    $tests[0]['value'] = [
      'amount' => '234',
      'currency_code' => 'NZD',
      'fraction_digits' => 0,
    ];
    $tests[0]['expected'] = [
      'number' => '234',
      'currency_code' => 'NZD',
    ];
    $tests[1]['value'] = $tests[0]['value'];
    $tests[1]['value']['fraction_digits'] = 1;
    $tests[1]['expected'] = [
      'number' => '23.4',
      'currency_code' => 'NZD',
    ];

    // Tests with fractional input.
    $tests[2]['value'] = [
      'amount' => '234.56',
      'currency_code' => 'NZD',
      'fraction_digits' => 0,
    ];
    $tests[2]['expected'] = [
      'number' => '234.56',
      'currency_code' => 'NZD',
    ];
    $tests[3]['value'] = [
      'amount' => '234.56',
      'currency_code' => 'NZD',
      'fraction_digits' => 3,
    ];
    $tests[3]['expected'] = [
      'number' => '0.23456',
      'currency_code' => 'NZD',
    ];
    return $tests;
  }

  /**
   * Tests that exception is thrown when input is not an array.
   *
   * @dataProvider providerTestNotArray
   */
  public function testNotArray($value = NULL) {
    $this
      ->expectException(MigrateSkipRowException::class);
    $this
      ->expectExceptionMessage("CommercePrice input is not an array for destination 'new_value'");
    $this->plugin = new CommercePrice([], 'test_format_date', []);
    $this->plugin
      ->transform($value, $this->migrateExecutable, $this->row, 'new_value');
  }

  /**
   * Data provider for testSubstr().
   */
  public function providerTestNotArray() {

    // Test input not an array.
    $tests[0]['value'] = NULL;
    $tests[1]['value'] = 'string';
    $tests[2]['value'] = 1;
    return $tests;
  }

  /**
   * Tests that exception is thrown when input is not valid.
   *
   * @dataProvider providerTestInvalidValue
   */
  public function testInvalidValue($value = NULL) {
    $this
      ->expectException(MigrateSkipRowException::class);
    $this
      ->expectExceptionMessage("CommercePrice input array is invalid for destination 'new_value'");
    $this->plugin = new CommercePrice([], 'test_format_date', []);
    $this->plugin
      ->transform($value, $this->migrateExecutable, $this->row, 'new_value');
  }

  /**
   * Data provider for testSubstr().
   */
  public function providerTestInvalidValue() {

    // Missing fraction_digits key.
    $tests[0]['value'] = [
      'amount' => '234',
      'currency_code' => 'NZD',
    ];

    // Missing currency_code key.
    $tests[1]['value'] = [
      'amount' => '234',
      'fraction_digits' => 0,
    ];

    // Missing amount key.
    $tests[2]['value'] = [
      'currency_code' => 'NZD',
      'fraction_digits' => 0,
    ];

    // Invalid fraction_digits.
    $tests[3]['value'] = [
      'amount' => '234',
      'currency_code' => 'NZD',
      'fraction_digits' => -1,
    ];
    $tests[4]['value'] = [];
    return $tests;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommercePriceTest::providerTestCommercePrice public function Data provider for testSubstr().
CommercePriceTest::providerTestInvalidValue public function Data provider for testSubstr().
CommercePriceTest::providerTestNotArray public function Data provider for testSubstr().
CommercePriceTest::testCommercePrice public function Tests Commerce Price plugin.
CommercePriceTest::testInvalidValue public function Tests that exception is thrown when input is not valid.
CommercePriceTest::testNotArray public function Tests that exception is thrown when input is not an array.
MigrateProcessTestCase::$migrateExecutable protected property
MigrateProcessTestCase::$plugin protected property
MigrateProcessTestCase::$row protected property
MigrateProcessTestCase::setUp protected function Overrides UnitTestCase::setUp 17
MigrateTestCase::$idMap protected property The migration ID map.
MigrateTestCase::$migrationConfiguration protected property An array of migration configuration values. 7
MigrateTestCase::$migrationStatus protected property Local store for mocking setStatus()/getStatus().
MigrateTestCase::createSchemaFromRow protected function Generates a table schema from a row.
MigrateTestCase::getDatabase protected function Gets an SQLite database connection object for use in tests.
MigrateTestCase::getMigration protected function Retrieves a mocked migration.
MigrateTestCase::getValue protected function Gets the value on a row for a given key.
MigrateTestCase::queryResultTest public function Tests a query.
MigrateTestCase::retrievalAssertHelper protected function Asserts tested values during test retrieval.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function