You are here

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

Test voiding a transaction.

File

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

Class

TaxTypePluginTest
Tests the tax type plugin.

Namespace

Drupal\Tests\commerce_avatax\Kernel

Code

public function testVoidTransaction() {
  $this
    ->mockResponse([
    new Response(200, [], Json::encode([
      'lines' => [
        [
          'lineNumber' => 1,
          'tax' => 5.25,
        ],
      ],
    ])),
    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);
        $body = $request
          ->getBody()
          ->getContents();
        \Drupal::state()
          ->set('avatax_request_body_' . $count, Json::decode($body));
        return $handler($request, $options);
      };
    },
  ]);
  $plugin = $this->taxType
    ->getPlugin();
  $plugin
    ->apply($this->order);
  $transition = $this->order
    ->getState()
    ->getTransitions()['cancel'];
  $this->order
    ->getState()
    ->applyTransition($transition);
  $this->order
    ->save();
  $this
    ->assertEquals(2, $this->container
    ->get('state')
    ->get('avatax_request_count'));
  $request_body = $this->container
    ->get('state')
    ->get('avatax_request_body_2');
  $this
    ->assertEquals([
    'code' => 'DocVoided',
  ], $request_body);
  $this->order
    ->removeItem($this->order
    ->getItems()[0]);
  $this->order
    ->delete();
  $this
    ->assertEquals(3, $this->container
    ->get('state')
    ->get('avatax_request_count'));
  $request_body = $this->container
    ->get('state')
    ->get('avatax_request_body_3');
  $this
    ->assertEquals([
    'code' => 'DocVoided',
  ], $request_body);
}