objectManager = Bootstrap::getObjectManager(); $this->cartFactory = $this->objectManager->get(CartFactory::class); $this->productFactory = $this->objectManager->get(ProductInterfaceFactory::class); $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); $this->productRepository->cleanCache(); $this->executeInStoreContext = $this->objectManager->get(ExecuteInStoreContext::class); $this->checkoutSession = $this->objectManager->get(CheckoutSessionFactory::class)->create(); $this->quoteRepository = $this->objectManager->get(CartRepositoryInterface::class); } /** * @inheritdoc */ protected function tearDown(): void { if ($this->quote instanceof CartInterface) { $this->quoteRepository->delete($this->quote); } parent::tearDown(); } /** * @magentoDataFixture Magento/Checkout/_files/set_product_min_in_cart.php * @magentoAppIsolation enabled * * @return void */ public function testAddProductWithLowerQty(): void { $cart = $this->cartFactory->create(); $this->expectException(LocalizedException::class); $this->expectExceptionMessage((string)__('The fewest you may purchase is %1', 3)); $product = $this->productRepository->get('simple'); $cart->addProduct($product->getId(), ['qty' => 1]); } /** * @magentoDataFixture Magento/Checkout/_files/set_product_min_in_cart.php * * @return void */ public function testAddProductWithNoQty(): void { $cart = $this->cartFactory->create(); $product = $this->productRepository->get('simple'); $cart->addProduct($product->getId(), [])->save(); $this->quote = $cart->getQuote(); $this->assertCount(1, $cart->getItems()); $this->assertEquals($product->getId(), $this->checkoutSession->getLastAddedProductId()); } /** * @return void */ public function testAddNotExistingProduct(): void { $product = $this->productFactory->create(); $this->expectExceptionObject( new LocalizedException(__('The product wasn\'t found. Verify the product and try again.')) ); $this->cartFactory->create()->addProduct($product); } /** * @return void */ public function testAddNotExistingProductId(): void { $this->expectExceptionObject( new LocalizedException(__('The product wasn\'t found. Verify the product and try again.')) ); $this->cartFactory->create()->addProduct(989); } /** * @magentoDataFixture Magento/Catalog/_files/product_simple.php * @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php * * @return void */ public function testAddProductFromUnavailableWebsite(): void { $product = $this->productRepository->get('simple'); $this->expectExceptionObject( new LocalizedException(__('The product wasn\'t found. Verify the product and try again.')) ); $this->executeInStoreContext ->execute('fixture_second_store', [$this->cartFactory->create(), 'addProduct'], $product->getId()); } /** * @magentoDataFixture Magento/Catalog/_files/product_simple.php * * @return void */ public function testAddProductWithInvalidRequest(): void { $product = $this->productRepository->get('simple'); $message = __('We found an invalid request for adding product to quote.'); $this->expectExceptionObject(new LocalizedException($message)); $this->cartFactory->create()->addProduct($product->getId(), ''); } }