You are here

class OrderItemDiscountAdjustmentTest in Commerce Migrate 3.1.x

Same name and namespace in other branches
  1. 8.2 modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustmentTest.php \Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1\OrderItemDiscountAdjustmentTest
  2. 3.0.x modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustmentTest.php \Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1\OrderItemDiscountAdjustmentTest

Tests the order item discount adjustment plugin.

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

@group commerce_migrate_commerce

Hierarchy

Expanded class hierarchy of OrderItemDiscountAdjustmentTest

File

modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustmentTest.php, line 22

Namespace

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

  /**
   * The mocked migration.
   *
   * @var \Drupal\migrate\Plugin\MigrationInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $migration;

  /**
   * The mocked migration.
   *
   * @var \Drupal\migrate\Plugin\MigrationPluginManager|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $migrationPluginManager;

  /**
   * The mocked migration.
   *
   * @var \Drupal\core\Entity\EntityTypeManager|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityTypeManager;

  /**
   * The mocked migration.
   *
   * @var \Drupal\commerce_price\Rounder|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $rounder;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();
    $this->migration = $this
      ->prophesize(Migration::class);
    $this->migrationPluginManager = $this
      ->prophesize(MigrationPluginManager::class);
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->rounder = $this
      ->prophesize(Rounder::class);
  }

  /**
   * Tests valid input.
   *
   * @dataProvider providerTestTransform
   */
  public function testTransform($value, $components, $shipping, $getadj, $expected) {
    $row = $this
      ->prophesize(Row::class);
    $row
      ->getSourceProperty('order_components/0/data/components')
      ->willReturn($components);
    $row
      ->getSourceProperty('shipping')
      ->willReturn($shipping);
    $row
      ->getSourceProperty('line_item_id')
      ->willReturn(2);
    $row
      ->getSourceProperty('max_line_item_id')
      ->willReturn(1);
    $mock = $this
      ->getMockBuilder('\\Drupal\\Tests\\commerce_migrate_commerce\\Unit\\Plugin\\migrate\\process\\commerce1\\TestOrderItemDiscountAdjustment')
      ->disableOriginalConstructor()
      ->setMethods([
      'getAdjustment',
    ])
      ->getMock();
    $mock
      ->expects($this
      ->any())
      ->method('getAdjustment')
      ->willReturn($getadj);
    $mock
      ->setRounder($this->rounder
      ->reveal());
    $result = $mock
      ->transform($value, $this->migrateExecutable, $row
      ->reveal(), 'foo');
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testTransform().
   */
  public function providerTestTransform() {
    return [
      'no components' => [
        [
          'name' => 'tax|sample_michigan_sales_tax',
          'price' => [
            'amount' => 288.0,
            'currency_code' => 'USD',
            'data' => [],
          ],
        ],
        [],
        [
          'data' => serialize([]),
        ],
        new Price(0, 'USD'),
        [],
      ],
      'not shipping and adjustment' => [
        [
          'name' => 'tax|sample_michigan_sales_tax',
          'price' => [
            'amount' => 288.0,
            'currency_code' => 'USD',
            'data' => [],
          ],
          'included' => FALSE,
        ],
        [
          [
            'name' => 'base_price',
            'price' => [
              'amount' => 4800.0,
              'currency_code' => 'USD',
              'data' => [],
            ],
            'included' => TRUE,
          ],
          [
            'name' => 'tax|sample_michigan_sales_tax',
            'price' => [
              'amount' => 288.0,
              'currency_code' => 'USD',
              'data' => [],
            ],
            'included' => FALSE,
          ],
        ],
        [
          [
            'line_item_id' => '26',
            'data' => serialize([
              'shipping_service' => [
                'name' => 'express_shipping',
                'price_component' => 'flat_rate_express_shipping',
              ],
            ]),
          ],
        ],
        new Price(10, 'USD'),
        new Price(10, 'USD'),
      ],
      'shipping no adjustment' => [
        [
          'name' => 'tax|sample_michigan_sales_tax',
          'price' => [
            'amount' => 288.0,
            'currency_code' => 'USD',
            'data' => [],
          ],
          'included' => FALSE,
        ],
        [
          [
            'name' => 'flat_rate_express_shipping',
            'price' => [
              'amount' => 288.0,
              'currency_code' => 'USD',
              'data' => [],
            ],
            'included' => FALSE,
          ],
        ],
        [
          [
            'line_item_id' => '26',
            'data' => serialize([
              'shipping_service' => [
                'name' => 'express_shipping',
                'price_component' => 'flat_rate_express_shipping',
              ],
            ]),
          ],
        ],
        new Price(0, 'USD'),
        [],
      ],
    ];
  }

  /**
   * Tests that exceptions are thrown as needed.
   *
   * @dataProvider providerTestInvalidValue
   */
  public function testInvalidValue($value, $shipping, $components, $message) {
    $row = $this
      ->prophesize(Row::class);
    $row
      ->getSourceProperty('order_components/0/data/components')
      ->willReturn($components);
    $row
      ->getSourceProperty('shipping')
      ->willReturn($shipping);
    $row
      ->getSourceProperty('line_item_id')
      ->willReturn(2);
    $this
      ->expectException(MigrateSkipRowException::class);
    $this
      ->expectExceptionMessage($message);
    $configuration = [];
    $this->plugin = new OrderItemDiscountAdjustment($configuration, 'test', [], $this->migration
      ->reveal(), $this->migrationPluginManager
      ->reveal(), $this->entityTypeManager
      ->reveal(), $this->rounder
      ->reveal());
    $this->plugin
      ->transform($value, $this->migrateExecutable, $row
      ->reveal(), 'foo');
  }

  /**
   * Data provider for testInvalidValue().
   */
  public function providerTestInvalidValue() {
    return [
      'test 0' => [
        [
          'name' => 'taxerror|sample_michigan_sales_tax',
          'price' => [
            'amount' => 288.0,
            'currency_code' => 'USD',
            'data' => [],
          ],
          'included' => FALSE,
        ],
        [
          [
            'line_item_id' => '26',
            'data' => 'a:2:{s:16:"shipping_service";a:14:{s:4:"name";s:16:"express_shipping";s:4:"base";s:16:"express_shipping";s:13:"display_title";s:32:"Express shipping: 1 business day";s:11:"description";s:48:"An express shipping service with additional fee.";s:15:"shipping_method";s:9:"flat_rate";s:15:"rules_component";b:1;s:15:"price_component";s:26:"flat_rate_express_shipping";s:6:"weight";i:0;s:9:"callbacks";a:4:{s:4:"rate";s:37:"commerce_flat_rate_service_rate_order";s:12:"details_form";s:29:"express_shipping_details_form";s:21:"details_form_validate";s:38:"express_shipping_details_form_validate";s:19:"details_form_submit";s:36:"express_shipping_details_form_submit";}s:6:"module";s:18:"commerce_flat_rate";s:5:"title";s:16:"Express Shipping";s:9:"base_rate";a:3:{s:6:"amount";s:4:"1500";s:13:"currency_code";s:3:"USD";s:4:"data";a:0:{}}s:4:"data";a:0:{}s:10:"admin_list";b:1;}s:15:"service_details";a:0:{}}',
          ],
        ],
        [
          [
            'name' => 'taxerror|sample_michigan_sales_tax',
          ],
        ],
        "Unknown adjustment type for line item '2'",
      ],
    ];
  }

  /**
   * Tests getAdjustment method.
   *
   * @dataProvider providerTestGetAdjustment
   */
  public function testGetAdjustment($value, $last_line, $rounder_imput, $rounder_output, $split, $expected) {
    $num_product_line = 1;
    $row = $this
      ->prophesize(Row::class);
    $row
      ->getSourceProperty('line_item_id')
      ->willReturn(2);
    $row
      ->getSourceProperty('max_line_item_id')
      ->willReturn(1);
    $row
      ->getSourceProperty('num_product_line')
      ->willReturn($num_product_line);
    $this->rounder
      ->round($rounder_imput)
      ->willReturn($rounder_output);
    $mock = $this
      ->getMockBuilder('\\Drupal\\Tests\\commerce_migrate_commerce\\Unit\\Plugin\\migrate\\process\\commerce1\\TestOrderItemDiscountAdjustment')
      ->disableOriginalConstructor()
      ->setMethods([
      'split',
    ])
      ->getMock();
    $mock
      ->expects($this
      ->any())
      ->method('split')
      ->willReturn($split);
    $mock
      ->setRounder($this->rounder
      ->reveal());
    $result = $mock
      ->getAdjustment($value, $this->migrateExecutable, $row
      ->reveal());
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Data provider for testGetAdjustment().
   */
  public function providerTestGetAdjustment() {
    return [
      'not array' => [
        'not array',
        FALSE,
        NULL,
        NULL,
        NULL,
        [],
      ],
      'base price' => [
        [
          'name' => 'base_price',
        ],
        FALSE,
        NULL,
        NULL,
        NULL,
        [],
      ],
      'tax' => [
        [
          'name' => 'tax|sales_tax',
          'price' => [
            'amount' => 1000.0,
            'currency_code' => 'USD',
            'data' => [
              'tax_rate' => [
                'name' => 'A tax',
                'display_title' => 'Sales tax',
                'rate' => '0.05',
                'type' => 'sales_tax',
              ],
            ],
          ],
          'included' => FALSE,
        ],
        FALSE,
        new Price(10.0, 'USD'),
        new Price(10.0, 'USD'),
        new Price(10.0, 'USD'),
        [
          'type' => 'tax',
          'label' => 'Sales tax',
          'amount' => '10',
          'currency_code' => 'USD',
          'percentage' => '0.05',
          'source_id' => 'custom',
          'included' => FALSE,
          'locked' => TRUE,
        ],
      ],
      'discount' => [
        [
          'name' => 'discount|arbor',
          'price' => [
            'amount' => 2000,
            'currency_code' => 'USD',
            'data' => [
              'discount_component_title' => 'Arbor',
            ],
          ],
          'included' => FALSE,
        ],
        FALSE,
        new Price(20, 'USD'),
        new Price(20, 'USD'),
        new Price(20, 'USD'),
        [
          'type' => 'promotion',
          'label' => 'Arbor',
          'amount' => '20',
          'currency_code' => 'USD',
          'percentage' => NULL,
          'source_id' => 'custom',
          'included' => FALSE,
          'locked' => TRUE,
        ],
      ],
    ];
  }

  /**
   * Tests split method.
   *
   * @dataProvider providerTestSplit
   */
  public function testSplit($num_product_line, $last_line, $price, $split_rounder_input, $split_rounder_output, $expected) {
    $this->rounder
      ->round($split_rounder_input, PHP_ROUND_HALF_DOWN)
      ->willReturn($split_rounder_output);
    $configuration = [];
    $this->plugin = new TestOrderItemDiscountAdjustment($configuration, 'map', [], $this->migration
      ->reveal(), $this->migrationPluginManager
      ->reveal(), $this->entityTypeManager
      ->reveal(), $this->rounder
      ->reveal());
    $result = $this->plugin
      ->split($num_product_line, $last_line, $price);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testSplit().
   */
  public function providerTestSplit() {
    $zero_price = new Price(0, 'USD');
    $ten_price = new Price(10.0, 'USD');
    return [
      'no product line' => [
        0,
        FALSE,
        $zero_price,
        $zero_price,
        $zero_price,
        NULL,
      ],
      'one product line not last line' => [
        1,
        FALSE,
        $ten_price,
        $ten_price,
        $ten_price,
        $ten_price,
      ],
      'one product line last line' => [
        1,
        TRUE,
        $ten_price,
        $ten_price,
        $ten_price,
        $ten_price,
      ],
      'two product lines not last line' => [
        2,
        FALSE,
        $ten_price,
        new Price(5.0, 'USD'),
        new Price(5.0, 'USD'),
        new Price(5.0, 'USD'),
      ],
      'two product lines last line' => [
        2,
        TRUE,
        $ten_price,
        new Price(5.0, 'USD'),
        new Price(5.0, 'USD'),
        new Price(5.0, 'USD'),
      ],
      'three product lines not last line' => [
        3,
        FALSE,
        new Price(10.0, 'USD'),
        new Price(3.333333, 'USD'),
        new Price(3.33, 'USD'),
        new Price(3.33, 'USD'),
      ],
      'three product lines last line' => [
        3,
        TRUE,
        new Price(10.0, 'USD'),
        new Price(3.333333, 'USD'),
        new Price(3.33, 'USD'),
        new Price(3.34, 'USD'),
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateProcessTestCase::$migrateExecutable protected property
MigrateProcessTestCase::$plugin protected property
MigrateProcessTestCase::$row protected property
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.
OrderItemDiscountAdjustmentTest::$entityTypeManager protected property The mocked migration.
OrderItemDiscountAdjustmentTest::$migration protected property The mocked migration.
OrderItemDiscountAdjustmentTest::$migrationPluginManager protected property The mocked migration.
OrderItemDiscountAdjustmentTest::$rounder protected property The mocked migration.
OrderItemDiscountAdjustmentTest::providerTestGetAdjustment public function Data provider for testGetAdjustment().
OrderItemDiscountAdjustmentTest::providerTestInvalidValue public function Data provider for testInvalidValue().
OrderItemDiscountAdjustmentTest::providerTestSplit public function Data provider for testSplit().
OrderItemDiscountAdjustmentTest::providerTestTransform public function Data provider for testTransform().
OrderItemDiscountAdjustmentTest::setUp public function Overrides MigrateProcessTestCase::setUp
OrderItemDiscountAdjustmentTest::testGetAdjustment public function Tests getAdjustment method.
OrderItemDiscountAdjustmentTest::testInvalidValue public function Tests that exceptions are thrown as needed.
OrderItemDiscountAdjustmentTest::testSplit public function Tests split method.
OrderItemDiscountAdjustmentTest::testTransform public function Tests valid input.
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