You are here

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

Test that the request is correctly cached.

File

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

Class

TaxTypePluginTest
Tests the tax type plugin.

Namespace

Drupal\Tests\commerce_avatax\Kernel

Code

public function testRequestCaching() {
  $response = [
    'lines' => [
      [
        'lineNumber' => 1,
        'tax' => 5.25,
      ],
    ],
  ];
  $this
    ->mockResponse([
    new Response(200, [], Json::encode($response)),
  ], [
    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);
        return $handler($request, $options);
      };
    },
  ]);
  $plugin = $this->taxType
    ->getPlugin();
  $this
    ->assertTrue($plugin
    ->applies($this->order));
  $plugin
    ->apply($this->order);
  $cached_data = \Drupal::cache('commerce_avatax')
    ->get('transactions_create:' . $this->order
    ->id())->data;
  $this
    ->assertEquals($response, $cached_data['response']);
  $plugin
    ->apply($this->order);
  $this
    ->assertEquals(1, $this->container
    ->get('state')
    ->get('avatax_request_count'));
  $order_items = $this->order
    ->getItems();
  $order_items[0]
    ->setQuantity(2);
  $order_items[0]
    ->save();
  $plugin
    ->apply($this->order);
  $this
    ->assertEquals(2, $this->container
    ->get('state')
    ->get('avatax_request_count'));
}