View source
<?php
namespace Drupal\Tests\commerce_google_tag_manager\Kernel;
use Drupal\Tests\commerce_google_tag_manager\Traits\InvokeMethodTrait;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\Product as CommerceProduct;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_google_tag_manager\Product;
class EventTrackerServiceTest extends CommerceKernelTestBase {
use InvokeMethodTrait;
public static $modules = [
'commerce_price_test',
'entity_reference_revisions',
'profile',
];
private $eventTracker;
protected $product;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('profile');
$this->eventTracker = $this->container
->get('commerce_google_tag_manager.event_tracker');
$this->product = CommerceProduct::create([
'type' => 'default',
'title' => 'Lorem Ipsum',
]);
$this->product
->save();
}
public function testBuildProductFromProductVariation() {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => $this
->randomString(10),
'status' => TRUE,
'price' => new Price('11.00', 'USD'),
]);
$variation
->save();
$this->product
->addVariation($variation)
->save();
$result = $this
->invokeMethod($this->eventTracker, 'buildProductFromProductVariation', [
$variation,
]);
$this
->assertInstanceOf(Product::class, $result);
$this
->assertEquals($result
->getPrice(), 11);
}
public function testBuildProductFromProductVariationCalculatedPrice() {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_CALCULATED_PRICE2',
'status' => TRUE,
'price' => new Price('11.00', 'USD'),
]);
$variation
->save();
$this->product
->addVariation($variation)
->save();
$result = $this
->invokeMethod($this->eventTracker, 'buildProductFromProductVariation', [
$variation,
]);
$this
->assertInstanceOf(Product::class, $result);
$this
->assertEquals($result
->getPrice(), 8);
}
public function testBuildProductFromProductVariationNoPrice() {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => $this
->randomString(10),
'status' => TRUE,
]);
$variation
->save();
$this->product
->addVariation($variation)
->save();
$this
->expectException(\TypeError::class);
$this
->expectExceptionMessage('Argument 1 passed to Drupal\\commerce_order\\PriceCalculatorResult::__construct() must be an instance of Drupal\\commerce_price\\Price, null given');
$this
->invokeMethod($this->eventTracker, 'buildProductFromProductVariation', [
$variation,
]);
}
}