SendTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SendFriend\Block;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. class SendTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\SendFriend\Block\Send
  12. */
  13. protected $_block;
  14. protected function setUp()
  15. {
  16. $this->_block = Bootstrap::getObjectManager()->create(\Magento\SendFriend\Block\Send::class);
  17. }
  18. /**
  19. * @param string $field
  20. * @param string $value
  21. * @dataProvider formDataProvider
  22. * @covers \Magento\SendFriend\Block\Send::getUserName
  23. * @covers \Magento\SendFriend\Block\Send::getEmail
  24. */
  25. public function testGetCustomerFieldFromFormData($field, $value)
  26. {
  27. $formData = ['sender' => [$field => $value]];
  28. $this->_block->setFormData($formData);
  29. $this->assertEquals(trim($value), $this->_callBlockMethod($field));
  30. }
  31. /**
  32. * @return array
  33. */
  34. public function formDataProvider()
  35. {
  36. return [
  37. ['name', 'Customer Form Name'],
  38. ['email', 'customer_form_email@example.com']
  39. ];
  40. }
  41. /**
  42. * @param string $field
  43. * @param string $value
  44. * @dataProvider customerSessionDataProvider
  45. * @covers \Magento\SendFriend\Block\Send::getUserName
  46. * @covers \Magento\SendFriend\Block\Send::getEmail
  47. * @magentoDataFixture Magento/Customer/_files/customer.php
  48. */
  49. public function testGetCustomerFieldFromSession($field, $value)
  50. {
  51. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  52. /** @var $session \Magento\Customer\Model\Session */
  53. $session = Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Session::class, [$logger]);
  54. /** @var \Magento\Customer\Api\AccountManagementInterface $service */
  55. $service = Bootstrap::getObjectManager()->create(\Magento\Customer\Api\AccountManagementInterface::class);
  56. $customer = $service->authenticate('customer@example.com', 'password');
  57. $session->setCustomerDataAsLoggedIn($customer);
  58. $this->assertEquals($value, $this->_callBlockMethod($field));
  59. }
  60. /**
  61. * @return array
  62. */
  63. public function customerSessionDataProvider()
  64. {
  65. return [
  66. ['name', 'John Smith'],
  67. ['email', 'customer@example.com']
  68. ];
  69. }
  70. /**
  71. * Call block method based on form field
  72. *
  73. * @param string $field
  74. * @return null|string
  75. */
  76. protected function _callBlockMethod($field)
  77. {
  78. switch ($field) {
  79. case 'name':
  80. return $this->_block->getUserName();
  81. case 'email':
  82. return $this->_block->getEmail();
  83. default:
  84. return null;
  85. }
  86. }
  87. }