DataTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Contact\Test\Unit\Helper;
  7. class DataTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * Helper
  11. *
  12. * @var \Magento\Contact\Helper\Data
  13. */
  14. protected $helper;
  15. /**
  16. * Scope config mock
  17. *
  18. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $scopeConfigMock;
  21. /**
  22. * Customer session mock
  23. *
  24. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $customerSessionMock;
  27. /**
  28. * Customer view helper mock
  29. *
  30. * @var \Magento\Customer\Helper\View|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $customerViewHelperMock;
  33. /**
  34. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  35. */
  36. protected $objectManagerHelper;
  37. protected function setUp()
  38. {
  39. $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  40. $className = \Magento\Contact\Helper\Data::class;
  41. $arguments = $this->objectManagerHelper->getConstructArguments($className);
  42. /**
  43. * @var \Magento\Framework\App\Helper\Context $context
  44. */
  45. $context = $arguments['context'];
  46. $this->scopeConfigMock = $context->getScopeConfig();
  47. $this->customerSessionMock = $arguments['customerSession'];
  48. $this->customerViewHelperMock = $arguments['customerViewHelper'];
  49. $this->helper = $this->objectManagerHelper->getObject($className, $arguments);
  50. }
  51. public function testIsEnabled()
  52. {
  53. $this->scopeConfigMock->expects($this->once())
  54. ->method('getValue')
  55. ->willReturn('1');
  56. $this->assertTrue(is_string($this->helper->isEnabled()));
  57. }
  58. public function testIsNotEnabled()
  59. {
  60. $this->scopeConfigMock->expects($this->once())
  61. ->method('getValue')
  62. ->willReturn(null);
  63. $this->assertTrue(null === $this->helper->isEnabled());
  64. }
  65. public function testGetUserNameNotLoggedIn()
  66. {
  67. $this->customerSessionMock->expects($this->once())
  68. ->method('isLoggedIn')
  69. ->willReturn(false);
  70. $this->assertEmpty($this->helper->getUserName());
  71. }
  72. public function testGetUserName()
  73. {
  74. $this->customerSessionMock->expects($this->once())
  75. ->method('isLoggedIn')
  76. ->willReturn(true);
  77. $customerDataObject = $this->getMockBuilder(\Magento\Customer\Model\Data\Customer::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->customerSessionMock->expects($this->once())
  81. ->method('getCustomerDataObject')
  82. ->willReturn($customerDataObject);
  83. $this->customerViewHelperMock->expects($this->once())
  84. ->method('getCustomerName')
  85. ->willReturn(' customer name ');
  86. $this->assertEquals('customer name', $this->helper->getUserName());
  87. }
  88. public function testGetUserEmailNotLoggedIn()
  89. {
  90. $this->customerSessionMock->expects($this->once())
  91. ->method('isLoggedIn')
  92. ->willReturn(false);
  93. $this->assertEmpty($this->helper->getUserEmail());
  94. }
  95. public function testGetUserEmail()
  96. {
  97. $this->customerSessionMock->expects($this->once())
  98. ->method('isLoggedIn')
  99. ->willReturn(true);
  100. $customerDataObject = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  101. $customerDataObject->expects($this->once())
  102. ->method('getEmail')
  103. ->willReturn('customer@email.com');
  104. $this->customerSessionMock->expects($this->once())
  105. ->method('getCustomerDataObject')
  106. ->willReturn($customerDataObject);
  107. $this->assertEquals('customer@email.com', $this->helper->getUserEmail());
  108. }
  109. public function testGetPostValue()
  110. {
  111. $postData = ['name' => 'Some Name', 'email' => 'Some Email'];
  112. $dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
  113. ->getMockForAbstractClass();
  114. $dataPersistorMock->expects($this->once())
  115. ->method('get')
  116. ->with('contact_us')
  117. ->willReturn($postData);
  118. $dataPersistorMock->expects($this->once())
  119. ->method('clear')
  120. ->with('contact_us');
  121. $this->objectManagerHelper->setBackwardCompatibleProperty($this->helper, 'dataPersistor', $dataPersistorMock);
  122. $this->assertSame($postData['name'], $this->helper->getPostValue('name'));
  123. $this->assertSame($postData['email'], $this->helper->getPostValue('email'));
  124. }
  125. }