FormTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Block\Widget;
  7. use Magento\Backend\Block\Template\Context;
  8. use Magento\Backend\Block\Widget\Form;
  9. use Magento\Framework\Data\Form as DataForm;
  10. use Magento\Framework\UrlInterface;
  11. class FormTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var Form */
  14. protected $model;
  15. /** @var Context |\PHPUnit_Framework_MockObject_MockObject */
  16. protected $context;
  17. /** @var DataForm |\PHPUnit_Framework_MockObject_MockObject */
  18. protected $dataForm;
  19. /** @var UrlInterface |\PHPUnit_Framework_MockObject_MockObject */
  20. protected $urlBuilder;
  21. protected function setUp()
  22. {
  23. $this->prepareContext();
  24. $this->dataForm = $this->getMockBuilder(\Magento\Framework\Data\Form::class)
  25. ->disableOriginalConstructor()
  26. ->setMethods([
  27. 'setParent',
  28. 'setBaseUrl',
  29. 'addCustomAttribute',
  30. ])
  31. ->getMock();
  32. $this->model = new Form(
  33. $this->context
  34. );
  35. }
  36. protected function prepareContext()
  37. {
  38. $this->urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  39. ->getMock();
  40. $this->context = $this->getMockBuilder(\Magento\Backend\Block\Template\Context::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->context->expects($this->any())
  44. ->method('getUrlBuilder')
  45. ->willReturn($this->urlBuilder);
  46. }
  47. public function testSetForm()
  48. {
  49. $baseUrl = 'base_url';
  50. $attributeKey = 'attribute_key';
  51. $attributeValue = 'attribute_value';
  52. $this->dataForm->expects($this->once())
  53. ->method('setParent')
  54. ->with($this->model)
  55. ->willReturnSelf();
  56. $this->dataForm->expects($this->once())
  57. ->method('setBaseUrl')
  58. ->with($baseUrl)
  59. ->willReturnSelf();
  60. $this->dataForm->expects($this->once())
  61. ->method('addCustomAttribute')
  62. ->with($attributeKey, $attributeValue)
  63. ->willReturnSelf();
  64. $this->urlBuilder->expects($this->once())
  65. ->method('getBaseUrl')
  66. ->willReturn($baseUrl);
  67. $this->model->setData('custom_attributes', [$attributeKey => $attributeValue]);
  68. $this->assertEquals($this->model, $this->model->setForm($this->dataForm));
  69. }
  70. public function testSetFormNoCustomAttributes()
  71. {
  72. $baseUrl = 'base_url';
  73. $this->dataForm->expects($this->once())
  74. ->method('setParent')
  75. ->with($this->model)
  76. ->willReturnSelf();
  77. $this->dataForm->expects($this->once())
  78. ->method('setBaseUrl')
  79. ->with($baseUrl)
  80. ->willReturnSelf();
  81. $this->urlBuilder->expects($this->once())
  82. ->method('getBaseUrl')
  83. ->willReturn($baseUrl);
  84. $this->assertEquals($this->model, $this->model->setForm($this->dataForm));
  85. }
  86. }