IdentifierTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\CustomerData\Section;
  7. use \Magento\Customer\CustomerData\Section\Identifier;
  8. class IdentifierTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Customer\CustomerData\Section\Identifier
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $cookieManMock;
  18. /**
  19. * @var string
  20. */
  21. protected $cookieMarkId;
  22. protected function setUp()
  23. {
  24. $this->cookieManMock = $this->createMock(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class);
  25. $this->cookieMarkId = '123456';
  26. $this->model = new Identifier(
  27. $this->cookieManMock
  28. );
  29. }
  30. public function testInitMark()
  31. {
  32. $this->cookieManMock->expects($this->once())
  33. ->method('getCookie')
  34. ->with(Identifier::COOKIE_KEY)
  35. ->willReturn($this->cookieMarkId);
  36. $this->assertEquals($this->cookieMarkId, $this->model->initMark(false));
  37. }
  38. public function testMarkSectionsDontUpdate()
  39. {
  40. $sectionsData = [
  41. 'section1' => [1],
  42. 'section2' => [2],
  43. 'section3' => [3],
  44. ];
  45. $expectedData = [
  46. 'section1' => [1, 'data_id' => $this->cookieMarkId],
  47. 'section2' => [2, 'data_id' => $this->cookieMarkId],
  48. 'section3' => [3],
  49. ];
  50. $sectionNames = ['section1', 'section2'];
  51. $this->cookieManMock->expects($this->once())
  52. ->method('getCookie')
  53. ->with(Identifier::COOKIE_KEY)
  54. ->willReturn($this->cookieMarkId);
  55. // third parameter is true to avoid diving deeply into initMark()
  56. $result = $this->model->markSections($sectionsData, $sectionNames, false);
  57. $this->assertEquals($expectedData, $result);
  58. }
  59. public function testMarkSectionsUpdate()
  60. {
  61. $sectionsData = [
  62. 'section1' => [1, 'data_id' => 0],
  63. 'section2' => [2, 'data_id' => 0],
  64. 'section3' => [3],
  65. ];
  66. $sectionNames = ['section1', 'section2'];
  67. // third parameter is true to avoid diving deeply into initMark()
  68. $result = $this->model->markSections($sectionsData, $sectionNames, true);
  69. $this->assertArrayHasKey('data_id', $result['section1']);
  70. $this->assertNotEquals(0, $result['section1']['data_id']);
  71. $this->assertArrayHasKey('data_id', $result['section2']);
  72. $this->assertNotEquals(0, $result['section2']['data_id']);
  73. }
  74. }