AttributeResolverTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Metadata;
  7. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  8. use Magento\Customer\Model\Attribute;
  9. use Magento\Customer\Model\AttributeMetadataDataProvider;
  10. use Magento\Customer\Model\Metadata\AttributeResolver;
  11. class AttributeResolverTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var AttributeResolver */
  14. protected $model;
  15. /** @var AttributeMetadataDataProvider|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $metadataDataProviderMock;
  17. protected function setUp()
  18. {
  19. $this->metadataDataProviderMock = $this->getMockBuilder(
  20. \Magento\Customer\Model\AttributeMetadataDataProvider::class
  21. )->disableOriginalConstructor()->getMock();
  22. $this->model = new AttributeResolver(
  23. $this->metadataDataProviderMock
  24. );
  25. }
  26. public function testGetModelByAttribute()
  27. {
  28. $entityType = 'type';
  29. $attributeCode = 'code';
  30. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  31. $attributeMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $attributeMock->expects($this->once())
  35. ->method('getAttributeCode')
  36. ->willReturn($attributeCode);
  37. /** @var Attribute|\PHPUnit_Framework_MockObject_MockObject $modelMock */
  38. $modelMock = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->metadataDataProviderMock->expects($this->once())
  42. ->method('getAttribute')
  43. ->with($entityType, $attributeCode)
  44. ->willReturn($modelMock);
  45. $this->assertEquals($modelMock, $this->model->getModelByAttribute($entityType, $attributeMock));
  46. }
  47. /**
  48. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  49. * @expectedExceptionMessage No such entity with entityType = type, attributeCode = code
  50. */
  51. public function testGetModelByAttributeWithoutModel()
  52. {
  53. $entityType = 'type';
  54. $attributeCode = 'code';
  55. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  56. $attributeMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $attributeMock->expects($this->exactly(2))
  60. ->method('getAttributeCode')
  61. ->willReturn($attributeCode);
  62. $this->metadataDataProviderMock->expects($this->once())
  63. ->method('getAttribute')
  64. ->with($entityType, $attributeCode)
  65. ->willReturn(false);
  66. $this->model->getModelByAttribute($entityType, $attributeMock);
  67. }
  68. }