OrderEventTest.php in Commerce Google Tag Manager 8.2        
                          
                  
                        
  
  
  
  
File
  tests/src/Kernel/EventSubscriber/OrderEventTest.php
  
    View source  
  <?php
namespace Drupal\Tests\commerce_google_tag_manager\Kernel\EventSubscriber;
use Drupal\Tests\commerce_google_tag_manager\Kernel\CommerceKernelTestBase;
use Drupal\commerce_google_tag_manager\EventSubscriber\CommerceEventsSubscriber;
use Drupal\commerce_price\Price;
use Drupal\profile\Entity\Profile;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Drupal\Tests\commerce_google_tag_manager\Traits\DeprecationSuppressionTrait;
class OrderEventTest extends CommerceKernelTestBase {
  use DeprecationSuppressionTrait;
  
  public static $modules = [
    'entity_reference_revisions',
    'profile',
  ];
  
  protected $order;
  
  private $eventDispatcher;
  
  protected function setUp() : void {
    parent::setUp();
    $this
      ->installEntitySchema('profile');
    $user = $this
      ->createUser([
      'mail' => $this
        ->randomString() . '@example.com',
    ]);
    $variation = ProductVariation::create([
      'type' => 'default',
      'sku' => strtolower($this
        ->randomMachineName()),
      'title' => $this
        ->randomString(),
      'price' => new Price('1.00', 'USD'),
      'status' => TRUE,
    ]);
    $product = Product::create([
      'type' => 'default',
      'title' => $this
        ->randomString(),
    ]);
    $product
      ->save();
    $product
      ->addVariation($variation)
      ->save();
    $profile = Profile::create([
      'type' => 'customer',
    ]);
    $profile
      ->save();
    $profile = $this
      ->reloadEntity($profile);
    
    $order_item_storage = $this->container
      ->get('entity_type.manager')
      ->getStorage('commerce_order_item');
    $order_item = $order_item_storage
      ->createFromPurchasableEntity($variation);
    $order_item
      ->save();
    $order = Order::create([
      'type' => 'default',
      'state' => 'draft',
      'mail' => $user
        ->getEmail(),
      'uid' => $user
        ->id(),
      'ip_address' => '127.0.0.1',
      'order_number' => '6',
      'billing_profile' => $profile,
      'store_id' => $this->store
        ->id(),
      'order_items' => [
        $order_item,
      ],
    ]);
    $order
      ->save();
    $this->order = $this
      ->reloadEntity($order);
    
    $this->eventDispatcher = $this->container
      ->get('event_dispatcher');
    $this->eventDispatcher
      ->removeSubscriber($this->container
      ->get('commerce_google_tag_manager.commerce_events_subscriber'));
  }
  
  public function testTrackPurchase() {
    
    $subscriber = $this
      ->getMockBuilder(CommerceEventsSubscriber::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'trackPurchase',
    ])
      ->getMock();
    
    $subscriber
      ->expects($this
      ->once())
      ->method('trackPurchase')
      ->with($this
      ->isInstanceOf(WorkflowTransitionEvent::class));
    
    $this->eventDispatcher
      ->addSubscriber($subscriber);
    
    $transition = $this->order
      ->getState()
      ->getTransitions();
    $this->order
      ->getState()
      ->applyTransition($transition['place']);
    $this->order
      ->save();
  }
}