View source
<?php
namespace Drupal\Tests\commerce_migrate\Kernel\Plugin\migrate\process;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
use Drupal\commerce_migrate\Plugin\migrate\process\CommerceAdjustments;
use Drupal\commerce_order\Adjustment;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Row;
class CommerceAdjustmentsTest extends CommerceKernelTestBase {
public static $modules = [
'entity_reference_revisions',
'profile',
'state_machine',
'commerce_order',
'commerce_order_test',
];
protected $plugin;
protected $row;
protected $migrateExecutable;
public function setUp() : void {
parent::setUp();
$this->row = new Row([]);
$configuration = [];
$this->plugin = new CommerceAdjustments($configuration, 'map', []);
$this->migrateExecutable = $this
->getMockBuilder(MigrateExecutable::class)
->disableOriginalConstructor()
->getMock();
}
public function testValidCommerceAdjustments($value = NULL) {
$adjustments = $this->plugin
->transform($value, $this->migrateExecutable, $this->row, 'destination_property');
foreach ($adjustments as $adjustment) {
$this
->assertInstanceOf(Adjustment::class, $adjustment);
}
}
public function providerValidCommerceAdjustments() {
$tests = [
[
[
[
'type' => 'custom',
'title' => '10% off',
'amount' => '1.23',
'currency_code' => 'CAD',
],
],
],
[
[
[
'type' => 'custom',
'title' => '10% off',
'amount' => '1.23000',
'currency_code' => 'CAD',
],
],
],
[
[
[
'type' => 'custom',
'title' => '10% off',
'amount' => '1.23',
'currency_code' => 'CAD',
],
[
'type' => 'custom',
'title' => '$ off',
'amount' => '20.00',
'currency_code' => 'CAD',
],
],
],
[
[
[
'type' => '',
'title' => 'No type',
'amount' => '1.00',
'currency_code' => 'CAD',
],
],
],
[
[
[
'type' => 'custom',
'title' => '',
'label' => 'Empty title',
'amount' => '2.00',
'currency_code' => 'CAD',
],
],
],
[
[
[
'type' => 'custom',
'title' => 'Empty label',
'label' => '',
'amount' => '2.00',
'currency_code' => 'CAD',
],
],
],
];
return $tests;
}
public function testNoCommerceAdjustments($value = NULL, $expected = NULL) {
$new_value = $this->plugin
->transform($value, $this->migrateExecutable, $this->row, 'destination_property');
$this
->assertEquals($expected, $new_value);
}
public function providerNoCommerceAdjustment() {
$tests = [
[
'not an array',
'not an array',
],
[
[],
[],
],
];
return $tests;
}
public function testExceptionPrice($value = NULL) {
$msg = sprintf('Failed creating price for adjustment %s', var_export($value, TRUE));
$this
->expectException(MigrateSkipRowException::class);
$this
->expectExceptionMessage($msg);
$this->plugin
->transform([
$value,
], $this->migrateExecutable, $this->row, 'destination_property');
}
public function providerExceptionPrice() {
$tests = [
[
[
'type' => 'custom',
'title' => 'test',
'amount' => '1.00',
'currency_code' => '',
],
],
[
[
'type' => 'custom',
'title' => 'test',
'amount' => '2.00',
'currency_code' => 'Latinum',
],
],
[
[
'type' => 'custom',
'title' => 'test',
'amount' => '3.00',
'currency_code' => '1234',
],
],
[
[
'type' => 'custom',
'title' => 'test',
'amount' => 'string',
'currency_code' => 'CAD',
],
],
];
return $tests;
}
public function testNotSet($value = NULL, $property = NULL) {
if (is_string($property)) {
$msg = sprintf("Property '%s' is not set for adjustment '%s'", $property, var_export($value, TRUE));
}
else {
$msg = sprintf("Properties 'amount' and 'currency_code' are not set for adjustment '%s'", var_export($value, TRUE));
}
$this
->expectException(MigrateSkipRowException::class);
$this
->expectExceptionMessage($msg);
$this->plugin
->transform([
$value,
], $this->migrateExecutable, $this->row, 'destination_property');
}
public function providerNotSet() {
$tests = [
[
[
'type' => 'custom',
'title' => 'test',
'amount' => '4.00',
],
'currency_code',
],
[
[
'type' => 'custom',
'title' => 'test',
'currency_code' => 'CAD',
],
'amount',
],
[
[
'type' => 'custom',
'title' => 'test',
],
[
'amount',
'currency_code',
],
],
];
return $tests;
}
}