|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Test\Unit\Block\Product\View; |
| 8 | + |
| 9 | +use \PHPUnit\Framework\TestCase; |
| 10 | +use \Magento\Framework\Phrase; |
| 11 | +use \Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 12 | +use \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend; |
| 13 | +use \Magento\Catalog\Model\Product; |
| 14 | +use \Magento\Framework\View\Element\Template\Context; |
| 15 | +use \Magento\Framework\Registry; |
| 16 | +use \Magento\Framework\Pricing\PriceCurrencyInterface; |
| 17 | +use \Magento\Catalog\Block\Product\View\Attributes as AttributesBlock; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test class for \Magento\Catalog\Block\Product\View\Attributes |
| 21 | + * |
| 22 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 23 | + */ |
| 24 | +class AttributesTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var \Magento\Framework\Phrase |
| 28 | + */ |
| 29 | + private $phrase; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Eav\Model\Entity\Attribute\AbstractAttribute |
| 33 | + */ |
| 34 | + private $attribute; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend |
| 38 | + */ |
| 39 | + private $frontendAttribute; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product |
| 43 | + */ |
| 44 | + private $product; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\Template\Context |
| 48 | + */ |
| 49 | + private $context; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Registry |
| 53 | + */ |
| 54 | + private $registry; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Pricing\PriceCurrencyInterface |
| 58 | + */ |
| 59 | + private $priceCurrencyInterface; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var \Magento\Catalog\Block\Product\View\Attributes |
| 63 | + */ |
| 64 | + private $attributesBlock; |
| 65 | + |
| 66 | + protected function setUp() |
| 67 | + { |
| 68 | + $this->attribute = $this |
| 69 | + ->getMockBuilder(AbstractAttribute::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->getMock(); |
| 72 | + $this->attribute |
| 73 | + ->expects($this->any()) |
| 74 | + ->method('getIsVisibleOnFront') |
| 75 | + ->willReturn(true); |
| 76 | + $this->attribute |
| 77 | + ->expects($this->any()) |
| 78 | + ->method('getAttributeCode') |
| 79 | + ->willReturn('phrase'); |
| 80 | + $this->frontendAttribute = $this |
| 81 | + ->getMockBuilder(AbstractFrontend::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->getMock(); |
| 84 | + $this->attribute |
| 85 | + ->expects($this->any()) |
| 86 | + ->method('getFrontendInput') |
| 87 | + ->willReturn('phrase'); |
| 88 | + $this->attribute |
| 89 | + ->expects($this->any()) |
| 90 | + ->method('getFrontend') |
| 91 | + ->willReturn($this->frontendAttribute); |
| 92 | + $this->product = $this |
| 93 | + ->getMockBuilder(Product::class) |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->getMock(); |
| 96 | + $this->product |
| 97 | + ->expects($this->any()) |
| 98 | + ->method('getAttributes') |
| 99 | + ->willReturn([$this->attribute]); |
| 100 | + $this->product |
| 101 | + ->expects($this->any()) |
| 102 | + ->method('hasData') |
| 103 | + ->willReturn(true); |
| 104 | + $this->context = $this |
| 105 | + ->getMockBuilder(Context::class) |
| 106 | + ->disableOriginalConstructor() |
| 107 | + ->getMock(); |
| 108 | + $this->registry = $this |
| 109 | + ->getMockBuilder(Registry::class) |
| 110 | + ->disableOriginalConstructor() |
| 111 | + ->getMock(); |
| 112 | + $this->registry |
| 113 | + ->expects($this->any()) |
| 114 | + ->method('registry') |
| 115 | + ->willReturn($this->product); |
| 116 | + $this->priceCurrencyInterface = $this |
| 117 | + ->getMockBuilder(PriceCurrencyInterface::class) |
| 118 | + ->disableOriginalConstructor() |
| 119 | + ->getMock(); |
| 120 | + $this->attributesBlock = new AttributesBlock( |
| 121 | + $this->context, |
| 122 | + $this->registry, |
| 123 | + $this->priceCurrencyInterface |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @return void |
| 129 | + */ |
| 130 | + public function testGetAttributeNoValue() |
| 131 | + { |
| 132 | + $this->phrase = ''; |
| 133 | + $this->frontendAttribute |
| 134 | + ->expects($this->any()) |
| 135 | + ->method('getValue') |
| 136 | + ->willReturn($this->phrase); |
| 137 | + $attributes = $this->attributesBlock->getAdditionalData(); |
| 138 | + $this->assertTrue(empty($attributes['phrase'])); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @return void |
| 143 | + */ |
| 144 | + public function testGetAttributeHasValue() |
| 145 | + { |
| 146 | + $this->phrase = __('Yes'); |
| 147 | + $this->frontendAttribute |
| 148 | + ->expects($this->any()) |
| 149 | + ->method('getValue') |
| 150 | + ->willReturn($this->phrase); |
| 151 | + $attributes = $this->attributesBlock->getAdditionalData(); |
| 152 | + $this->assertNotTrue(empty($attributes['phrase'])); |
| 153 | + $this->assertNotTrue(empty($attributes['phrase']['value'])); |
| 154 | + $this->assertEquals('Yes', $attributes['phrase']['value']); |
| 155 | + } |
| 156 | +} |
0 commit comments