EventStorageServiceTest.php in Commerce Google Tag Manager 8
File
tests/src/Unit/EventStorageServiceTest.php
View source
<?php
namespace Drupal\Tests\commerce_google_tag_manager\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\commerce_google_tag_manager\EventStorageService;
use Drupal\Tests\commerce_google_tag_manager\Traits\InvokeMethodTrait;
use Drupal\commerce_google_tag_manager\EventTrackerService;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\TempStore\PrivateTempStore;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\commerce_google_tag_manager\Event\EnhancedEcommerceEvents;
use Drupal\commerce_google_tag_manager\Event\AlterEventDataEvent;
class EventStorageServiceTest extends UnitTestCase {
use InvokeMethodTrait;
private $eventStorage;
private $eventDispatcher;
private $tempStore;
private $alterEventDataEvent;
protected $event;
protected function setUp() {
parent::setUp();
$this->tempStore = $this
->getMockBuilder(PrivateTempStoreFactory::class)
->disableOriginalConstructor()
->getMock();
$this->eventDispatcher = $this
->getMockBuilder(EventDispatcherInterface::class)
->disableOriginalConstructor()
->getMock();
$this->eventStorage = new EventStorageService($this->tempStore, $this->eventDispatcher);
$this->event = [
'event' => EventTrackerService::EVENT_PRODUCT_DETAIL_VIEWS,
'ecommerce' => [
'detail' => [
'actionField' => [
'list' => '',
],
'products' => [
0 => [
'name' => 'Lorem Ipsum',
'id' => '1',
'price' => '11.99',
'variant' => 'Lorem Ipsum',
],
],
],
],
];
$this->alterEventDataEvent = new AlterEventDataEvent($this->event);
}
public function testHash() {
$result = $this
->invokeMethod($this->eventStorage, 'hash', [
$this->event,
]);
$this
->assertEquals('0e05cdf318b5832a7caf62ad11d386f4', $result);
}
public function testAddEventDispatchAlter() {
$stored_events = $this
->getMockBuilder(PrivateTempStore::class)
->disableOriginalConstructor()
->getMock();
$stored_events
->expects($this
->once())
->method('get')
->with('events')
->willReturn([]);
$this->tempStore
->expects($this
->once())
->method('get')
->with('commerce_google_tag_manager')
->willReturn($stored_events);
$this->eventDispatcher
->expects($this
->once())
->method('dispatch')
->with(EnhancedEcommerceEvents::ALTER_EVENT_DATA, $this->alterEventDataEvent);
$this->eventStorage = new EventStorageService($this->tempStore, $this->eventDispatcher);
$this->eventStorage
->addEvent($this->event);
}
public function testAddEventShouldDispatchAlterOnlyOnce() {
$stored_events = $this
->getMockBuilder(PrivateTempStore::class)
->disableOriginalConstructor()
->getMock();
$stored_events
->expects($this
->once())
->method('get')
->with('events')
->willReturn([
'0e05cdf318b5832a7caf62ad11d386f4' => $this->event,
]);
$this->tempStore
->expects($this
->once())
->method('get')
->with('commerce_google_tag_manager')
->willReturn($stored_events);
$this->eventDispatcher
->expects($this
->never())
->method('dispatch');
$this->eventStorage = new EventStorageService($this->tempStore, $this->eventDispatcher);
$this->eventStorage
->addEvent($this->event);
}
}