|
14 | 14 | use Magento\Framework\App\Request\Http; |
15 | 15 | use Magento\Framework\Component\ComponentRegistrarInterface; |
16 | 16 | use PHPUnit\Framework\Attributes\CoversClass; |
| 17 | +use Adyen\Payment\Gateway\Request\Header\HeaderDataBuilderInterface; |
| 18 | +use Magento\Payment\Model\InfoInterface; |
17 | 19 |
|
18 | 20 | #[CoversClass(PlatformInfo::class)] |
19 | 21 | class PlatformInfoTest extends AbstractAdyenTestCase |
@@ -73,4 +75,110 @@ public function testBuildApplicationInfo(): void |
73 | 75 | $this->assertInstanceOf(CommonField::class, $applicationInfo->getAdyenLibrary()); |
74 | 76 | $this->assertInstanceOf(CommonField::class, $applicationInfo->getAdyenPaymentSource()); |
75 | 77 | } |
| 78 | + |
| 79 | + public function testBuildRequestHeadersWithFrontendTypeInPayment(): void |
| 80 | + { |
| 81 | + $this->productMetadata->method('getName')->willReturn('Magento'); |
| 82 | + $this->productMetadata->method('getVersion')->willReturn('2.4.7'); |
| 83 | + $this->productMetadata->method('getEdition')->willReturn('Community'); |
| 84 | + |
| 85 | + $payment = $this->createMock(InfoInterface::class); |
| 86 | + $payment->method('getAdditionalInformation') |
| 87 | + ->with(HeaderDataBuilderInterface::ADDITIONAL_DATA_FRONTEND_TYPE_KEY) |
| 88 | + ->willReturn('web'); |
| 89 | + |
| 90 | + $this->componentRegistrar->method('getPath') |
| 91 | + ->with(\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Adyen_Payment') |
| 92 | + ->willReturn(__DIR__ . '/../../../'); // or mock to valid path if you want to test version logic |
| 93 | + |
| 94 | + // Mock the getModuleVersion result |
| 95 | + $platformInfo = $this->getMockBuilder(PlatformInfo::class) |
| 96 | + ->setConstructorArgs([$this->componentRegistrar, $this->productMetadata, $this->request]) |
| 97 | + ->onlyMethods(['getModuleVersion']) |
| 98 | + ->getMock(); |
| 99 | + |
| 100 | + $platformInfo->method('getModuleVersion')->willReturn('9.6.0'); |
| 101 | + |
| 102 | + $headers = $platformInfo->buildRequestHeaders($payment); |
| 103 | + |
| 104 | + $this->assertSame('Magento', $headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_NAME]); |
| 105 | + $this->assertSame('2.4.7', $headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_VERSION]); |
| 106 | + $this->assertSame('Community', $headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_EDITION]); |
| 107 | + $this->assertSame('adyen-magento2', $headers[HeaderDataBuilderInterface::MERCHANT_APPLICATION_NAME]); |
| 108 | + $this->assertSame('9.6.0', $headers[HeaderDataBuilderInterface::MERCHANT_APPLICATION_VERSION]); |
| 109 | + $this->assertSame('web', $headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE]); |
| 110 | + } |
| 111 | + |
| 112 | + public function testBuildRequestHeadersWithFallbackToRequestPath(): void |
| 113 | + { |
| 114 | + $this->productMetadata->method('getName')->willReturn('Magento'); |
| 115 | + $this->productMetadata->method('getVersion')->willReturn('2.4.7'); |
| 116 | + $this->productMetadata->method('getEdition')->willReturn('Community'); |
| 117 | + |
| 118 | + $payment = $this->createMock(InfoInterface::class); |
| 119 | + $payment->method('getAdditionalInformation') |
| 120 | + ->with(HeaderDataBuilderInterface::ADDITIONAL_DATA_FRONTEND_TYPE_KEY) |
| 121 | + ->willReturn(null); |
| 122 | + |
| 123 | + $this->request->method('getOriginalPathInfo')->willReturn('/graphql'); |
| 124 | + $this->request->method('getMethod')->willReturn('POST'); |
| 125 | + |
| 126 | + $this->componentRegistrar->method('getPath') |
| 127 | + ->with(\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Adyen_Payment') |
| 128 | + ->willReturn(__DIR__ . '/../../../'); |
| 129 | + |
| 130 | + $platformInfo = $this->getMockBuilder(PlatformInfo::class) |
| 131 | + ->setConstructorArgs([$this->componentRegistrar, $this->productMetadata, $this->request]) |
| 132 | + ->onlyMethods(['getModuleVersion']) |
| 133 | + ->getMock(); |
| 134 | + |
| 135 | + $platformInfo->method('getModuleVersion')->willReturn('9.6.0'); |
| 136 | + |
| 137 | + $headers = $platformInfo->buildRequestHeaders($payment); |
| 138 | + |
| 139 | + $this->assertSame('headless-graphql', $headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE]); |
| 140 | + } |
| 141 | + |
| 142 | + public function testGetModuleVersionWithValidComposerJson(): void |
| 143 | + { |
| 144 | + $tempDir = sys_get_temp_dir() . '/adyen_module_test_' . uniqid(); |
| 145 | + mkdir($tempDir); |
| 146 | + |
| 147 | + $composerJsonContent = json_encode(['version' => '9.7.0']); |
| 148 | + file_put_contents($tempDir . '/composer.json', $composerJsonContent); |
| 149 | + |
| 150 | + $this->componentRegistrar->method('getPath') |
| 151 | + ->with(\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Adyen_Payment') |
| 152 | + ->willReturn($tempDir); |
| 153 | + |
| 154 | + $version = $this->platformInfo->getModuleVersion(); |
| 155 | + |
| 156 | + $this->assertSame('9.7.0', $version); |
| 157 | + |
| 158 | + // Clean up |
| 159 | + unlink($tempDir . '/composer.json'); |
| 160 | + rmdir($tempDir); |
| 161 | + } |
| 162 | + |
| 163 | + public function testGetModuleVersionWithMissingVersion(): void |
| 164 | + { |
| 165 | + $tempDir = sys_get_temp_dir() . '/adyen_module_test_' . uniqid(); |
| 166 | + mkdir($tempDir); |
| 167 | + |
| 168 | + $composerJsonContent = json_encode(['name' => 'adyen/magento2']); |
| 169 | + file_put_contents($tempDir . '/composer.json', $composerJsonContent); |
| 170 | + |
| 171 | + $this->componentRegistrar->method('getPath') |
| 172 | + ->with(\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Adyen_Payment') |
| 173 | + ->willReturn($tempDir); |
| 174 | + |
| 175 | + $version = $this->platformInfo->getModuleVersion(); |
| 176 | + |
| 177 | + $this->assertSame('Version is not available in composer.json', $version); |
| 178 | + |
| 179 | + // Clean up |
| 180 | + unlink($tempDir . '/composer.json'); |
| 181 | + rmdir($tempDir); |
| 182 | + } |
| 183 | + |
76 | 184 | } |
0 commit comments