|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\DataExporter\Test\Integration\Uuid; |
| 9 | + |
| 10 | +use Magento\DataExporter\Uuid\UuidManager; |
| 11 | +use Magento\TestFramework\Helper\Bootstrap; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class UuidManagerTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Magento\Framework\ObjectManagerInterface|mixed |
| 18 | + */ |
| 19 | + private $objectManager; |
| 20 | + |
| 21 | + /** |
| 22 | + * @inheritdoc |
| 23 | + */ |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param array $entityIds |
| 32 | + * @param string $type |
| 33 | + * @return void |
| 34 | + * @dataProvider happyPathDataProvider |
| 35 | + */ |
| 36 | + public function testHappyPath(array $entityIds, string $type): void |
| 37 | + { |
| 38 | + /** @var UuidManager $uuidManager */ |
| 39 | + $uuidManager = $this->objectManager->create(UuidManager::class); |
| 40 | + |
| 41 | + if (count($entityIds) === 1) { |
| 42 | + $uuidManager->assign(current($entityIds), $type); |
| 43 | + } else { |
| 44 | + $uuidManager->assignBulk($entityIds, $type); |
| 45 | + } |
| 46 | + foreach ($entityIds as $entityId) { |
| 47 | + self::assertTrue( |
| 48 | + $uuidManager->isAssigned($entityId, $type), |
| 49 | + 'uuid is not assigned for ' . $entityId . ' ' . $type |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function testUuidAssignedOnlyOnce(): void |
| 58 | + { |
| 59 | + /** @var UuidManager $uuidManager */ |
| 60 | + $uuidManager = $this->objectManager->create(UuidManager::class); |
| 61 | + |
| 62 | + $assignedUuid = $uuidManager->assign(1, 'just-once'); |
| 63 | + self::assertNotEmpty($assignedUuid); |
| 64 | + $assignedUuidRepeat = $uuidManager->assign(1, 'just-once'); |
| 65 | + self::assertEquals($assignedUuid, $assignedUuidRepeat); |
| 66 | + |
| 67 | + $assignedUuids = $uuidManager->assignBulk([3, 4], 'just-once'); |
| 68 | + $assignedUuidsRepeat = $uuidManager->assignBulk([4, 3, 4, 3], 'just-once'); |
| 69 | + |
| 70 | + self::assertCount(2, $assignedUuids); |
| 71 | + self::assertEquals($assignedUuids, $assignedUuidsRepeat); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function testUuidDuplicatesGenerated(): void |
| 78 | + { |
| 79 | + $uuidGeneratorMock = $this->createMock(\Magento\Framework\DataObject\IdentityService::class); |
| 80 | + /** @var UuidManager $uuidManager */ |
| 81 | + $uuidManager = $this->objectManager->create(UuidManager::class, ['identityService' => $uuidGeneratorMock]); |
| 82 | + $uuidGeneratorMock->method('generateId')->willReturn('uuid'); |
| 83 | + |
| 84 | + $this->expectException(\Magento\DataExporter\Uuid\UuidSaveException::class); |
| 85 | + $this->expectExceptionMessage('Failed to assign UUID for type: test-type, ids: 8,9. duplicates: uuid'); |
| 86 | + |
| 87 | + $uuidManager->assignBulk([8, 9], 'test-type'); |
| 88 | + |
| 89 | + } |
| 90 | + /** |
| 91 | + * @return array[] |
| 92 | + */ |
| 93 | + public function happyPathDataProvider(): array |
| 94 | + { |
| 95 | + return [ |
| 96 | + [ |
| 97 | + [1], //entityIds |
| 98 | + 'test-type', // type |
| 99 | + ], |
| 100 | + [ |
| 101 | + [1, 2], //entityIds |
| 102 | + 'test-type', // type |
| 103 | + ], |
| 104 | + [ |
| 105 | + [3, 4], //entityIds |
| 106 | + 'test-type', // type |
| 107 | + ], |
| 108 | + [ |
| 109 | + [4, 4, 5, 4, 5, 5], //entityIds |
| 110 | + 'test-type', // type |
| 111 | + ] |
| 112 | + ]; |
| 113 | + } |
| 114 | +} |
0 commit comments