You are here

class EventStorageServiceTest in Commerce Google Tag Manager 8.2

Same name in this branch
  1. 8.2 tests/src/Unit/EventStorageServiceTest.php \Drupal\Tests\commerce_google_tag_manager\Unit\EventStorageServiceTest
  2. 8.2 tests/src/Kernel/EventStorageServiceTest.php \Drupal\Tests\commerce_google_tag_manager\Kernel\EventStorageServiceTest
Same name and namespace in other branches
  1. 8 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

Expanded class hierarchy of EventStorageServiceTest

File

tests/src/Unit/EventStorageServiceTest.php, line 22

Namespace

Drupal\Tests\commerce_google_tag_manager\Unit
View 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

Namesort descending Modifiers Type Description Overrides
EventStorageServiceTest::$alterEventDataEvent private property Event to alter Enhanced Ecommerce event data.
EventStorageServiceTest::$event protected property The Google Tag Manager event structure to test with.
EventStorageServiceTest::$eventDispatcher private property The event dispatcher.
EventStorageServiceTest::$eventStorage private property The Commerce GTM event storage.
EventStorageServiceTest::$tempStore private property The tempstore object.
EventStorageServiceTest::setUp protected function Overrides UnitTestCase::setUp
EventStorageServiceTest::testAddEventDispatchAlter public function When addEvent called on a new event, we should fire an Event Alter.
EventStorageServiceTest::testAddEventShouldDispatchAlterOnlyOnce public function When addEvent called on a un-new event, we should not fire an Event Alter.
EventStorageServiceTest::testHash public function @covers ::hash
InvokeMethodTrait::invokeMethod protected function Calls protected/private method of a class.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.