|
| 1 | +<?php |
| 2 | +/************************************************************************* |
| 3 | + * ADOBE CONFIDENTIAL |
| 4 | + * ___________________ |
| 5 | + * |
| 6 | + * Copyright 2024 Adobe |
| 7 | + * All Rights Reserved. |
| 8 | + * |
| 9 | + * NOTICE: All information contained herein is, and remains |
| 10 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 11 | + * and technical concepts contained herein are proprietary to Adobe |
| 12 | + * and its suppliers and are protected by all applicable intellectual |
| 13 | + * property laws, including trade secret and copyright laws. |
| 14 | + * Dissemination of this information or reproduction of this material |
| 15 | + * is strictly forbidden unless prior written permission is obtained |
| 16 | + * from Adobe. |
| 17 | + ************************************************************************* |
| 18 | + */ |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace Magento\CustomerImportExport\Test\Unit\Model\Import\Customer; |
| 22 | + |
| 23 | +use Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\Storage; |
| 24 | +use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory; |
| 25 | +use Magento\Customer\Model\ResourceModel\Customer\Collection; |
| 26 | +use Magento\Customer\Model\Config\Share; |
| 27 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 28 | +use Magento\Framework\DB\Select; |
| 29 | +use PHPUnit\Framework\MockObject\Exception; |
| 30 | +use PHPUnit\Framework\TestCase; |
| 31 | +use ReflectionClass; |
| 32 | +use ReflectionException; |
| 33 | + |
| 34 | +class StorageTest extends TestCase |
| 35 | +{ |
| 36 | + /** |
| 37 | + * @var Storage |
| 38 | + */ |
| 39 | + private Storage $storage; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Collection|MockObject |
| 43 | + */ |
| 44 | + private mixed $customerCollectionMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Share|MockObject |
| 48 | + */ |
| 49 | + private mixed $configShareMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var AdapterInterface|MockObject |
| 53 | + */ |
| 54 | + private mixed $connectionMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritdoc |
| 58 | + * @throws Exception |
| 59 | + */ |
| 60 | + protected function setUp(): void |
| 61 | + { |
| 62 | + $collectionFactoryMock = $this->createMock(CollectionFactory::class); |
| 63 | + $this->customerCollectionMock = $this->createMock(Collection::class); |
| 64 | + $this->configShareMock = $this->createMock(Share::class); |
| 65 | + $this->connectionMock = $this->createMock(AdapterInterface::class); |
| 66 | + |
| 67 | + $collectionFactoryMock->method('create') |
| 68 | + ->willReturn($this->customerCollectionMock); |
| 69 | + |
| 70 | + $this->storage = new Storage( |
| 71 | + $collectionFactoryMock, |
| 72 | + $this->configShareMock, |
| 73 | + ['page_size' => 4] |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test loadCustomersData method when the scope is set to global. |
| 79 | + * |
| 80 | + * @throws Exception|ReflectionException |
| 81 | + */ |
| 82 | + public function testLoadCustomersData() |
| 83 | + { |
| 84 | + $customerIdentifiers = [ |
| 85 | + 'test@example.com_2' => ['email' => 'test@example.com', 'website_id' => 2], |
| 86 | + 'test@example.com_3' => ['email' => 'test@example.com', 'website_id' => 3], |
| 87 | + 'test@example.com_4' => ['email' => 'test@example.com', 'website_id' => 4], |
| 88 | + 'test@example.com_5' => ['email' => 'test@example.com', 'website_id' => 5] |
| 89 | + ]; |
| 90 | + |
| 91 | + $selectMock = $this->createMock(Select::class); |
| 92 | + $selectMock->expects($this->once()) |
| 93 | + ->method('getPart') |
| 94 | + ->willReturn(['main_table' => 'customer_entity']); |
| 95 | + |
| 96 | + $this->customerCollectionMock->expects($this->once()) |
| 97 | + ->method('getSelect') |
| 98 | + ->willReturn($selectMock); |
| 99 | + $connectionMock = $this->getConnectionMock(); |
| 100 | + $this->customerCollectionMock |
| 101 | + ->expects($this->once()) |
| 102 | + ->method('getConnection') |
| 103 | + ->willReturn($connectionMock); |
| 104 | + |
| 105 | + $this->configShareMock->expects($this->once()) |
| 106 | + ->method('isGlobalScope') |
| 107 | + ->willReturn(true); |
| 108 | + |
| 109 | + $reflection = new ReflectionClass($this->storage); |
| 110 | + $customerIdsProperty = $reflection->getProperty('_customerIds'); |
| 111 | + $loadCustomersDataMethod = $reflection->getMethod('loadCustomersData'); |
| 112 | + $loadCustomersDataMethod->setAccessible(true); |
| 113 | + $loadCustomersDataMethod->invokeArgs($this->storage, [$customerIdentifiers]); |
| 114 | + $customerIds = $customerIdsProperty->getValue($this->storage); |
| 115 | + $this->assertArrayHasKey('test@example.com', $customerIds); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Mock DB connection and return customer data's |
| 120 | + * |
| 121 | + * @return AdapterInterface|MockObject |
| 122 | + * @throws Exception |
| 123 | + */ |
| 124 | + private function getConnectionMock(): AdapterInterface|MockObject |
| 125 | + { |
| 126 | + $customerData = [ |
| 127 | + 'email' => 'test@example.com', |
| 128 | + 'website_id' => 1, |
| 129 | + 'entity_id' => 1, |
| 130 | + 'store_id' => 1 |
| 131 | + ]; |
| 132 | + $this->connectionMock->expects($this->once()) |
| 133 | + ->method('fetchAll')->willReturn([$customerData]); |
| 134 | + |
| 135 | + return $this->connectionMock; |
| 136 | + } |
| 137 | +} |
0 commit comments