SwatchAttributesProviderTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Test\Unit\Model;
  7. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Swatches\Model\SwatchAttributeCodes;
  10. use Magento\Swatches\Model\SwatchAttributesProvider;
  11. use Magento\Swatches\Model\SwatchAttributeType;
  12. class SwatchAttributesProviderTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var SwatchAttributesProvider
  16. */
  17. private $swatchAttributeProvider;
  18. /**
  19. * @var Configurable|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $typeConfigurable;
  22. /**
  23. * @var SwatchAttributeCodes|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $swatchAttributeCodes;
  26. /**
  27. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $productMock;
  30. /**
  31. * @var SwatchAttributeType | \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $swatchTypeChecker;
  34. protected function setUp()
  35. {
  36. $this->typeConfigurable = $this->createPartialMock(
  37. Configurable::class,
  38. ['getConfigurableAttributes', 'getCodes', 'getProductAttribute']
  39. );
  40. $this->swatchAttributeCodes = $this->createMock(SwatchAttributeCodes::class);
  41. $this->productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getId', 'getTypeId']);
  42. $this->swatchTypeChecker = $this->createMock(SwatchAttributeType::class);
  43. $this->swatchAttributeProvider = (new ObjectManager($this))->getObject(SwatchAttributesProvider::class, [
  44. 'typeConfigurable' => $this->typeConfigurable,
  45. 'swatchAttributeCodes' => $this->swatchAttributeCodes,
  46. 'swatchTypeChecker' => $this->swatchTypeChecker,
  47. ]);
  48. }
  49. public function testProvide()
  50. {
  51. $this->productMock->method('getId')->willReturn(1);
  52. $this->productMock->method('getTypeId')
  53. ->willReturn(Configurable::TYPE_CODE);
  54. $attributeMock = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
  55. ->disableOriginalConstructor()
  56. ->setMethods(['setStoreId', 'getData', 'setData', 'getSource', 'hasData'])
  57. ->getMock();
  58. $configAttributeMock = $this->createPartialMock(
  59. Configurable\Attribute::class,
  60. ['getAttributeId', 'getProductAttribute']
  61. );
  62. $configAttributeMock
  63. ->method('getAttributeId')
  64. ->willReturn(1);
  65. $configAttributeMock
  66. ->method('getProductAttribute')
  67. ->willReturn($attributeMock);
  68. $this->typeConfigurable
  69. ->method('getConfigurableAttributes')
  70. ->with($this->productMock)
  71. ->willReturn([$configAttributeMock]);
  72. $swatchAttributes = [1 => 'text_swatch'];
  73. $this->swatchAttributeCodes
  74. ->method('getCodes')
  75. ->willReturn($swatchAttributes);
  76. $this->swatchTypeChecker->expects($this->once())->method('isSwatchAttribute')->willReturn(true);
  77. $result = $this->swatchAttributeProvider->provide($this->productMock);
  78. $this->assertEquals([1 => $attributeMock], $result);
  79. }
  80. }