TemplateTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Email\Test\Unit\Block\Adminhtml;
  7. /**
  8. * @covers Magento\Email\Block\Adminhtml\Template
  9. */
  10. class TemplateTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\Email\Block\Adminhtml\Template */
  13. protected $template;
  14. /** @var \Magento\Backend\Block\Template\Context */
  15. protected $context;
  16. /** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $urlBuilderMock;
  18. /** @var \Magento\Backend\Block\Widget\Button\ItemFactory|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $itemFactoryMock;
  20. /** @var \Magento\Backend\Block\Widget\Button\ButtonList */
  21. protected $buttonList;
  22. /** @var \Magento\Backend\Block\Widget\Button\Item|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $buttonMock;
  24. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  25. protected $objectManager;
  26. protected function setUp()
  27. {
  28. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  29. $this->itemFactoryMock = $this->getMockBuilder(\Magento\Backend\Block\Widget\Button\ItemFactory::class)
  30. ->disableOriginalConstructor()
  31. ->setMethods(['create'])
  32. ->getMock();
  33. $this->buttonMock = $this->getMockBuilder(\Magento\Backend\Block\Widget\Button\Item::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->itemFactoryMock->expects($this->any())
  37. ->method('create')
  38. ->willReturn($this->buttonMock);
  39. $this->buttonList = $this->objectManager->getObject(
  40. \Magento\Backend\Block\Widget\Button\ButtonList::class,
  41. [ 'itemFactory' => $this->itemFactoryMock]
  42. );
  43. $this->urlBuilderMock = $this->getMockForAbstractClass(
  44. \Magento\Framework\UrlInterface::class,
  45. [],
  46. '',
  47. false,
  48. true,
  49. true,
  50. ['getUrl']
  51. );
  52. $this->context = $this->objectManager->getObject(
  53. \Magento\Backend\Block\Template\Context::class,
  54. [
  55. 'urlBuilder' => $this->urlBuilderMock
  56. ]
  57. );
  58. $this->template = $this->objectManager->getObject(
  59. \Magento\Email\Block\Adminhtml\Template::class,
  60. [
  61. 'context' => $this->context,
  62. 'buttonList' => $this->buttonList
  63. ]
  64. );
  65. }
  66. public function testAddButton()
  67. {
  68. $this->template->addButton('1', ['title' => 'My Button']);
  69. $buttons = $this->buttonList->getItems()[0];
  70. $this->assertContains('1', array_keys($buttons));
  71. }
  72. public function testUpdateButton()
  73. {
  74. $this->testAddButton();
  75. $this->buttonMock->expects($this->once())
  76. ->method('setData')
  77. ->with('title', 'Updated Button')
  78. ->willReturnSelf();
  79. $result = $this->template->updateButton('1', 'title', 'Updated Button');
  80. $this->assertSame($this->template, $result);
  81. }
  82. public function testRemoveButton()
  83. {
  84. $this->testAddButton();
  85. $this->template->removeButton('1');
  86. $buttons = $this->buttonList->getItems()[0];
  87. $this->assertNotContains('1', array_keys($buttons));
  88. }
  89. public function testGetCreateUrl()
  90. {
  91. $this->urlBuilderMock->expects($this->once())
  92. ->method('getUrl')
  93. ->with('adminhtml/*/new', []);
  94. $this->template->getCreateUrl();
  95. }
  96. public function testGetHeaderText()
  97. {
  98. $this->assertEquals('Transactional Emails', $this->template->getHeaderText());
  99. }
  100. public function testCanRender()
  101. {
  102. $this->buttonMock->expects($this->once())
  103. ->method('isDeleted')
  104. ->willReturn(false);
  105. $this->assertTrue($this->template->canRender($this->buttonMock));
  106. }
  107. }