|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Persistent\Test\Unit\CustomerData; |
| 10 | + |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 12 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 13 | +use Magento\Customer\Helper\View; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 15 | +use Magento\Persistent\CustomerData\Persistent; |
| 16 | +use Magento\Persistent\Helper\Session; |
| 17 | +use Magento\Persistent\Model\Session as PersistentSession; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class PersistentTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Stub customer id |
| 25 | + */ |
| 26 | + private const STUB_CUSTOMER_ID = 1; |
| 27 | + |
| 28 | + /** |
| 29 | + * Stub customer name |
| 30 | + */ |
| 31 | + private const STUB_CUSTOMER_NAME = 'Adam John'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Persistent |
| 35 | + */ |
| 36 | + private $customerData; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Session|MockObject |
| 40 | + */ |
| 41 | + private $persistentSessionHelperMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var View|MockObject |
| 45 | + */ |
| 46 | + private $customerViewHelperMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var CustomerRepositoryInterface|MockObject |
| 50 | + */ |
| 51 | + private $customerRepositoryMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * Setup environment for test |
| 55 | + */ |
| 56 | + protected function setUp() |
| 57 | + { |
| 58 | + $this->persistentSessionHelperMock = $this->createMock(Session::class); |
| 59 | + $this->customerViewHelperMock = $this->createMock(View::class); |
| 60 | + $this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class); |
| 61 | + |
| 62 | + $objectManager = new ObjectManagerHelper($this); |
| 63 | + |
| 64 | + $this->customerData = $objectManager->getObject( |
| 65 | + Persistent::class, |
| 66 | + [ |
| 67 | + 'persistentSession' => $this->persistentSessionHelperMock, |
| 68 | + 'customerViewHelper' => $this->customerViewHelperMock, |
| 69 | + 'customerRepository' => $this->customerRepositoryMock |
| 70 | + ] |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test getSectionData() when disable persistent |
| 76 | + */ |
| 77 | + public function testGetSectionDataWhenDisablePersistent() |
| 78 | + { |
| 79 | + $this->persistentSessionHelperMock->method('isPersistent')->willReturn(false); |
| 80 | + |
| 81 | + $this->assertEquals([], $this->customerData->getSectionData()); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test getSectionData() when customer doesn't login |
| 86 | + */ |
| 87 | + public function testGetSectionDataWhenCustomerNotLoggedInReturnsEmptyArray() |
| 88 | + { |
| 89 | + $this->persistentSessionHelperMock->method('isPersistent')->willReturn(true); |
| 90 | + |
| 91 | + $persistentSessionMock = $this->createPartialMock(PersistentSession::class, ['getCustomerId']); |
| 92 | + $persistentSessionMock->method('getCustomerId')->willReturn(null); |
| 93 | + $this->persistentSessionHelperMock->method('getSession')->willReturn($persistentSessionMock); |
| 94 | + |
| 95 | + $this->assertEquals([], $this->customerData->getSectionData()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Test getSectionData() when customer login and enable persistent |
| 100 | + */ |
| 101 | + public function testGetSectionDataCustomerLoginAndEnablePersistent() |
| 102 | + { |
| 103 | + $this->persistentSessionHelperMock->method('isPersistent')->willReturn(true); |
| 104 | + |
| 105 | + $persistentSessionMock = $this->createPartialMock(PersistentSession::class, ['getCustomerId']); |
| 106 | + $persistentSessionMock->method('getCustomerId')->willReturn(self::STUB_CUSTOMER_ID); |
| 107 | + $this->persistentSessionHelperMock->method('getSession')->willReturn($persistentSessionMock); |
| 108 | + |
| 109 | + $customerMock = $this->createMock(CustomerInterface::class); |
| 110 | + $this->customerRepositoryMock->method('getById')->with(self::STUB_CUSTOMER_ID)->willReturn($customerMock); |
| 111 | + $this->customerViewHelperMock->method('getCustomerName')->with($customerMock) |
| 112 | + ->willReturn(self::STUB_CUSTOMER_NAME); |
| 113 | + |
| 114 | + $this->assertEquals( |
| 115 | + [ |
| 116 | + 'fullname' => self::STUB_CUSTOMER_NAME |
| 117 | + ], |
| 118 | + $this->customerData->getSectionData() |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments