Skip to content

Commit

Permalink
Merge pull request #18 from gridonic/init-kernel-tests
Browse files Browse the repository at this point in the history
Add unit test for the EventStorageService
  • Loading branch information
WengerK authored Dec 4, 2018
2 parents c13453c + 06767ec commit 3e2bc12
Show file tree
Hide file tree
Showing 8 changed files with 682 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DEVELOPPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ Maintaining code quality by adding the custom post-commit hook to yours.

```bash
cat ./scripts/hooks/post-commit >> ./.git/hooks/post-commit
```
```
2 changes: 1 addition & 1 deletion commerce_google_tag_manager.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ type: module
dependencies:
- drupal:google_tag
- drupal:commerce
- drupal:commerce_checkout
- drupal:commerce_checkout
70 changes: 70 additions & 0 deletions tests/src/Kernel/AlterProductEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Drupal\Tests\commerce_google_tag_manager\Kernel;

use Drupal\commerce_google_tag_manager\Event\AlterProductEvent;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_google_tag_manager\Product;

/**
* @coversDefaultClass \Drupal\commerce_google_tag_manager\Event\AlterProductEvent
*
* @group commerce
* @group commerce_google_tag_manager
* @group commerce_google_tag_manager_kernel
*/
class AlterProductEventTest extends CommerceKernelTestBase {
/**
* The variations to test with.
*
* @var \Drupal\commerce_product\Entity\ProductVariationInterface
*/
protected $variation;

/**
* The GTM product to test against.
*
* @var \Drupal\commerce_google_tag_manager\Product
*/
protected $product;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

// The variations to test with.
$this->variation = ProductVariation::create([
'type' => 'default',
'sku' => $this->randomString(10),
'status' => TRUE,
]);
$this->variation->save();

$this->product = new Product();
$this->product
->setName($this->randomString(16))
->setId(1)
->setVariant($this->variation->getTitle())
->setPrice('11.99');
}

/**
* @covers ::getProduct
*/
public function testGetProduct() {
$event = new AlterProductEvent($this->product, $this->variation);
$this->assertInstanceOf(Product::class, $event->getProduct());
}

/**
* @covers ::getProductVariation
*/
public function testGetProductVariation() {
$event = new AlterProductEvent($this->product, $this->variation);
$this->assertInstanceOf(ProductVariationInterface::class, $event->getProductVariation());
}

}
32 changes: 32 additions & 0 deletions tests/src/Kernel/CommerceKernelTestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Drupal\Tests\commerce_google_tag_manager\Kernel;

use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase as DrupalCommerceKernelTestBase;

/**
* Provides a base class for Commerce Google Tag Manager kernel tests.
*/
abstract class CommerceKernelTestBase extends DrupalCommerceKernelTestBase {

/**
* Modules to additionnaly enable.
*
* @var array
*/
public static $modules = [
'commerce_product',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

$this->installEntitySchema('commerce_product_variation');
$this->installEntitySchema('commerce_product');
$this->installConfig(['commerce_product']);
}

}
182 changes: 182 additions & 0 deletions tests/src/Kernel/EventStorageServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

namespace Drupal\Tests\commerce_google_tag_manager\Kernel;

use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
use Drupal\commerce_google_tag_manager\EventTrackerService;

/**
* @coversDefaultClass \Drupal\commerce_google_tag_manager\EventStorageService
*
* @group commerce
* @group commerce_google_tag_manager
* @group commerce_google_tag_manager_kernel
*/
class EventStorageServiceTest extends CommerceKernelTestBase {

/**
* {@inheritdoc}
*/
public static $modules = [
'commerce_checkout',
'commerce_google_tag_manager',
];

/**
* The tempstore object.
*
* @var \Drupal\Core\TempStore\PrivateTempStore
*/
private $tempStore;

/**
* The Commerce GTM event storage.
*
* @var \Drupal\commerce_google_tag_manager\EventStorageService
*/
private $eventStorage;

/**
* The Google Tag Manager Product Detail view event structure to test with.
*
* @var array
*/
protected $detailEvent;

/**
* The Google Tag Manager Checkout event structure to test with.
*
* @var array
*/
protected $checkoutEvent;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

$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,
],
],
],
],
];
}

/**
* @covers ::getEvents
*/
public function testGetEvents() {
$this->tempStore->set('events', [
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
]);
$result = $this->eventStorage->getEvents();
$this->assertSame([0 => $this->detailEvent], $result);
}

/**
* @covers ::getEvents
*/
public function testGetEventsEmpty() {
$this->tempStore->set('events', NULL);
$result = $this->eventStorage->getEvents();
$this->assertInternalType('array', $result);
$this->assertEmpty($result);
}

/**
* @covers ::flush
*/
public function testFlush() {
$this->tempStore->set('events', [
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
]);
$this->eventStorage->flush();
$result = $this->tempStore->get('events');
$this->assertNull($result);
}

/**
* @covers ::addEvent
*
* Asserts usage of Private Temp Store for events queue.
*/
public function testAddEvent() {
$this->eventStorage->addEvent($this->detailEvent);

$result = $this->tempStore->get('events');
$this->assertInternalType('array', $result);
$this->assertSame([
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
], $result);
}

/**
* @covers ::addEvent
*
* Asserts the events queue follows the FIFO (First in First out) pattern.
*/
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);
}

/**
* @covers ::addEvent
*
* Asserts strictly same event aren't added twice in the events queue.
*/
public function testAddEventSameSkipped() {
$this->eventStorage->addEvent($this->detailEvent);
$this->eventStorage->addEvent($this->detailEvent);
$events = $this->tempStore->get('events');
$this->assertSame([
'0e05cdf318b5832a7caf62ad11d386f4' => $this->detailEvent,
], $events);
}

}
Loading

0 comments on commit 3e2bc12

Please sign in to comment.