You are here

public function TaxTypePluginTest::testCommitTransaction in Drupal Commerce Connector for AvaTax 8

Tests that a transaction is committed when an order is placed.

File

tests/src/Kernel/TaxTypePluginTest.php, line 252

Class

TaxTypePluginTest
Tests the tax type plugin.

Namespace

Drupal\Tests\commerce_avatax\Kernel

Code

public function testCommitTransaction() {
  $this
    ->mockResponse([
    new Response(200, [], Json::encode([
      'lines' => [
        [
          'lineNumber' => 1,
          'tax' => 5.25,
        ],
      ],
    ])),
    new Response(200, [], Json::encode([
      'lines' => [
        [
          'lineNumber' => 1,
          'tax' => 5.25,
        ],
      ],
    ])),
  ], [
    function (callable $handler) {
      return function (RequestInterface $request, array $options) use ($handler) {
        $count = \Drupal::state()
          ->get('avatax_request_count', 0);
        $count++;
        \Drupal::state()
          ->set('avatax_request_count', $count);
        if ($count == 2) {
          $body = $request
            ->getBody()
            ->getContents();
          $body = Json::decode($body);
          \Drupal::state()
            ->set('avatax_commit_request_body', $body);
        }
        return $handler($request, $options);
      };
    },
  ]);
  $this->taxType
    ->getPlugin()
    ->apply($this->order);
  $adjustments = $this->order
    ->collectAdjustments();
  $this
    ->assertCount(1, $adjustments);
  $transition = $this->order
    ->getState()
    ->getTransitions()['place'];
  $this->order
    ->getState()
    ->applyTransition($transition);
  $this->order
    ->save();
  $this
    ->assertEquals(2, $this->container
    ->get('state')
    ->get('avatax_request_count'));
  $body = $this->container
    ->get('state')
    ->get('avatax_commit_request_body');
  $this
    ->assertEquals('SalesInvoice', $body['type']);
  $this
    ->assertTrue($body['commit']);
}