Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions
CartTest Class Reference
Inheritance diagram for CartTest:
AbstractController

Public Member Functions

 addAddProductDataProvider ()
 
 testMessageAtAddToCartWithRedirect ()
 
 testMessageAtAddToCartWithoutRedirect ()
 
 testReorderItems (bool $loggedIn, string $request)
 
 reorderItemsDataProvider ()
 
- Public Member Functions inherited from AbstractController
 getRequest ()
 
 getResponse ()
 
 assert404NotFound ()
 
 assertHeaderPcre ($headerName, $valueRegex)
 
 assertRedirect (\PHPUnit\Framework\Constraint\Constraint $urlConstraint=null)
 

Protected Member Functions

 setUp ()
 
 tearDown ()
 
- Protected Member Functions inherited from AbstractController
 _getBootstrap ()
 
 setUp ()
 
 tearDown ()
 
 assertPostConditions ()
 
 getMessages ( $messageType=null, $messageManagerClass=\Magento\Framework\Message\Manager::class)
 

Additional Inherited Members

- Protected Attributes inherited from AbstractController
 $_runCode = ''
 
 $_runScope = 'store'
 
 $_runOptions = []
 
 $_request
 
 $_response
 
 $_objectManager
 
 $_assertSessionErrors = false
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects) @magentoDbIsolation enabled

Definition at line 31 of file CartTest.php.

Member Function Documentation

◆ addAddProductDataProvider()

addAddProductDataProvider ( )

Data provider for testAddToCartSimpleProduct

Definition at line 347 of file CartTest.php.

348  {
349  return [
350  'frontend' => ['frontend', 'expected_price' => 10],
351  'adminhtml' => ['adminhtml', 'expected_price' => 1]
352  ];
353  }

◆ reorderItemsDataProvider()

reorderItemsDataProvider ( )

Data provider for testReorderItems.

Returns
array

Definition at line 472 of file CartTest.php.

473  {
474  return [
475  [
476  'logged_in' => false,
477  'request_type' => Request::METHOD_POST,
478  ],
479  [
480  'logged_in' => false,
481  'request_type' => Request::METHOD_GET,
482  ],
483  [
484  'logged_in' => true,
485  'request_type' => Request::METHOD_POST,
486  ],
487  [
488  'logged_in' => true,
489  'request_type' => Request::METHOD_GET,
490  ],
491  ];
492  }

◆ setUp()

setUp ( )
protected

Definition at line 39 of file CartTest.php.

40  {
41  parent::setUp();
42  $this->checkoutSession = $this->_objectManager->get(CheckoutSession::class);
43  $this->_objectManager->addSharedInstance($this->checkoutSession, CheckoutSession::class);
44  }

◆ tearDown()

tearDown ( )
protected

Definition at line 49 of file CartTest.php.

50  {
51  $this->_objectManager->removeSharedInstance(CheckoutSession::class);
52  parent::tearDown();
53  }

◆ testMessageAtAddToCartWithoutRedirect()

testMessageAtAddToCartWithoutRedirect ( )

Test for \Magento\Checkout\Controller\Cart\Add::execute() with simple product and deactivated redirect to cart

@magentoDataFixture Magento/Catalog/_files/products.php @magentoConfigFixture current_store checkout/cart/redirect_to_cart 0 @magentoAppIsolation enabled

Definition at line 398 of file CartTest.php.

399  {
400  $formKey = $this->_objectManager->get(FormKey::class);
401  $postData = [
402  'qty' => '1',
403  'product' => '1',
404  'custom_price' => 1,
405  'form_key' => $formKey->getFormKey(),
406  'isAjax' => 1
407  ];
409  $this->getRequest()->setPostValue($postData);
410  $this->getRequest()->setMethod('POST');
411 
412  $this->dispatch('checkout/cart/add');
413 
414  $this->assertFalse($this->getResponse()->isRedirect());
415  $this->assertEquals('[]', $this->getResponse()->getBody());
416 
417  $this->assertSessionMessages(
418  $this->contains(
419  "\n" . 'You added Simple Product to your ' .
420  '<a href="http://localhost/index.php/checkout/cart/">shopping cart</a>.'
421  ),
422  \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
423  );
424  }

◆ testMessageAtAddToCartWithRedirect()

testMessageAtAddToCartWithRedirect ( )

Test for \Magento\Checkout\Controller\Cart\Add::execute() with simple product and activated redirect to cart

@magentoDataFixture Magento/Catalog/_files/products.php @magentoConfigFixture current_store checkout/cart/redirect_to_cart 1 @magentoAppIsolation enabled

Definition at line 362 of file CartTest.php.

363  {
364  $formKey = $this->_objectManager->get(FormKey::class);
365  $postData = [
366  'qty' => '1',
367  'product' => '1',
368  'custom_price' => 1,
369  'form_key' => $formKey->getFormKey(),
370  'isAjax' => 1
371  ];
373  $this->getRequest()->setPostValue($postData);
374  $this->getRequest()->setMethod('POST');
375 
376  $this->dispatch('checkout/cart/add');
377 
378  $this->assertEquals(
379  '{"backUrl":"http:\/\/localhost\/index.php\/checkout\/cart\/"}',
380  $this->getResponse()->getBody()
381  );
382 
383  $this->assertSessionMessages(
384  $this->contains(
385  'You added Simple Product to your shopping cart.'
386  ),
387  \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
388  );
389  }

◆ testReorderItems()

testReorderItems ( bool  $loggedIn,
string  $request 
)

@covers \Magento\Checkout\Controller\Cart\Addgroup::execute()

Test customer can add items to cart only if they belong to him.

Parameters
bool$loggedIn
string$request@magentoAppArea frontend @magentoDataFixture Magento/Checkout/_files/order_items.php @dataProvider reorderItemsDataProvider
Returns
void

Definition at line 438 of file CartTest.php.

439  {
440  // Make sure test starts without logged in customer.
441  $customerSession = $this->_objectManager->get(CustomerSession::class);
442  $customerSession->logout();
443 
444  $checkoutSession = Bootstrap::getObjectManager()->get(Session::class);
445  $expected = [];
446  if ($loggedIn && $request == Request::METHOD_POST) {
447  $customer = $this->_objectManager->create(CustomerRepository::class)->get('[email protected]');
448  $customerSession->setCustomerDataObject($customer);
449  $orderCollection = $this->_objectManager->create(OrderCollection::class);
450  $orderCollection->addFieldToFilter('customer_id', $customer->getId());
451  $orderItemCollection = $this->_objectManager->create(OrderItemCollection::class);
452  $orderItemCollection->addFieldToFilter('order_id', ['in' => $orderCollection->getAllIds()]);
453  $expected = $orderItemCollection->getColumnValues('product_id');
454  }
455  $this->prepareRequest($request);
456  $this->dispatch('checkout/cart/addGroup');
457 
458  $this->assertEquals(
459  $expected,
460  $checkoutSession->getQuote()->getItemsCollection()->getColumnValues('product_id')
461  );
462 
463  // Make sure test doesn't left logged in customer after execution.
464  $customerSession->logout();
465  }
$customer
Definition: customers.php:11

The documentation for this class was generated from the following file: