Skip to content

Commit 7773102

Browse files
committed
Add one more env-specific API test
1 parent 1d05399 commit 7773102

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

module/Rest/test-api/Middleware/CorsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use GuzzleHttp\RequestOptions;
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\Attributes\Test;
10+
use Shlinkio\Shlink\Core\Config\EnvVars;
1011
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
12+
use ShlinkioApiTest\Shlink\Rest\Utils\WithEnvVars;
1113

1214
class CorsTest extends ApiTestCase
1315
{
@@ -72,4 +74,20 @@ public static function providePreflightEndpoints(): iterable
7274
yield 'tags route' => ['/tags', 'GET,DELETE,PUT'];
7375
yield 'health route' => ['/health', 'GET'];
7476
}
77+
78+
#[Test, WithEnvVars([
79+
EnvVars::CORS_MAX_AGE->value => 5000,
80+
EnvVars::CORS_ALLOW_CREDENTIALS->value => true,
81+
EnvVars::CORS_ALLOW_ORIGIN->value => 'example.com,localhost:8000',
82+
])]
83+
public function parametersCanBeCustomized(): void
84+
{
85+
$resp = $this->callApiWithKey(self::METHOD_OPTIONS, '/short-urls', [
86+
RequestOptions::HEADERS => ['Origin' => 'example.com'],
87+
]);
88+
89+
self::assertEquals('5000', $resp->getHeaderLine('Access-Control-Max-Age'));
90+
self::assertEquals('example.com', $resp->getHeaderLine('Access-Control-Allow-Origin'));
91+
self::assertEquals('true', $resp->getHeaderLine('Access-Control-Allow-Credentials'));
92+
}
7593
}

module/Rest/test-api/Utils/ApiTestsExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Runner\Extension\Facade;
99
use PHPUnit\Runner\Extension\ParameterCollection;
1010
use PHPUnit\TextUI\Configuration\Configuration;
11+
use Symfony\Component\Process\Process;
1112

1213
class ApiTestsExtension implements Extension
1314
{
@@ -16,4 +17,9 @@ public function bootstrap(Configuration $configuration, Facade $facade, Paramete
1617
$facade->registerSubscriber(new EnvSpecificTestListener());
1718
$facade->registerSubscriber(new CleanDynamicEnvVarsTestListener());
1819
}
20+
21+
public static function restartRRServer(): void
22+
{
23+
(new Process(['bin/rr', 'reset', '-c=config/roadrunner/.rr.test.yml']))->mustRun();
24+
}
1925
}

module/Rest/test-api/Utils/CleanDynamicEnvVarsTestListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPUnit\Event\Test\Finished;
88
use PHPUnit\Event\Test\FinishedSubscriber;
9-
use Symfony\Component\Process\Process;
109

1110
use function file_exists;
1211
use function unlink;
@@ -23,7 +22,7 @@ public function notify(Finished $event): void
2322
if (file_exists(DYNAMIC_ENV_VARS_FILE)) {
2423
unlink(DYNAMIC_ENV_VARS_FILE);
2524
// Restart server again so that it removes the env vars from the file that has just been deleted
26-
(new Process(['bin/rr', 'reset', '-c=config/roadrunner/.rr.test.yml']))->mustRun();
25+
ApiTestsExtension::restartRRServer();
2726
}
2827
}
2928
}

module/Rest/test-api/Utils/EnvSpecificTestListener.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace ShlinkioApiTest\Shlink\Rest\Utils;
66

7+
use PHPUnit\Event\Code\TestMethod;
78
use PHPUnit\Event\Test\PreparationStarted;
89
use PHPUnit\Event\Test\PreparationStartedSubscriber;
910
use ReflectionMethod;
10-
use Symfony\Component\Process\Process;
1111
use Webimpress\SafeWriter\FileWriter;
1212

1313
use function sprintf;
@@ -24,6 +24,10 @@ class EnvSpecificTestListener implements PreparationStartedSubscriber
2424
public function notify(PreparationStarted $event): void
2525
{
2626
$test = $event->test();
27+
if (! ($test instanceof TestMethod)) {
28+
return;
29+
}
30+
2731
$className = $test->className();
2832
$methodName = $test->methodName();
2933

@@ -35,7 +39,7 @@ public function notify(PreparationStarted $event): void
3539
/** @var WithEnvVars $withEnvVars */
3640
$withEnvVars = $attributes[0]->newInstance();
3741
$this->createDynamicEnvVarsFile($withEnvVars->envVars);
38-
$this->restartServer();
42+
ApiTestsExtension::restartRRServer();
3943
}
4044
}
4145

@@ -53,9 +57,4 @@ private function createDynamicEnvVarsFile(array $envVars): void
5357

5458
FileWriter::writeFile(DYNAMIC_ENV_VARS_FILE, $content);
5559
}
56-
57-
private function restartServer(): void
58-
{
59-
(new Process(['bin/rr', 'reset', '-c=config/roadrunner/.rr.test.yml']))->mustRun();
60-
}
6160
}

0 commit comments

Comments
 (0)