|
| 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\Customer\Test\Unit\CustomerData; |
| 10 | + |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 12 | +use Magento\Customer\CustomerData\Customer as CustomerData; |
| 13 | +use Magento\Customer\Helper\Session\CurrentCustomer; |
| 14 | +use Magento\Customer\Helper\View; |
| 15 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class CustomerTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var ObjectManagerHelper |
| 22 | + */ |
| 23 | + private $objectManagerHelper; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var CustomerData |
| 27 | + */ |
| 28 | + private $customerData; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var CurrentCustomer |
| 32 | + */ |
| 33 | + private $currentCustomerMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var View |
| 37 | + */ |
| 38 | + private $customerViewHelperMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * Setup environment to test |
| 42 | + */ |
| 43 | + protected function setUp() |
| 44 | + { |
| 45 | + $this->currentCustomerMock = $this->createMock(CurrentCustomer::class); |
| 46 | + $this->customerViewHelperMock = $this->createMock(View::class); |
| 47 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 48 | + |
| 49 | + $this->customerData = $this->objectManagerHelper->getObject( |
| 50 | + CustomerData::class, |
| 51 | + [ |
| 52 | + 'currentCustomer' => $this->currentCustomerMock, |
| 53 | + 'customerViewHelper' => $this->customerViewHelperMock |
| 54 | + ] |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test getSectionData() without customer Id |
| 60 | + */ |
| 61 | + public function testGetSectionDataWithoutCustomerId() |
| 62 | + { |
| 63 | + $this->currentCustomerMock->expects($this->any())->method('getCustomerId')->willReturn(null); |
| 64 | + $this->assertEquals([], $this->customerData->getSectionData()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test getSectionData() with customer |
| 69 | + */ |
| 70 | + public function testGetSectionDataWithCustomer() |
| 71 | + { |
| 72 | + $this->currentCustomerMock->expects($this->any())->method('getCustomerId')->willReturn(1); |
| 73 | + $customerMock = $this->createMock(CustomerInterface::class); |
| 74 | + $customerMock->expects($this->any())->method('getFirstname')->willReturn('John'); |
| 75 | + $customerMock->expects($this->any())->method('getWebsiteId')->willReturn(1); |
| 76 | + $this->currentCustomerMock->expects($this->any())->method('getCustomer')->willReturn($customerMock); |
| 77 | + $this->customerViewHelperMock->expects($this->any())->method('getCustomerName') |
| 78 | + ->with($customerMock) |
| 79 | + ->willReturn('John Adam'); |
| 80 | + |
| 81 | + $this->assertEquals( |
| 82 | + [ |
| 83 | + 'fullname' => 'John Adam', |
| 84 | + 'firstname' => 'John', |
| 85 | + 'websiteId' => 1, |
| 86 | + ], |
| 87 | + $this->customerData->getSectionData() |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
0 commit comments