ValidateTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Test\Unit\Controller\Address;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class ValidateTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Customer\Controller\Adminhtml\Address\Validate
  16. */
  17. private $model;
  18. /**
  19. * @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $formFactoryMock;
  22. /**
  23. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $requestMock;
  26. /**
  27. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $resultRedirectFactoryMock;
  30. /**
  31. * @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $resultJsonFactoryMock;
  34. /**
  35. * @inheritdoc
  36. */
  37. protected function setUp()
  38. {
  39. $this->formFactoryMock = $this->createMock(\Magento\Customer\Model\Metadata\FormFactory::class);
  40. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  41. $this->resultJsonFactoryMock = $this->createMock(\Magento\Framework\Controller\Result\JsonFactory::class);
  42. $this->resultRedirectFactoryMock = $this->createMock(\Magento\Backend\Model\View\Result\RedirectFactory::class);
  43. $objectManager = new ObjectManagerHelper($this);
  44. $this->model = $objectManager->getObject(
  45. \Magento\Customer\Controller\Adminhtml\Address\Validate::class,
  46. [
  47. 'formFactory' => $this->formFactoryMock,
  48. 'request' => $this->requestMock,
  49. 'resultRedirectFactory' => $this->resultRedirectFactoryMock,
  50. 'resultJsonFactory' => $this->resultJsonFactoryMock,
  51. ]
  52. );
  53. }
  54. /**
  55. * Test method \Magento\Customer\Controller\Adminhtml\Address\Save::execute
  56. *
  57. * @throws \Magento\Framework\Exception\NoSuchEntityException
  58. */
  59. public function testExecute()
  60. {
  61. $addressId = 11;
  62. $errors = ['Error Message 1', 'Error Message 2'];
  63. $addressExtractedData = [
  64. 'entity_id' => $addressId,
  65. 'default_billing' => true,
  66. 'default_shipping' => true,
  67. 'code' => 'value',
  68. 'region' => [
  69. 'region' => 'region',
  70. 'region_id' => 'region_id',
  71. ],
  72. 'region_id' => 'region_id',
  73. 'id' => $addressId,
  74. ];
  75. $customerAddressFormMock = $this->createMock(\Magento\Customer\Model\Metadata\Form::class);
  76. $customerAddressFormMock->expects($this->atLeastOnce())
  77. ->method('extractData')
  78. ->with($this->requestMock)
  79. ->willReturn($addressExtractedData);
  80. $customerAddressFormMock->expects($this->once())
  81. ->method('validateData')
  82. ->with($addressExtractedData)
  83. ->willReturn($errors);
  84. $this->formFactoryMock->expects($this->exactly(1))
  85. ->method('create')
  86. ->willReturn($customerAddressFormMock);
  87. $resultJson = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
  88. $this->resultJsonFactoryMock->method('create')
  89. ->willReturn($resultJson);
  90. $validateResponseMock = $this->createPartialMock(
  91. \Magento\Framework\DataObject::class,
  92. ['getError', 'setMessages']
  93. );
  94. $validateResponseMock->method('setMessages')->willReturnSelf();
  95. $validateResponseMock->method('getError')->willReturn(1);
  96. $resultJson->method('setData')->willReturnSelf();
  97. $this->assertEquals($resultJson, $this->model->execute());
  98. }
  99. }