|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Checkout\Block\Cart\Item; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 15 | +use Magento\Quote\Model\Quote; |
| 16 | +use Magento\Quote\Model\Quote\Item; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | + |
| 20 | +/** |
| 21 | + * Class RendererTest |
| 22 | + * @package Magento\Checkout\Block\Cart\Item |
| 23 | + * @magentoAppArea frontend |
| 24 | + */ |
| 25 | +class RendererTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var ObjectManagerInterface |
| 29 | + */ |
| 30 | + private $objectManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var mixed |
| 34 | + */ |
| 35 | + private $renderer; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var mixed |
| 39 | + */ |
| 40 | + public $productRepository; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->renderer = $this->objectManager->get(Renderer::class); |
| 49 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Gets quote by reserved order id. |
| 54 | + * |
| 55 | + * @param string $reservedOrderId |
| 56 | + * @return Quote |
| 57 | + */ |
| 58 | + private function getQuote($reservedOrderId) |
| 59 | + { |
| 60 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 61 | + $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 62 | + $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId) |
| 63 | + ->create(); |
| 64 | + |
| 65 | + /** @var CartRepositoryInterface $quoteRepository */ |
| 66 | + $quoteRepository = $this->objectManager->get(CartRepositoryInterface::class); |
| 67 | + |
| 68 | + /** @var Quote[] $items */ |
| 69 | + $items = $quoteRepository->getList($searchCriteria)->getItems(); |
| 70 | + |
| 71 | + return $items[0]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id |
| 76 | + * |
| 77 | + * @param Quote $quote |
| 78 | + * @param string|int $productId |
| 79 | + * |
| 80 | + * @return Item|null |
| 81 | + */ |
| 82 | + private function _getQuoteItemIdByProductId($quote, $productId) |
| 83 | + { |
| 84 | + /** @var $quoteItems Item[] */ |
| 85 | + $quoteItems = $quote->getAllItems(); |
| 86 | + foreach ($quoteItems as $quoteItem) { |
| 87 | + if ($productId == $quoteItem->getProductId()) { |
| 88 | + return $quoteItem; |
| 89 | + } |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @magentoDataFixture Magento/Checkout/_files/cart_with_simple_product_and_custom_option_text_area.php |
| 96 | + */ |
| 97 | + public function testTextAreaCustomOption() |
| 98 | + { |
| 99 | + $quote = $this->getQuote('test_order_item_with_custom_option_text_area'); |
| 100 | + |
| 101 | + /** @var $product Product */ |
| 102 | + $product = $this->productRepository->get('simple_with_custom_option_text_area'); |
| 103 | + |
| 104 | + $quoteItem = $this->_getQuoteItemIdByProductId($quote, $product->getId()); |
| 105 | + $this->assertNotNull($quoteItem, 'Cannot get quote item for simple product with custom option text area'); |
| 106 | + |
| 107 | + $template = 'Magento_Checkout::cart/item/default.phtml'; |
| 108 | + $this->renderer->setTemplate($template); |
| 109 | + $this->renderer->setItem($quoteItem); |
| 110 | + |
| 111 | + $priceBlock = $this->objectManager->create(\Magento\Checkout\Block\Item\Price\Renderer::class); |
| 112 | + $this->renderer->getLayout()->setBlock('checkout.item.price.unit', $priceBlock); |
| 113 | + $this->renderer->getLayout()->setBlock('checkout.item.price.row', $priceBlock); |
| 114 | + $html = $this->renderer->toHtml(); |
| 115 | + |
| 116 | + $this->assertMatchesRegularExpression('/Test product simple with |
| 117 | +custom option text area |
| 118 | +with more 50 characters/', $html); |
| 119 | + } |
| 120 | +} |
0 commit comments