InfoTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Block\Account\Dashboard;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Customer\Block\Account\Dashboard\Info;
  9. /**
  10. * Test class for \Magento\Customer\Block\Account\Dashboard\Info.
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class InfoTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** Constant values used for testing */
  16. const CUSTOMER_ID = 1;
  17. const CHANGE_PASSWORD_URL = 'http://localhost/index.php/account/edit/changepass/1';
  18. const EMAIL_ADDRESS = 'john.doe@example.com';
  19. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\View\Element\Template\Context */
  20. private $_context;
  21. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Session */
  22. private $_customerSession;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Api\Data\CustomerInterface */
  24. private $_customer;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Helper\View
  27. */
  28. private $_helperView;
  29. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Newsletter\Model\Subscriber */
  30. private $_subscriber;
  31. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Newsletter\Model\SubscriberFactory */
  32. private $_subscriberFactory;
  33. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Block\Form\Register */
  34. private $_formRegister;
  35. /** @var Info */
  36. private $_block;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Helper\Session\CurrentCustomer
  39. */
  40. protected $currentCustomer;
  41. protected function setUp()
  42. {
  43. $this->currentCustomer = $this->createMock(\Magento\Customer\Helper\Session\CurrentCustomer::class);
  44. $urlBuilder = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class, [], '', false);
  45. $urlBuilder->expects($this->any())->method('getUrl')->will($this->returnValue(self::CHANGE_PASSWORD_URL));
  46. $layout = $this->getMockForAbstractClass(\Magento\Framework\View\LayoutInterface::class, [], '', false);
  47. $this->_formRegister = $this->createMock(\Magento\Customer\Block\Form\Register::class);
  48. $layout->expects($this->any())
  49. ->method('getBlockSingleton')
  50. ->with(\Magento\Customer\Block\Form\Register::class)
  51. ->will($this->returnValue($this->_formRegister));
  52. $this->_context = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
  53. ->disableOriginalConstructor()->getMock();
  54. $this->_context->expects($this->once())->method('getUrlBuilder')->will($this->returnValue($urlBuilder));
  55. $this->_context->expects($this->once())->method('getLayout')->will($this->returnValue($layout));
  56. $this->_customerSession = $this->createMock(\Magento\Customer\Model\Session::class);
  57. $this->_customerSession->expects($this->any())->method('getId')->will($this->returnValue(self::CUSTOMER_ID));
  58. $this->_customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  59. $this->_customer->expects($this->any())->method('getEmail')->will($this->returnValue(self::EMAIL_ADDRESS));
  60. $this->_helperView = $this->getMockBuilder(
  61. \Magento\Customer\Helper\View::class
  62. )->disableOriginalConstructor()->getMock();
  63. $this->_subscriberFactory = $this->createPartialMock(
  64. \Magento\Newsletter\Model\SubscriberFactory::class,
  65. ['create']
  66. );
  67. $this->_subscriber = $this->createMock(\Magento\Newsletter\Model\Subscriber::class);
  68. $this->_subscriber->expects($this->any())->method('loadByEmail')->will($this->returnSelf());
  69. $this->_subscriberFactory->expects($this->any())
  70. ->method('create')
  71. ->will($this->returnValue($this->_subscriber));
  72. $this->_block = new \Magento\Customer\Block\Account\Dashboard\Info(
  73. $this->_context,
  74. $this->currentCustomer,
  75. $this->_subscriberFactory,
  76. $this->_helperView
  77. );
  78. }
  79. public function testGetCustomer()
  80. {
  81. $this->currentCustomer->expects($this->once())
  82. ->method('getCustomer')
  83. ->will($this->returnValue($this->_customer));
  84. $customer = $this->_block->getCustomer();
  85. $this->assertEquals($customer, $this->_customer);
  86. }
  87. public function testGetCustomerException()
  88. {
  89. $this->currentCustomer->expects($this->once())
  90. ->method('getCustomer')
  91. ->will(
  92. $this->throwException(new NoSuchEntityException(
  93. __(
  94. 'No such entity with %fieldName = %fieldValue',
  95. ['fieldName' => 'customerId', 'fieldValue' => 1]
  96. )
  97. ))
  98. );
  99. $this->assertNull($this->_block->getCustomer());
  100. }
  101. public function testGetName()
  102. {
  103. $expectedValue = 'John Q Doe Jr';
  104. $this->currentCustomer->expects($this->once())
  105. ->method('getCustomer')
  106. ->will($this->returnValue($this->_customer));
  107. /**
  108. * Called three times, once for each attribute (i.e. prefix, middlename, and suffix)
  109. */
  110. $this->_helperView->expects($this->any())->method('getCustomerName')->will($this->returnValue($expectedValue));
  111. $this->assertEquals($expectedValue, $this->_block->getName());
  112. }
  113. public function testGetChangePasswordUrl()
  114. {
  115. $this->assertEquals(self::CHANGE_PASSWORD_URL, $this->_block->getChangePasswordUrl());
  116. }
  117. public function testGetSubscriptionObject()
  118. {
  119. $this->assertSame($this->_subscriber, $this->_block->getSubscriptionObject());
  120. }
  121. /**
  122. * @param bool $isSubscribed Is the subscriber subscribed?
  123. * @param bool $expectedValue The expected value - Whether the subscriber is subscribed or not.
  124. *
  125. * @dataProvider getIsSubscribedProvider
  126. */
  127. public function testGetIsSubscribed($isSubscribed, $expectedValue)
  128. {
  129. $this->_subscriber->expects($this->once())->method('isSubscribed')->will($this->returnValue($isSubscribed));
  130. $this->assertEquals($expectedValue, $this->_block->getIsSubscribed());
  131. }
  132. /**
  133. * @return array
  134. */
  135. public function getIsSubscribedProvider()
  136. {
  137. return [[true, true], [false, false]];
  138. }
  139. /**
  140. * @param bool $isNewsletterEnabled Determines if the newsletter is enabled
  141. * @param bool $expectedValue The expected value - Whether the newsletter is enabled or not
  142. *
  143. * @dataProvider isNewsletterEnabledProvider
  144. */
  145. public function testIsNewsletterEnabled($isNewsletterEnabled, $expectedValue)
  146. {
  147. $this->_formRegister->expects($this->once())
  148. ->method('isNewsletterEnabled')
  149. ->will($this->returnValue($isNewsletterEnabled));
  150. $this->assertEquals($expectedValue, $this->_block->isNewsletterEnabled());
  151. }
  152. /**
  153. * @return array
  154. */
  155. public function isNewsletterEnabledProvider()
  156. {
  157. return [[true, true], [false, false]];
  158. }
  159. }