View source
<?php
namespace Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1;
use Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\CommercePrice;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
class CommercePriceTest extends MigrateProcessTestCase {
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);
}
public function providerTestCommercePrice() {
$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[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;
}
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');
}
public function providerTestNotArray() {
$tests[0]['value'] = NULL;
$tests[1]['value'] = 'string';
$tests[2]['value'] = 1;
return $tests;
}
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');
}
public function providerTestInvalidValue() {
$tests[0]['value'] = [
'amount' => '234',
'currency_code' => 'NZD',
];
$tests[1]['value'] = [
'amount' => '234',
'fraction_digits' => 0,
];
$tests[2]['value'] = [
'currency_code' => 'NZD',
'fraction_digits' => 0,
];
$tests[3]['value'] = [
'amount' => '234',
'currency_code' => 'NZD',
'fraction_digits' => -1,
];
$tests[4]['value'] = [];
return $tests;
}
}