-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from gridonic/init-kernel-tests
Add unit test for the EventStorageService
- Loading branch information
Showing
8 changed files
with
682 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.