|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenForgeProject\MageForge\Test\Unit\Service; |
| 6 | + |
| 7 | +use Magento\Framework\Component\ComponentRegistrar; |
| 8 | +use Magento\Framework\Component\ComponentRegistrarInterface; |
| 9 | +use Magento\Framework\Filesystem\DirectoryList; |
| 10 | +use OpenForgeProject\MageForge\Service\VendorFileMapper; |
| 11 | +use PHPUnit\Framework\MockObject\MockObject; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class VendorFileMapperTest extends TestCase |
| 15 | +{ |
| 16 | + private VendorFileMapper $mapper; |
| 17 | + private ComponentRegistrarInterface|MockObject $componentRegistrarMock; |
| 18 | + private DirectoryList|MockObject $directoryListMock; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + $this->componentRegistrarMock = $this->getMockBuilder(ComponentRegistrarInterface::class) |
| 23 | + ->getMock(); |
| 24 | + |
| 25 | + $this->directoryListMock = $this->getMockBuilder(DirectoryList::class) |
| 26 | + ->disableOriginalConstructor() |
| 27 | + ->getMock(); |
| 28 | + |
| 29 | + // Default root path for tests |
| 30 | + $this->directoryListMock->method('getRoot') |
| 31 | + ->willReturn('/var/www/html'); |
| 32 | + |
| 33 | + $this->mapper = new VendorFileMapper( |
| 34 | + $this->componentRegistrarMock, |
| 35 | + $this->directoryListMock |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public function testMapToThemePathWithStandardModule(): void |
| 40 | + { |
| 41 | + $sourceFile = 'vendor/magento/module-catalog/view/frontend/templates/product/list.phtml'; |
| 42 | + $themePath = 'app/design/frontend/My/Theme'; |
| 43 | + |
| 44 | + // Mock ComponentRegistrar to find the module |
| 45 | + $this->componentRegistrarMock->expects($this->once()) |
| 46 | + ->method('getPaths') |
| 47 | + ->with(ComponentRegistrar::MODULE) |
| 48 | + ->willReturn([ |
| 49 | + 'Magento_Catalog' => '/var/www/html/vendor/magento/module-catalog' |
| 50 | + ]); |
| 51 | + |
| 52 | + $result = $this->mapper->mapToThemePath($sourceFile, $themePath); |
| 53 | + |
| 54 | + $this->assertEquals( |
| 55 | + 'app/design/frontend/My/Theme/Magento_Catalog/templates/product/list.phtml', |
| 56 | + $result |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + public function testMapToThemePathWithNestedCompatModule(): void |
| 61 | + { |
| 62 | + // Path simulates a Hyvä Compat module with nested target module folder |
| 63 | + $sourceFile = 'vendor/mollie/magento2-hyva-compatibility/src/Mollie_HyvaCompatibility/view/frontend/templates/Mollie_Payment/product/view/applepay.phtml'; |
| 64 | + $themePath = 'app/design/frontend/My/Theme'; |
| 65 | + |
| 66 | + // Regex detection should prioritize this, so ComponentRegistrar might not even be called |
| 67 | + // or if called, it shouldn't matter for the logic flow if regex matches first. |
| 68 | + |
| 69 | + $result = $this->mapper->mapToThemePath($sourceFile, $themePath); |
| 70 | + |
| 71 | + $this->assertEquals( |
| 72 | + 'app/design/frontend/My/Theme/Mollie_Payment/templates/product/view/applepay.phtml', |
| 73 | + $result |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + public function testMapToThemePathWithVendorTheme(): void |
| 78 | + { |
| 79 | + // Path simulates a Vendor Theme (e.g. Hyva Default) |
| 80 | + $sourceFile = 'vendor/hyva-themes/magento2-default-theme/Magento_GiftMessage/templates/php-cart/gift-options-body.phtml'; |
| 81 | + $themePath = 'app/design/frontend/My/Theme'; |
| 82 | + |
| 83 | + $result = $this->mapper->mapToThemePath($sourceFile, $themePath); |
| 84 | + |
| 85 | + $this->assertEquals( |
| 86 | + 'app/design/frontend/My/Theme/Magento_GiftMessage/templates/php-cart/gift-options-body.phtml', |
| 87 | + $result |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + public function testMapToThemePathWithAbsolutePaths(): void |
| 92 | + { |
| 93 | + $sourceFile = '/var/www/html/vendor/magento/module-customer/view/frontend/templates/account/dashboard.phtml'; |
| 94 | + // Note: passing absolute theme path here |
| 95 | + $themePath = '/var/www/html/app/design/frontend/Vendor/Theme'; |
| 96 | + |
| 97 | + $this->componentRegistrarMock->method('getPaths') |
| 98 | + ->with(ComponentRegistrar::MODULE) |
| 99 | + ->willReturn([ |
| 100 | + 'Magento_Customer' => '/var/www/html/vendor/magento/module-customer' |
| 101 | + ]); |
| 102 | + |
| 103 | + $result = $this->mapper->mapToThemePath($sourceFile, $themePath); |
| 104 | + |
| 105 | + // Expect absolute path return because input theme path was absolute (logic prepends theme path) |
| 106 | + $this->assertEquals( |
| 107 | + '/var/www/html/app/design/frontend/Vendor/Theme/Magento_Customer/templates/account/dashboard.phtml', |
| 108 | + $result |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + public function testThrowsExceptionIfModuleNotFound(): void |
| 113 | + { |
| 114 | + $this->expectException(\RuntimeException::class); |
| 115 | + |
| 116 | + $sourceFile = 'vendor/unknown/package/somefile.txt'; |
| 117 | + $themePath = 'app/design/frontend/My/Theme'; |
| 118 | + |
| 119 | + $this->componentRegistrarMock->method('getPaths') |
| 120 | + ->willReturn([]); // No modules registered |
| 121 | + |
| 122 | + $this->mapper->mapToThemePath($sourceFile, $themePath); |
| 123 | + } |
| 124 | +} |
0 commit comments