You are here

class ShippingRateTest in Commerce Shipping 8.2

@coversDefaultClass \Drupal\commerce_shipping\ShippingRate @group commerce_shipping

Hierarchy

Expanded class hierarchy of ShippingRateTest

File

tests/src/Unit/ShippingRateTest.php, line 15

Namespace

Drupal\Tests\commerce_shipping\Unit
View source
class ShippingRateTest extends UnitTestCase {

  /**
   * Tests the constructor and definition checks.
   *
   * @covers ::__construct
   *
   * @dataProvider invalidDefinitionProvider
   */
  public function testInvalidDefinition($definition, $message) {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage($message);
    new ShippingRate($definition);
  }

  /**
   * Invalid constructor definitions.
   *
   * @return array
   *   The definitions.
   */
  public function invalidDefinitionProvider() {
    return [
      [
        [],
        'Missing required property shipping_method_id',
      ],
      [
        [
          'shipping_method_id' => 'standard',
        ],
        'Missing required property service',
      ],
      [
        [
          'shipping_method_id' => 'standard',
          'service' => new ShippingService('test', 'Test'),
        ],
        'Missing required property amount',
      ],
      [
        [
          'shipping_method_id' => 'standard',
          'service' => 'Test',
          'amount' => '10 USD',
        ],
        sprintf('Property "service" should be an instance of %s.', ShippingService::class),
      ],
      [
        [
          'shipping_method_id' => 'standard',
          'service' => new ShippingService('test', 'Test'),
          'amount' => '10 USD',
        ],
        sprintf('Property "amount" should be an instance of %s.', Price::class),
      ],
    ];
  }

  /**
   * @covers ::getId
   * @covers ::getShippingMethodId
   * @covers ::getService
   * @covers ::getOriginalAmount
   * @covers ::setOriginalAmount
   * @covers ::getAmount
   * @covers ::setAmount
   * @covers ::getDescription
   * @covers ::setDescription
   * @covers ::getDeliveryDate
   * @covers ::setDeliveryDate
   * @covers ::toArray
   */
  public function testMethods() {
    $first_date = new DrupalDateTime('2016-11-24', 'UTC', [
      'langcode' => 'en',
    ]);
    $second_date = new DrupalDateTime('2016-12-01', 'UTC', [
      'langcode' => 'en',
    ]);
    $definition = [
      'id' => '717c2f9',
      'shipping_method_id' => 'standard',
      'service' => new ShippingService('test', 'Test'),
      'original_amount' => new Price('15.00', 'USD'),
      'amount' => new Price('10.00', 'USD'),
      'description' => 'Delivery in 3-5 business days.',
      'delivery_date' => $first_date,
    ];
    $shipping_rate = new ShippingRate($definition);
    $this
      ->assertEquals($definition['id'], $shipping_rate
      ->getId());
    $this
      ->assertEquals($definition['shipping_method_id'], $shipping_rate
      ->getShippingMethodId());
    $this
      ->assertEquals($definition['service'], $shipping_rate
      ->getService());
    $this
      ->assertEquals($definition['original_amount'], $shipping_rate
      ->getOriginalAmount());
    $this
      ->assertEquals($definition['amount'], $shipping_rate
      ->getAmount());
    $this
      ->assertEquals($definition['description'], $shipping_rate
      ->getDescription());
    $this
      ->assertEquals($definition['delivery_date'], $shipping_rate
      ->getDeliveryDate());
    $this
      ->assertEquals($definition, $shipping_rate
      ->toArray());
    $shipping_rate
      ->setOriginalAmount(new Price('14.00', 'USD'));
    $this
      ->assertEquals(new Price('14.00', 'USD'), $shipping_rate
      ->getOriginalAmount());
    $shipping_rate
      ->setAmount(new Price('11.00', 'USD'));
    $this
      ->assertEquals(new Price('11.00', 'USD'), $shipping_rate
      ->getAmount());
    $shipping_rate
      ->setDescription('Arrives yesterday.');
    $this
      ->assertEquals('Arrives yesterday.', $shipping_rate
      ->getDescription());
    $shipping_rate
      ->setDeliveryDate($second_date);
    $this
      ->assertEquals($second_date, $shipping_rate
      ->getDeliveryDate());
  }

  /**
   * @covers ::getId
   * @covers ::getOriginalAmount
   */
  public function testDefaults() {
    $definition = [
      'shipping_method_id' => 'standard',
      'service' => new ShippingService('test', 'Test'),
      'amount' => new Price('10.00', 'USD'),
    ];
    $shipping_rate = new ShippingRate($definition);
    $this
      ->assertEquals('standard--test', $shipping_rate
      ->getId());
    $this
      ->assertEquals($definition['amount'], $shipping_rate
      ->getOriginalAmount());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
ShippingRateTest::invalidDefinitionProvider public function Invalid constructor definitions.
ShippingRateTest::testDefaults public function @covers ::getId @covers ::getOriginalAmount
ShippingRateTest::testInvalidDefinition public function Tests the constructor and definition checks.
ShippingRateTest::testMethods public function @covers ::getId @covers ::getShippingMethodId @covers ::getService @covers ::getOriginalAmount @covers ::setOriginalAmount @covers ::getAmount @covers ::setAmount @covers ::getDescription @covers ::setDescription @covers ::getDeliveryDate @covers…
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::setUp protected function 340