ElementFactoryTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Model\Metadata\ElementFactory;
  8. class ElementFactoryTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject */
  11. private $_objectManager;
  12. /** @var \Magento\Customer\Model\Data\AttributeMetadata | \PHPUnit_Framework_MockObject_MockObject */
  13. private $_attributeMetadata;
  14. /** @var string */
  15. private $_entityTypeCode = 'customer_address';
  16. /** @var ElementFactory */
  17. private $_elementFactory;
  18. protected function setUp()
  19. {
  20. $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  21. $this->_attributeMetadata = $this->createMock(\Magento\Customer\Model\Data\AttributeMetadata::class);
  22. $this->_elementFactory = new ElementFactory($this->_objectManager, new \Magento\Framework\Stdlib\StringUtils());
  23. }
  24. /** TODO fix when Validation is implemented MAGETWO-17341 */
  25. public function testAttributePostcodeDataModelClass()
  26. {
  27. $this->_attributeMetadata->expects(
  28. $this->once()
  29. )->method(
  30. 'getDataModel'
  31. )->will(
  32. $this->returnValue(\Magento\Customer\Model\Attribute\Data\Postcode::class)
  33. );
  34. $dataModel = $this->createMock(\Magento\Customer\Model\Metadata\Form\Text::class);
  35. $this->_objectManager->expects($this->once())->method('create')->will($this->returnValue($dataModel));
  36. $actual = $this->_elementFactory->create($this->_attributeMetadata, '95131', $this->_entityTypeCode);
  37. $this->assertSame($dataModel, $actual);
  38. }
  39. public function testAttributeEmptyDataModelClass()
  40. {
  41. $this->_attributeMetadata->expects($this->once())->method('getDataModel')->will($this->returnValue(''));
  42. $this->_attributeMetadata->expects(
  43. $this->once()
  44. )->method(
  45. 'getFrontendInput'
  46. )->will(
  47. $this->returnValue('text')
  48. );
  49. $dataModel = $this->createMock(\Magento\Customer\Model\Metadata\Form\Text::class);
  50. $params = [
  51. 'entityTypeCode' => $this->_entityTypeCode,
  52. 'value' => 'Some Text',
  53. 'isAjax' => false,
  54. 'attribute' => $this->_attributeMetadata,
  55. ];
  56. $this->_objectManager->expects(
  57. $this->once()
  58. )->method(
  59. 'create'
  60. )->with(
  61. \Magento\Customer\Model\Metadata\Form\Text::class,
  62. $params
  63. )->will(
  64. $this->returnValue($dataModel)
  65. );
  66. $actual = $this->_elementFactory->create($this->_attributeMetadata, 'Some Text', $this->_entityTypeCode);
  67. $this->assertSame($dataModel, $actual);
  68. }
  69. }