View source
<?php
namespace Drupal\Tests\commerce_google_tag_manager\Kernel;
use Drupal\commerce_google_tag_manager\EventTrackerService;
use Drupal\Tests\commerce_google_tag_manager\Traits\DeprecationSuppressionTrait;
class EventStorageServiceTest extends CommerceKernelTestBase {
use DeprecationSuppressionTrait;
public static $modules = [
'state_machine',
'entity_reference_revisions',
'profile',
];
private $tempStore;
private $eventStorage;
protected $detailEvent;
protected $checkoutEvent;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('profile');
$this
->installSchema('system', [
'key_value_expire',
]);
$this->tempStore = $this->container
->get('tempstore.private')
->get('commerce_google_tag_manager');
$this->eventStorage = $this->container
->get('commerce_google_tag_manager.event_storage');
$this->detailEvent = [
'event' => EventTrackerService::EVENT_PRODUCT_DETAIL_VIEWS,
'ecommerce' => [
'detail' => [
'actionField' => [
'list' => '',
],
'products' => [
0 => [
'name' => 'Lorem Ipsum',
'id' => '1',
'price' => '11.99',
'variant' => 'Lorem Ipsum',
],
],
],
],
];
$this->checkoutEvent = [
'event' => EventTrackerService::EVENT_CHECKOUT,
'ecommerce' => [
'checkout' => [
'actionField' => [
'step' => 1,
],
'products' => [
0 => [
'name' => 'Lorem Ipsum',
'id' => '1',
'price' => '11.99',
'variant' => 'Lorem Ipsum',
'quantity' => 1,
],
],
],
],
];
}
public function testGetEvents() {
$this->tempStore
->set('events', [
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
]);
$result = $this->eventStorage
->getEvents();
$this
->assertSame([
0 => $this->detailEvent,
], $result);
}
public function testGetEventsEmpty() {
$this->tempStore
->set('events', NULL);
$result = $this->eventStorage
->getEvents();
$this
->assertIsArray($result);
$this
->assertEmpty($result);
}
public function testFlush() {
$this->tempStore
->set('events', [
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
]);
$this->eventStorage
->flush();
$result = $this->tempStore
->get('events');
$this
->assertNull($result);
}
public function testAddEvent() {
$this->eventStorage
->addEvent($this->detailEvent);
$result = $this->tempStore
->get('events');
$this
->assertIsArray($result);
$this
->assertSame([
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
], $result);
}
public function testAddEventFifoQueue() {
$this->eventStorage
->addEvent($this->detailEvent);
$this->eventStorage
->addEvent($this->checkoutEvent);
$events = $this->tempStore
->get('events');
$this
->assertSame([
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
'5d92a6ab1f5bd49c7ac5a065302dcb16' => $this->checkoutEvent,
], $events);
}
public function testAddEventSameSkipped() {
$this->eventStorage
->addEvent($this->detailEvent);
$this->eventStorage
->addEvent($this->detailEvent);
$events = $this->tempStore
->get('events');
$this
->assertSame([
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
], $events);
}
}