AlterProductPurchasedEntityEventTest.php in Commerce Google Tag Manager 8.2
File
tests/src/Kernel/AlterProductPurchasedEntityEventTest.php
View source
<?php
namespace Drupal\Tests\commerce_google_tag_manager\Kernel;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\commerce_google_tag_manager\Event\AlterProductPurchasedEntityEvent;
use Drupal\commerce_google_tag_manager\Product;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Tests\commerce_google_tag_manager\Traits\DeprecationSuppressionTrait;
class AlterProductPurchasedEntityEventTest extends CommerceKernelTestBase {
use DeprecationSuppressionTrait;
public static $modules = [
'entity_reference_revisions',
'profile',
];
protected $purchasedEntity;
protected $product;
protected $orderItem;
protected function setUp() : void {
parent::setUp();
$this->purchasedEntity = ProductVariation::create([
'type' => 'default',
'sku' => $this
->randomString(10),
'status' => TRUE,
]);
$this->purchasedEntity
->save();
$order_item_storage = $this->container
->get('entity_type.manager')
->getStorage('commerce_order_item');
$this->orderItem = $order_item_storage
->createFromPurchasableEntity($this->purchasedEntity, [
'quantity' => 1,
]);
$this->product = new Product();
$this->product
->setName($this
->randomString(16))
->setId(1)
->setVariant($this->purchasedEntity
->getTitle())
->setPrice('11.99');
}
public function testGetProduct() {
$event = new AlterProductPurchasedEntityEvent($this->product, $this->orderItem, $this->purchasedEntity);
$this
->assertInstanceOf(Product::class, $event
->getProduct());
}
public function testGetOrderItem() {
$event = new AlterProductPurchasedEntityEvent($this->product, $this->orderItem, $this->purchasedEntity);
$this
->assertInstanceOf(OrderItemInterface::class, $event
->getOrderItem());
}
public function testGetPurchasedEntity() {
$event = new AlterProductPurchasedEntityEvent($this->product, $this->orderItem, $this->purchasedEntity);
$this
->assertInstanceOf(PurchasableEntityInterface::class, $event
->getPurchasedEntity());
$event = new AlterProductPurchasedEntityEvent($this->product, $this->orderItem, NULL);
$this
->assertNull($event
->getPurchasedEntity());
}
}