AttributesListTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. class AttributesListTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Swatches\Model\AttributesList
  11. */
  12. protected $attributeListModel;
  13. /**
  14. * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $collectionMock;
  17. /**
  18. * @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $attributeMock;
  21. /** @var \Magento\Swatches\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $swatchHelper;
  23. protected function setUp()
  24. {
  25. $this->swatchHelper = $this->createMock(\Magento\Swatches\Helper\Data::class);
  26. $this->collectionMock = $this->createMock(
  27. \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection::class
  28. );
  29. /** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactoryMock */
  30. $collectionFactoryMock = $this->createPartialMock(
  31. \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory::class,
  32. ['create']
  33. );
  34. $collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
  35. $methods = ['getId', 'getFrontendLabel', 'getAttributeCode', 'getSource'];
  36. $this->attributeMock = $this->createPartialMock(
  37. \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class,
  38. $methods
  39. );
  40. $this->collectionMock
  41. ->expects($this->once())
  42. ->method('getItems')
  43. ->will($this->returnValue(['id' => $this->attributeMock]));
  44. $this->attributeListModel = new \Magento\Swatches\Model\AttributesList(
  45. $collectionFactoryMock,
  46. $this->swatchHelper
  47. );
  48. }
  49. public function testGetAttributes()
  50. {
  51. $ids = [1, 2, 3];
  52. $result = [
  53. [
  54. 'id' => 'id',
  55. 'label' => 'label',
  56. 'code' => 'code',
  57. 'options' => ['options'],
  58. 'canCreateOption' => false
  59. ]
  60. ];
  61. $this->collectionMock
  62. ->expects($this->any())
  63. ->method('addFieldToFilter')
  64. ->with('main_table.attribute_id', $ids);
  65. $this->attributeMock->expects($this->once())->method('getId')->will($this->returnValue('id'));
  66. $this->attributeMock->expects($this->once())->method('getFrontendLabel')->will($this->returnValue('label'));
  67. $this->attributeMock->expects($this->once())->method('getAttributeCode')->will($this->returnValue('code'));
  68. $source = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class);
  69. $source->expects($this->once())->method('getAllOptions')->with(false)->will($this->returnValue(['options']));
  70. $this->attributeMock->expects($this->once())->method('getSource')->will($this->returnValue($source));
  71. $this->swatchHelper->expects($this->once())->method('isSwatchAttribute')
  72. ->with($this->attributeMock)
  73. ->willReturn(true);
  74. $this->assertEquals($result, $this->attributeListModel->getAttributes($ids));
  75. }
  76. }