View source
<?php
namespace Drupal\Tests\commerce_shipping\Kernel\EventSubscriber;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_price\Price;
use Drupal\commerce_promotion\Entity\Promotion;
use Drupal\commerce_shipping\Entity\Shipment;
use Drupal\commerce_shipping\Entity\ShippingMethodInterface;
use Drupal\commerce_shipping\Event\ShippingRatesEvent;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\commerce_shipping\ShippingRate;
use Drupal\commerce_shipping\ShippingService;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
use Drupal\physical\Weight;
use Drupal\Tests\commerce_shipping\Kernel\ShippingKernelTestBase;
class PromotionSubscriberTest extends ShippingKernelTestBase implements ServiceModifierInterface {
protected $user;
protected $order;
protected $shipment;
public static $modules = [
'commerce_promotion',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_promotion');
$user = $this
->createUser([
'mail' => strtolower($this
->randomString()) . '@example.com',
]);
$this->user = $this
->reloadEntity($user);
$this->order = Order::create([
'type' => 'default',
'state' => 'draft',
'mail' => $this->user
->getEmail(),
'uid' => $this->user
->id(),
'store_id' => $this->store
->id(),
]);
$this->order
->save();
$this->shipment = Shipment::create([
'type' => 'default',
'title' => 'Shipment',
'items' => [
new ShipmentItem([
'order_item_id' => 10,
'title' => 'T-shirt (red, large)',
'quantity' => 1,
'weight' => new Weight('10', 'kg'),
'declared_value' => new Price('15.00', 'USD'),
]),
],
'order_id' => $this->order
->id(),
'amount' => new Price("57.88", "USD"),
]);
$this->shipment
->save();
}
public function testSubscriberNoShippingPromotions() {
$subscriber = $this->container
->get('commerce_shipping.promotion_subscriber');
$rates = [
new ShippingRate([
'shipping_method_id' => 'test',
'service' => new ShippingService('test', 'Test'),
'amount' => Price::fromArray([
'currency_code' => 'USD',
'number' => 100,
]),
]),
];
$method = $this
->createMock(ShippingMethodInterface::class);
$event = new ShippingRatesEvent($rates, $method, $this->shipment);
$promotion = Promotion::create([
'name' => 'Promotion 1',
'order_types' => [
'default',
],
'stores' => [
$this->store
->id(),
],
'status' => TRUE,
'offer' => [
'target_plugin_id' => 'order_item_percentage_off',
'target_plugin_configuration' => [
'percentage' => '0.5',
],
],
]);
$promotion
->save();
$subscriber
->onCalculate($event);
$this
->assertEqual(count($event
->getRates()), 1);
}
public function alter(ContainerBuilder $container) {
$container
->getDefinition('plugin.manager.commerce_promotion_offer')
->setClass(TestNoShippingOfferManager::class);
}
}