ConfigurableAttributeDataTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Test\Unit\Model;
  7. /**
  8. * Class CustomOptionTest
  9. */
  10. class ConfigurableAttributeDataTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $product;
  16. /**
  17. * @var \Magento\ConfigurableProduct\Model\ConfigurableAttributeData|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $configurableAttributeData;
  20. /**
  21. * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute|
  22. * \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $attributeMock;
  25. /**
  26. * Test setUp
  27. */
  28. protected function setUp()
  29. {
  30. $this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
  31. 'getTypeInstance',
  32. 'setParentId',
  33. 'hasPreconfiguredValues',
  34. 'getPreconfiguredValues',
  35. 'getPriceInfo',
  36. 'getStoreId'
  37. ]);
  38. $this->attributeMock = $this->createMock(
  39. \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute::class
  40. );
  41. $this->configurableAttributeData = new \Magento\ConfigurableProduct\Model\ConfigurableAttributeData();
  42. }
  43. /**
  44. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  45. */
  46. public function testPrepareJsonAttributes()
  47. {
  48. $storeId = '1';
  49. $attributeId = 5;
  50. $attributeOptions = [
  51. ['value_index' => 'option_id_1', 'label' => 'label_1'],
  52. ['value_index' => 'option_id_2', 'label' => 'label_2'],
  53. ];
  54. $position = 2;
  55. $expected = [
  56. 'attributes' => [
  57. $attributeId => [
  58. 'id' => $attributeId,
  59. 'code' => 'test_attribute',
  60. 'label' => 'Test',
  61. 'position' => $position,
  62. 'options' => [
  63. 0 => [
  64. 'id' => 'option_id_1',
  65. 'label' => 'label_1',
  66. 'products' => 'option_products_1',
  67. ],
  68. 1 => [
  69. 'id' => 'option_id_2',
  70. 'label' => 'label_2',
  71. 'products' => 'option_products_2',
  72. ],
  73. ],
  74. ],
  75. ],
  76. 'defaultValues' => [
  77. $attributeId => 'option_id_1',
  78. ],
  79. ];
  80. $options = [
  81. $attributeId => ['option_id_1' => 'option_products_1', 'option_id_2' => 'option_products_2'],
  82. ];
  83. $productAttributeMock = $this->getMockBuilder(\Magento\Catalog\Model\Entity\Attribute::class)
  84. ->disableOriginalConstructor()
  85. ->setMethods(['getStoreLabel', '__wakeup', 'getAttributeCode', 'getId', 'getAttributeLabel'])
  86. ->getMock();
  87. $productAttributeMock->expects($this->once())
  88. ->method('getId')
  89. ->willReturn($attributeId);
  90. $productAttributeMock->expects($this->once())
  91. ->method('getAttributeCode')
  92. ->willReturn($expected['attributes'][$attributeId]['code']);
  93. $attributeMock = $this->getMockBuilder(
  94. \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute::class
  95. )
  96. ->disableOriginalConstructor()
  97. ->setMethods(['getProductAttribute', '__wakeup', 'getLabel', 'getOptions', 'getAttributeId', 'getPosition'])
  98. ->getMock();
  99. $attributeMock->expects($this->once())
  100. ->method('getProductAttribute')
  101. ->willReturn($productAttributeMock);
  102. $attributeMock->expects($this->once())
  103. ->method('getPosition')
  104. ->willReturn($position);
  105. $this->product->expects($this->once())->method('getStoreId')->willReturn($storeId);
  106. $productAttributeMock->expects($this->once())
  107. ->method('getStoreLabel')
  108. ->with($storeId)
  109. ->willReturn($expected['attributes'][$attributeId]['label']);
  110. $attributeMock->expects($this->atLeastOnce())
  111. ->method('getAttributeId')
  112. ->willReturn($attributeId);
  113. $attributeMock->expects($this->atLeastOnce())
  114. ->method('getOptions')
  115. ->willReturn($attributeOptions);
  116. $configurableProduct = $this->getMockBuilder(
  117. \Magento\ConfigurableProduct\Model\Product\Type\Configurable::class
  118. )->disableOriginalConstructor()->getMock();
  119. $configurableProduct->expects($this->once())
  120. ->method('getConfigurableAttributes')
  121. ->with($this->product)
  122. ->willReturn([$attributeMock]);
  123. $configuredValueMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $configuredValueMock->expects($this->any())
  127. ->method('getData')
  128. ->willReturn($expected['defaultValues'][$attributeId]);
  129. $this->product->expects($this->once())
  130. ->method('getTypeInstance')
  131. ->willReturn($configurableProduct);
  132. $this->product->expects($this->once())
  133. ->method('hasPreconfiguredValues')
  134. ->willReturn(true);
  135. $this->product->expects($this->once())
  136. ->method('getPreconfiguredValues')
  137. ->willReturn($configuredValueMock);
  138. $this->assertEquals($expected, $this->configurableAttributeData->getAttributesData($this->product, $options));
  139. }
  140. }