|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2024 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\Catalog\Test\Unit\Model\ResourceModel; |
| 20 | + |
| 21 | +use Exception; |
| 22 | +use Magento\Catalog\Model\ResourceModel\CatalogCategoryAndProductResolverOnSingleStoreMode as Resolver; |
| 23 | +use Magento\Framework\App\ResourceConnection; |
| 24 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 25 | +use Magento\Framework\DB\Select; |
| 26 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 27 | +use PHPUnit\Framework\MockObject\MockObject; |
| 28 | +use PHPUnit\Framework\TestCase; |
| 29 | + |
| 30 | +class CatalogCategoryAndProductResolverOnSingleStoreModeTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var Resolver |
| 34 | + */ |
| 35 | + private $model; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ResourceConnection|MockObject |
| 39 | + */ |
| 40 | + private $resourceConnectionMock; |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + $objectManager = new ObjectManager($this); |
| 45 | + $this->resourceConnectionMock = $this->createMock(ResourceConnection::class); |
| 46 | + |
| 47 | + $this->model = $objectManager->getObject( |
| 48 | + Resolver::class, |
| 49 | + [ |
| 50 | + 'resourceConnection' => $this->resourceConnectionMock |
| 51 | + ] |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test Migrate catalog category and product tables without exception |
| 57 | + */ |
| 58 | + public function testCheckMigrateCatalogCategoryAndProductTablesWithoutException(): void |
| 59 | + { |
| 60 | + $catalogProducts = [ |
| 61 | + [ |
| 62 | + 'id' => 1, |
| 63 | + 'name' => 'simple1', |
| 64 | + 'category_id' => '1', |
| 65 | + 'website_id' => '2' |
| 66 | + ], |
| 67 | + [ |
| 68 | + 'id' => 2, |
| 69 | + 'name' => 'simple2', |
| 70 | + 'category_id' => '1', |
| 71 | + 'website_id' => '2' |
| 72 | + ], |
| 73 | + [ |
| 74 | + 'id' => 3, |
| 75 | + 'name' => 'bundle1', |
| 76 | + 'category_id' => '1', |
| 77 | + 'website_id' => '2' |
| 78 | + ] |
| 79 | + ]; |
| 80 | + $connection = $this->getConnection(); |
| 81 | + $connection->expects($this->any())->method('fetchCol')->willReturn($catalogProducts); |
| 82 | + $connection->expects($this->any())->method('delete')->willReturnSelf(); |
| 83 | + $connection->expects($this->any())->method('update')->willReturnSelf(); |
| 84 | + $connection->expects($this->any())->method('commit')->willReturnSelf(); |
| 85 | + |
| 86 | + $this->model->migrateCatalogCategoryAndProductTables(1); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test Migrate catalog category and product tables with exception |
| 91 | + */ |
| 92 | + public function testCheckMigrateCatalogCategoryAndProductTablesWithException(): void |
| 93 | + { |
| 94 | + $exceptionMessage = 'Exception message'; |
| 95 | + $connection = $this->getConnection(); |
| 96 | + $connection->expects($this->any()) |
| 97 | + ->method('fetchCol') |
| 98 | + ->willThrowException(new Exception($exceptionMessage)); |
| 99 | + $connection->expects($this->any())->method('rollBack')->willReturnSelf(); |
| 100 | + $this->model->migrateCatalogCategoryAndProductTables(1); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get connection |
| 105 | + * |
| 106 | + * @return MockObject |
| 107 | + */ |
| 108 | + private function getConnection(): MockObject |
| 109 | + { |
| 110 | + $connection = $this->getMockForAbstractClass(AdapterInterface::class); |
| 111 | + $this->resourceConnectionMock |
| 112 | + ->expects($this->any()) |
| 113 | + ->method('getConnection') |
| 114 | + ->willReturn($connection); |
| 115 | + $connection->expects($this->once()) |
| 116 | + ->method('beginTransaction') |
| 117 | + ->willReturnSelf(); |
| 118 | + $this->resourceConnectionMock |
| 119 | + ->expects($this->any()) |
| 120 | + ->method('getTableName') |
| 121 | + ->willReturnArgument(0); |
| 122 | + |
| 123 | + $select = $this->createMock(Select::class); |
| 124 | + $select->expects($this->any())->method('from')->willReturnSelf(); |
| 125 | + $select->expects($this->any())->method('where')->willReturnSelf(); |
| 126 | + |
| 127 | + $connection->expects($this->any())->method('select')->willReturn($select); |
| 128 | + return $connection; |
| 129 | + } |
| 130 | +} |
0 commit comments