Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShoppingCartPerCustomerTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\TestCase\Injectable;
10 use Magento\Mtf\Fixture\FixtureFactory;
11 use Magento\Checkout\Test\Page\CheckoutCart;
14 
31 class ShoppingCartPerCustomerTest extends Injectable
32 {
33  /* tags */
34  const MVP = 'yes';
35  /* end tags */
36 
42  private $checkoutCart;
43 
49  private $fixtureFactory;
50 
56  private $cache;
57 
66  public function __inject(
67  CheckoutCart $checkoutCart,
68  FixtureFactory $fixtureFactory,
69  Cache $cache
70  ) {
71  $this->checkoutCart = $checkoutCart;
72  $this->fixtureFactory = $fixtureFactory;
73  $this->cache = $cache;
74  }
75 
84  public function test(
85  array $productsData,
86  $customerDataset,
87  array $checkoutData
88  ) {
89  //Preconditions
90  $this->cache->flush();
91  $products = $this->objectManager->create(
92  \Magento\Catalog\Test\TestStep\CreateProductsStep::class,
93  ['products' => $productsData]
94  )->run()['products'];
95 
96  $customers = [];
97  $cartFixtures = [];
98  for ($i = 0; $i < count($checkoutData); $i++) {
99  $customers[$i] = $this->fixtureFactory->createByCode('customer', ['dataset' => $customerDataset]);
100  $customers[$i]->persist();
101 
102  if (isset($checkoutData[$i])) {
103  $cartFixtures[$i] = $this->prepareShoppingCart($customers[$i], $checkoutData[$i], $products);
104  }
105  }
106 
107  //Steps
108  if (!empty($customers[0])) {
109  $this->objectManager->create(
110  \Magento\Customer\Test\TestStep\LoginCustomerOnFrontendStep::class,
111  ['customer' => $customers[0]]
112  )->run();
113  $this->checkoutCart->open();
114  $this->checkoutCart->open();
115  }
116 
117  return [
118  'customers' => $customers,
119  'cartFixtures' => $cartFixtures,
120  ];
121  }
122 
131  private function prepareShoppingCart(Customer $customer, array $checkoutData, array $products)
132  {
133  $productsInCart = [];
134  if (isset($checkoutData['items'])) {
135  foreach ($checkoutData['items'] as $index => $dataset) {
136  if (isset($products[$index])) {
137  $productFixture = $this->fixtureFactory->create(
138  get_class($products[$index]),
139  [
140  'data' => array_merge(
141  $products[$index]->getData(),
142  ['checkout_data' => ['dataset' => $dataset]]
143  )
144  ]
145  );
146  $productsInCart[] = $productFixture;
147  }
148  }
149  }
150 
151  if (!empty($productsInCart)) {
152  $this->objectManager->create(
153  \Magento\Customer\Test\TestStep\LoginCustomerOnFrontendStep::class,
154  ['customer' => $customer]
155  )->run();
156 
157  $this->objectManager->create(
158  \Magento\Checkout\Test\TestStep\AddProductsToTheCartStep::class,
159  ['products' => $productsInCart]
160  )->run();
161 
162  $cart['data'] = isset($checkoutData['totals']) ? $checkoutData['totals'] : [];
163  $cart['data']['items'] = ['products' => $productsInCart];
164  return $this->fixtureFactory->createByCode('cart', $cart);
165  }
166 
167  return null;
168  }
169 }
$customer
Definition: customers.php:11
$productsData
Definition: products.php:19
test(array $productsData, $customerDataset, array $checkoutData)
__inject(CheckoutCart $checkoutCart, FixtureFactory $fixtureFactory, Cache $cache)
$i
Definition: gallery.phtml:31
$index
Definition: list.phtml:44