DataTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Helper;
  7. class DataTest extends \Magento\TestFramework\TestCase\AbstractController
  8. {
  9. /**
  10. * @var Data
  11. */
  12. private $_wishlistHelper;
  13. /**
  14. * @var \Magento\Framework\ObjectManagerInterface
  15. */
  16. private $objectManager;
  17. /**
  18. * @var \Magento\Customer\Model\Session
  19. */
  20. protected $_customerSession;
  21. /**
  22. * Get required instance
  23. */
  24. protected function setUp()
  25. {
  26. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  27. $this->_wishlistHelper = $this->objectManager->get(\Magento\Wishlist\Helper\Data::class);
  28. $this->_customerSession = $this->objectManager->get(\Magento\Customer\Model\Session::class);
  29. }
  30. /**
  31. * Clear wishlist helper property
  32. */
  33. protected function tearDown()
  34. {
  35. $this->_wishlistHelper = null;
  36. if ($this->_customerSession->isLoggedIn()) {
  37. $this->_customerSession->logout();
  38. }
  39. }
  40. public function testGetAddParams()
  41. {
  42. $product = $this->objectManager->get(\Magento\Catalog\Model\Product::class);
  43. $product->setId(11);
  44. $json = $this->_wishlistHelper->getAddParams($product);
  45. $params = (array)json_decode($json);
  46. $data = (array)$params['data'];
  47. $this->assertEquals('11', $data['product']);
  48. $this->assertArrayHasKey('uenc', $data);
  49. $this->assertStringEndsWith('wishlist/index/add/', $params['action']);
  50. }
  51. public function testGetMoveFromCartParams()
  52. {
  53. $json = $this->_wishlistHelper->getMoveFromCartParams(11);
  54. $params = (array)json_decode($json);
  55. $data = (array)$params['data'];
  56. $this->assertEquals('11', $data['item']);
  57. $this->assertArrayHasKey('uenc', $data);
  58. $this->assertStringEndsWith('wishlist/index/fromcart/', $params['action']);
  59. }
  60. public function testGetUpdateParams()
  61. {
  62. $product = $this->objectManager->get(\Magento\Catalog\Model\Product::class);
  63. $product->setId(11);
  64. $product->setWishlistItemId(15);
  65. $json = $this->_wishlistHelper->getUpdateParams($product);
  66. $params = (array)json_decode($json);
  67. $data = (array)$params['data'];
  68. $this->assertEquals('11', $data['product']);
  69. $this->assertEquals('15', $data['id']);
  70. $this->assertArrayHasKey('uenc', $data);
  71. $this->assertStringEndsWith('wishlist/index/updateItemOptions/', $params['action']);
  72. }
  73. /**
  74. * @magentoDataFixture Magento/Customer/_files/customer.php
  75. */
  76. public function testWishlistCustomer()
  77. {
  78. /** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
  79. $customerRepository = $this->objectManager->create(
  80. \Magento\Customer\Api\CustomerRepositoryInterface::class
  81. );
  82. $customer = $customerRepository->getById(1);
  83. $this->_wishlistHelper->setCustomer($customer);
  84. $this->assertSame($customer, $this->_wishlistHelper->getCustomer());
  85. $this->_wishlistHelper = null;
  86. /** @var \Magento\Wishlist\Helper\Data wishlistHelper */
  87. $this->_wishlistHelper = $this->objectManager->get(\Magento\Wishlist\Helper\Data::class);
  88. $this->_customerSession->loginById(1);
  89. $this->assertEquals($customer, $this->_wishlistHelper->getCustomer());
  90. /** @var \Magento\Customer\Helper\View $customerViewHelper */
  91. $customerViewHelper = $this->objectManager->create(\Magento\Customer\Helper\View::class);
  92. $this->assertEquals($customerViewHelper->getCustomerName($customer), $this->_wishlistHelper->getCustomerName());
  93. }
  94. }