class EventStorageServiceTest in Commerce Google Tag Manager 8
Same name in this branch
- 8 tests/src/Unit/EventStorageServiceTest.php \Drupal\Tests\commerce_google_tag_manager\Unit\EventStorageServiceTest
- 8 tests/src/Kernel/EventStorageServiceTest.php \Drupal\Tests\commerce_google_tag_manager\Kernel\EventStorageServiceTest
Same name and namespace in other branches
- 8.2 tests/src/Unit/EventStorageServiceTest.php \Drupal\Tests\commerce_google_tag_manager\Unit\EventStorageServiceTest
@coversDefaultClass \Drupal\commerce_google_tag_manager\EventStorageService
@group commerce @group commerce_google_tag_manager @group commerce_google_tag_manager_unit
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce_google_tag_manager\Unit\EventStorageServiceTest uses InvokeMethodTrait
Expanded class hierarchy of EventStorageServiceTest
File
- tests/
src/ Unit/ EventStorageServiceTest.php, line 22
Namespace
Drupal\Tests\commerce_google_tag_manager\UnitView source
class EventStorageServiceTest extends UnitTestCase {
use InvokeMethodTrait;
/**
* The Commerce GTM event storage.
*
* @var \Drupal\commerce_google_tag_manager\EventStorageService
*/
private $eventStorage;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
private $eventDispatcher;
/**
* The tempstore object.
*
* @var \Drupal\Core\TempStore\PrivateTempStore
*/
private $tempStore;
/**
* Event to alter Enhanced Ecommerce event data.
*
* @var \Drupal\commerce_google_tag_manager\Event\AlterEventDataEvent
*/
private $alterEventDataEvent;
/**
* The Google Tag Manager event structure to test with.
*
* @var array
*/
protected $event;
/**
* {@inheritdoc}
*/
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);
}
/**
* @covers ::hash
*/
public function testHash() {
$result = $this
->invokeMethod($this->eventStorage, 'hash', [
$this->event,
]);
$this
->assertEquals('0e05cdf318b5832a7caf62ad11d386f4', $result);
}
/**
* When addEvent called on a new event, we should fire an Event Alter.
*
* This should occure only when the added event has not already been added.
* This will then dispatch EnhancedEcommerceEvents::ALTER_EVENT_DATA.
*
* @covers ::addEvent
*/
public function testAddEventDispatchAlter() {
// Mock stored events, make sure the event has never been added.
$stored_events = $this
->getMockBuilder(PrivateTempStore::class)
->disableOriginalConstructor()
->getMock();
$stored_events
->expects($this
->once())
->method('get')
->with('events')
->willReturn([]);
// Re-Mock the Private Temp Store & The Symfony Event Dispatcher.
$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);
}
/**
* When addEvent called on a un-new event, we should not fire an Event Alter.
*
* This should occure only when the added event has already been added.
* This will then not dispatch EnhancedEcommerceEvents::ALTER_EVENT_DATA.
*
* @covers ::addEvent
*/
public function testAddEventShouldDispatchAlterOnlyOnce() {
// Mock stored events, make sure the event has already been added.
$stored_events = $this
->getMockBuilder(PrivateTempStore::class)
->disableOriginalConstructor()
->getMock();
$stored_events
->expects($this
->once())
->method('get')
->with('events')
->willReturn([
'0e05cdf318b5832a7caf62ad11d386f4' => $this->event,
]);
// Re-Mock the Private Temp Store & The Symfony Event Dispatcher.
$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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EventStorageServiceTest:: |
private | property | Event to alter Enhanced Ecommerce event data. | |
EventStorageServiceTest:: |
protected | property | The Google Tag Manager event structure to test with. | |
EventStorageServiceTest:: |
private | property | The event dispatcher. | |
EventStorageServiceTest:: |
private | property | The Commerce GTM event storage. | |
EventStorageServiceTest:: |
private | property | The tempstore object. | |
EventStorageServiceTest:: |
protected | function |
Overrides UnitTestCase:: |
|
EventStorageServiceTest:: |
public | function | When addEvent called on a new event, we should fire an Event Alter. | |
EventStorageServiceTest:: |
public | function | When addEvent called on a un-new event, we should not fire an Event Alter. | |
EventStorageServiceTest:: |
public | function | @covers ::hash | |
InvokeMethodTrait:: |
protected | function | Calls protected/private method of a class. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |