|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Tests\Sylius\PayPalPlugin\DependencyInjection; |
| 15 | + |
| 16 | +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; |
| 17 | +use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension; |
| 18 | + |
| 19 | +final class SyliusPayPalExtensionTest extends AbstractExtensionTestCase |
| 20 | +{ |
| 21 | + private ?string $originalSandboxEnv = null; |
| 22 | + |
| 23 | + private ?string $originalLoggingEnv = null; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + $this->saveEnvVars(); |
| 29 | + $this->clearEnvVars(); |
| 30 | + } |
| 31 | + |
| 32 | + protected function tearDown(): void |
| 33 | + { |
| 34 | + $this->restoreEnvVars(); |
| 35 | + parent::tearDown(); |
| 36 | + } |
| 37 | + |
| 38 | + protected function getContainerExtensions(): array |
| 39 | + { |
| 40 | + return [new SyliusPayPalExtension()]; |
| 41 | + } |
| 42 | + |
| 43 | + public function test_it_loads_services_on_load(): void |
| 44 | + { |
| 45 | + $this->expectNotToPerformAssertions(); |
| 46 | + |
| 47 | + $this->load(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @dataProvider sandboxModeParametersProvider |
| 52 | + */ |
| 53 | + public function test_it_sets_parameters_based_on_sandbox_mode( |
| 54 | + bool $sandboxEnabled, |
| 55 | + string $parameterName, |
| 56 | + string $expectedValue, |
| 57 | + ): void { |
| 58 | + $this->load(['sandbox' => $sandboxEnabled]); |
| 59 | + |
| 60 | + $this->assertContainerBuilderHasParameter($parameterName, $expectedValue); |
| 61 | + } |
| 62 | + |
| 63 | + public static function sandboxModeParametersProvider(): iterable |
| 64 | + { |
| 65 | + yield 'production mode api base url' => [ |
| 66 | + false, |
| 67 | + 'sylius_paypal.api_base_url', |
| 68 | + 'https://api.paypal.com/', |
| 69 | + ]; |
| 70 | + |
| 71 | + yield 'sandbox mode api base url' => [ |
| 72 | + true, |
| 73 | + 'sylius_paypal.api_base_url', |
| 74 | + 'https://api.sandbox.paypal.com/', |
| 75 | + ]; |
| 76 | + |
| 77 | + yield 'production mode facilitator url' => [ |
| 78 | + false, |
| 79 | + 'sylius_paypal.facilitator_url', |
| 80 | + 'https://prod.paypal.sylius.com', |
| 81 | + ]; |
| 82 | + |
| 83 | + yield 'sandbox mode facilitator url' => [ |
| 84 | + true, |
| 85 | + 'sylius_paypal.facilitator_url', |
| 86 | + 'https://paypal.sylius.com', |
| 87 | + ]; |
| 88 | + |
| 89 | + yield 'production mode sftp host' => [ |
| 90 | + false, |
| 91 | + 'sylius.pay_pal.reports_sftp_host', |
| 92 | + 'reports.paypal.com', |
| 93 | + ]; |
| 94 | + |
| 95 | + yield 'sandbox mode sftp host' => [ |
| 96 | + true, |
| 97 | + 'sylius.pay_pal.reports_sftp_host', |
| 98 | + 'reports.sandbox.paypal.com', |
| 99 | + ]; |
| 100 | + |
| 101 | + yield 'production mode aliased facilitator url' => [ |
| 102 | + false, |
| 103 | + 'sylius.pay_pal.facilitator_url', |
| 104 | + 'https://prod.paypal.sylius.com', |
| 105 | + ]; |
| 106 | + |
| 107 | + yield 'sandbox mode aliased facilitator url' => [ |
| 108 | + true, |
| 109 | + 'sylius.pay_pal.facilitator_url', |
| 110 | + 'https://paypal.sylius.com', |
| 111 | + ]; |
| 112 | + |
| 113 | + yield 'production mode aliased api base url' => [ |
| 114 | + false, |
| 115 | + 'sylius.pay_pal.api_base_url', |
| 116 | + 'https://api.paypal.com/', |
| 117 | + ]; |
| 118 | + |
| 119 | + yield 'sandbox mode aliased api base url' => [ |
| 120 | + true, |
| 121 | + 'sylius.pay_pal.api_base_url', |
| 122 | + 'https://api.sandbox.paypal.com/', |
| 123 | + ]; |
| 124 | + |
| 125 | + yield 'production mode aliased sftp host' => [ |
| 126 | + false, |
| 127 | + 'sylius.pay_pal.reports_sftp_host', |
| 128 | + 'reports.paypal.com', |
| 129 | + ]; |
| 130 | + |
| 131 | + yield 'sandbox mode aliased sftp host' => [ |
| 132 | + true, |
| 133 | + 'sylius.pay_pal.reports_sftp_host', |
| 134 | + 'reports.sandbox.paypal.com', |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @dataProvider environmentVariablesProvider |
| 140 | + */ |
| 141 | + public function test_it_respects_environment_variables( |
| 142 | + string $envVarName, |
| 143 | + string $envVarValue, |
| 144 | + string $parameterName, |
| 145 | + mixed $expectedValue, |
| 146 | + ): void { |
| 147 | + $this->setEnvVar($envVarName, $envVarValue); |
| 148 | + |
| 149 | + $this->load(); |
| 150 | + |
| 151 | + $this->assertContainerBuilderHasParameter($parameterName, $expectedValue); |
| 152 | + } |
| 153 | + |
| 154 | + public static function environmentVariablesProvider(): iterable |
| 155 | + { |
| 156 | + yield 'sandbox mode disabled via env' => [ |
| 157 | + 'SYLIUS_PAYPAL_SANDBOX_ENABLED', |
| 158 | + 'false', |
| 159 | + 'sylius_paypal.api_base_url', |
| 160 | + 'https://api.paypal.com/', |
| 161 | + ]; |
| 162 | + |
| 163 | + yield 'logging increased enabled via env' => [ |
| 164 | + 'SYLIUS_PAYPAL_LOGGING_INCREASED', |
| 165 | + 'true', |
| 166 | + 'sylius_paypal.logging.increased', |
| 167 | + true, |
| 168 | + ]; |
| 169 | + } |
| 170 | + |
| 171 | + private function saveEnvVars(): void |
| 172 | + { |
| 173 | + $this->originalSandboxEnv = $_ENV['SYLIUS_PAYPAL_SANDBOX_ENABLED'] ?? null; |
| 174 | + $this->originalLoggingEnv = $_ENV['SYLIUS_PAYPAL_LOGGING_INCREASED'] ?? null; |
| 175 | + } |
| 176 | + |
| 177 | + private function clearEnvVars(): void |
| 178 | + { |
| 179 | + unset($_ENV['SYLIUS_PAYPAL_SANDBOX_ENABLED']); |
| 180 | + unset($_ENV['SYLIUS_PAYPAL_LOGGING_INCREASED']); |
| 181 | + } |
| 182 | + |
| 183 | + private function restoreEnvVars(): void |
| 184 | + { |
| 185 | + if ($this->originalSandboxEnv !== null) { |
| 186 | + $_ENV['SYLIUS_PAYPAL_SANDBOX_ENABLED'] = $this->originalSandboxEnv; |
| 187 | + } else { |
| 188 | + unset($_ENV['SYLIUS_PAYPAL_SANDBOX_ENABLED']); |
| 189 | + } |
| 190 | + if ($this->originalLoggingEnv !== null) { |
| 191 | + $_ENV['SYLIUS_PAYPAL_LOGGING_INCREASED'] = $this->originalLoggingEnv; |
| 192 | + } else { |
| 193 | + unset($_ENV['SYLIUS_PAYPAL_LOGGING_INCREASED']); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private function setEnvVar(string $name, string $value): void |
| 198 | + { |
| 199 | + $_ENV[$name] = $value; |
| 200 | + } |
| 201 | +} |
0 commit comments