|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Catalog\Block\Product;
|
7 | 9 |
|
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Framework\Registry; |
| 14 | +use Magento\Framework\Serialize\Serializer\Json; |
| 15 | +use Magento\Framework\View\LayoutInterface; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
8 | 19 | /**
|
9 |
| - * Test class for \Magento\Catalog\Block\Product\View. |
| 20 | + * Checks product view block. |
10 | 21 | *
|
| 22 | + * @see \Magento\Catalog\Block\Product\View |
11 | 23 | * @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
| 24 | + * @magentoDbIsolation enabled |
12 | 25 | */
|
13 |
| -class ViewTest extends \PHPUnit\Framework\TestCase |
| 26 | +class ViewTest extends TestCase |
14 | 27 | {
|
15 |
| - /** |
16 |
| - * @var \Magento\Catalog\Block\Product\View |
17 |
| - */ |
18 |
| - protected $_block; |
| 28 | + /** @var ObjectManagerInterface */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** @var View */ |
| 32 | + private $block; |
| 33 | + |
| 34 | + /** @var ProductRepositoryInterface */ |
| 35 | + private $productRepository; |
| 36 | + |
| 37 | + /** @var Registry */ |
| 38 | + private $registry; |
| 39 | + |
| 40 | + /** @var LayoutInterface */ |
| 41 | + private $layout; |
| 42 | + |
| 43 | + /** @var Json */ |
| 44 | + private $json; |
19 | 45 |
|
20 | 46 | /**
|
21 |
| - * @var \Magento\Catalog\Model\Product |
| 47 | + * @inheritdoc |
22 | 48 | */
|
23 |
| - protected $_product; |
24 |
| - |
25 | 49 | protected function setUp()
|
26 | 50 | {
|
27 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
28 |
| - $this->_block = $objectManager->create(\Magento\Catalog\Block\Product\View::class); |
| 51 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 52 | + $this->block = $this->objectManager->create(View::class); |
| 53 | + $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 54 | + $this->layout = $this->objectManager->get(LayoutInterface::class); |
| 55 | + $this->registry = $this->objectManager->get(Registry::class); |
| 56 | + $this->json = $this->objectManager->get(Json::class); |
| 57 | + } |
29 | 58 |
|
30 |
| - /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ |
31 |
| - $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
32 |
| - $this->_product = $productRepository->get('simple'); |
| 59 | + /** |
| 60 | + * @return void |
| 61 | + */ |
| 62 | + public function testSetLayout(): void |
| 63 | + { |
| 64 | + $productView = $this->layout->createBlock(View::class); |
33 | 65 |
|
34 |
| - $objectManager->get(\Magento\Framework\Registry::class)->unregister('product'); |
35 |
| - $objectManager->get(\Magento\Framework\Registry::class)->register('product', $this->_product); |
| 66 | + $this->assertInstanceOf(LayoutInterface::class, $productView->getLayout()); |
36 | 67 | }
|
37 | 68 |
|
38 |
| - public function testSetLayout() |
| 69 | + /** |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + public function testGetProduct(): void |
39 | 73 | {
|
40 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 74 | + $product = $this->productRepository->get('simple'); |
| 75 | + $this->registerProduct($product); |
41 | 76 |
|
42 |
| - /** @var $layout \Magento\Framework\View\Layout */ |
43 |
| - $layout = $objectManager->get(\Magento\Framework\View\LayoutInterface::class); |
| 77 | + $this->assertNotEmpty($this->block->getProduct()->getId()); |
| 78 | + $this->assertEquals($product->getId(), $this->block->getProduct()->getId()); |
44 | 79 |
|
45 |
| - $productView = $layout->createBlock(\Magento\Catalog\Block\Product\View::class); |
| 80 | + $this->registry->unregister('product'); |
| 81 | + $this->block->setProductId($product->getId()); |
46 | 82 |
|
47 |
| - $this->assertInstanceOf(\Magento\Framework\View\LayoutInterface::class, $productView->getLayout()); |
| 83 | + $this->assertEquals($product->getId(), $this->block->getProduct()->getId()); |
48 | 84 | }
|
49 | 85 |
|
50 |
| - public function testGetProduct() |
| 86 | + /** |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + public function testCanEmailToFriend(): void |
51 | 90 | {
|
52 |
| - $this->assertNotEmpty($this->_block->getProduct()->getId()); |
53 |
| - $this->assertEquals($this->_product->getId(), $this->_block->getProduct()->getId()); |
54 |
| - |
55 |
| - /** @var $objectManager \Magento\TestFramework\ObjectManager */ |
56 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
57 |
| - $objectManager->get(\Magento\Framework\Registry::class)->unregister('product'); |
58 |
| - $this->_block->setProductId($this->_product->getId()); |
59 |
| - $this->assertEquals($this->_product->getId(), $this->_block->getProduct()->getId()); |
| 91 | + $this->assertFalse($this->block->canEmailToFriend()); |
60 | 92 | }
|
61 | 93 |
|
62 |
| - public function testCanEmailToFriend() |
| 94 | + /** |
| 95 | + * @return void |
| 96 | + */ |
| 97 | + public function testGetAddToCartUrl(): void |
63 | 98 | {
|
64 |
| - $this->assertFalse($this->_block->canEmailToFriend()); |
65 |
| - } |
| 99 | + $product = $this->productRepository->get('simple'); |
| 100 | + $url = $this->block->getAddToCartUrl($product); |
66 | 101 |
|
67 |
| - public function testGetAddToCartUrl() |
68 |
| - { |
69 |
| - $url = $this->_block->getAddToCartUrl($this->_product); |
70 |
| - $this->assertStringMatchesFormat('%scheckout/cart/add/%sproduct/' . $this->_product->getId() . '/', $url); |
| 102 | + $this->assertStringMatchesFormat( |
| 103 | + '%scheckout/cart/add/%sproduct/' . $product->getId() . '/', |
| 104 | + $url |
| 105 | + ); |
71 | 106 | }
|
72 | 107 |
|
73 |
| - public function testGetJsonConfig() |
| 108 | + /** |
| 109 | + * @return void |
| 110 | + */ |
| 111 | + public function testGetJsonConfig(): void |
74 | 112 | {
|
75 |
| - $config = (array)json_decode($this->_block->getJsonConfig()); |
| 113 | + $product = $this->productRepository->get('simple'); |
| 114 | + $this->registerProduct($product); |
| 115 | + $config = $this->json->unserialize($this->block->getJsonConfig()); |
| 116 | + |
76 | 117 | $this->assertNotEmpty($config);
|
77 | 118 | $this->assertArrayHasKey('productId', $config);
|
78 |
| - $this->assertEquals($this->_product->getId(), $config['productId']); |
| 119 | + $this->assertEquals($product->getId(), $config['productId']); |
79 | 120 | }
|
80 | 121 |
|
81 |
| - public function testHasOptions() |
| 122 | + /** |
| 123 | + * @return void |
| 124 | + */ |
| 125 | + public function testHasOptions(): void |
82 | 126 | {
|
83 |
| - $this->assertTrue($this->_block->hasOptions()); |
| 127 | + $product = $this->productRepository->get('simple'); |
| 128 | + $this->registerProduct($product); |
| 129 | + |
| 130 | + $this->assertTrue($this->block->hasOptions()); |
84 | 131 | }
|
85 | 132 |
|
86 |
| - public function testHasRequiredOptions() |
| 133 | + /** |
| 134 | + * @return void |
| 135 | + */ |
| 136 | + public function testHasRequiredOptions(): void |
87 | 137 | {
|
88 |
| - $this->assertTrue($this->_block->hasRequiredOptions()); |
| 138 | + $product = $this->productRepository->get('simple'); |
| 139 | + $this->registerProduct($product); |
| 140 | + |
| 141 | + $this->assertTrue($this->block->hasRequiredOptions()); |
89 | 142 | }
|
90 | 143 |
|
91 |
| - public function testStartBundleCustomization() |
| 144 | + /** |
| 145 | + * @return void |
| 146 | + */ |
| 147 | + public function testStartBundleCustomization(): void |
92 | 148 | {
|
93 | 149 | $this->markTestSkipped("Functionality not implemented in Magento 1.x. Implemented in Magento 2");
|
94 |
| - $this->assertFalse($this->_block->startBundleCustomization()); |
| 150 | + |
| 151 | + $this->assertFalse($this->block->startBundleCustomization()); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @magentoAppArea frontend |
| 156 | + * |
| 157 | + * @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php |
| 158 | + */ |
| 159 | + public function testAddToCartBlockInvisibility(): void |
| 160 | + { |
| 161 | + $outOfStockProduct = $this->productRepository->get('simple-out-of-stock'); |
| 162 | + $this->registerProduct($outOfStockProduct); |
| 163 | + $this->block->setTemplate('Magento_Catalog::product/view/addtocart.phtml'); |
| 164 | + $output = $this->block->toHtml(); |
| 165 | + |
| 166 | + $this->assertNotContains((string)__('Add to Cart'), $output); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @magentoAppArea frontend |
| 171 | + */ |
| 172 | + public function testAddToCartBlockVisibility(): void |
| 173 | + { |
| 174 | + $product = $this->productRepository->get('simple'); |
| 175 | + $this->registerProduct($product); |
| 176 | + $this->block->setTemplate('Magento_Catalog::product/view/addtocart.phtml'); |
| 177 | + $output = $this->block->toHtml(); |
| 178 | + |
| 179 | + $this->assertContains((string)__('Add to Cart'), $output); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Register the product |
| 184 | + * |
| 185 | + * @param ProductInterface $product |
| 186 | + * @return void |
| 187 | + */ |
| 188 | + private function registerProduct(ProductInterface $product): void |
| 189 | + { |
| 190 | + $this->registry->unregister('product'); |
| 191 | + $this->registry->register('product', $product); |
95 | 192 | }
|
96 | 193 | }
|
0 commit comments