CartBrowserTestTrait.php in Commerce Core 8.2
File
modules/cart/tests/src/Traits/CartBrowserTestTrait.php
View source
<?php
namespace Drupal\Tests\commerce_cart\Traits;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
trait CartBrowserTestTrait {
protected function postAddToCart(ProductInterface $product, array $edit = []) {
$this
->drupalGet('product/' . $product
->id());
$this
->assertSession()
->buttonExists('Add to cart');
$this
->submitForm($edit, 'Add to cart');
}
protected function assertAttributeSelected($selector, $option) {
$selected_option = $this
->getSession()
->getPage()
->find('css', 'select[name="' . $selector . '"] option[selected="selected"]')
->getText();
$this
->assertEquals($option, $selected_option);
}
protected function assertAttributeExists($selector, $option) {
$this
->assertSession()
->elementExists('xpath', '//select[@name="' . $selector . '"]//option[@value="' . $option . '"]');
}
protected function assertAttributeDoesNotExist($selector, $option) {
$this
->assertSession()
->elementNotExists('xpath', '//select[@name="' . $selector . '"]//option[@value="' . $option . '"]');
}
protected function assertOrderItemInOrder(ProductVariationInterface $variation, OrderItemInterface $order_item, $quantity = '1') {
$this
->assertEquals($order_item
->getTitle(), $variation
->getOrderItemTitle());
$this
->assertNotEmpty($order_item
->getQuantity() == $quantity, t('The product @product has been added to cart with quantity of @quantity.', [
'@product' => $order_item
->getTitle(),
'@quantity' => $order_item
->getQuantity(),
]));
}
}