jetzt auch mit einheitstests
This commit is contained in:
parent
d68e7f45b3
commit
e883913d3a
10 changed files with 3952 additions and 16 deletions
138
tests/Feature/AppTest.php
Normal file
138
tests/Feature/AppTest.php
Normal file
|
@ -0,0 +1,138 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\App;
|
||||
use Symfony\Component\Uid\UuidV1;
|
||||
use Symfony\Component\Uid\UuidV4;
|
||||
use Symfony\Component\Uid\UuidV7;
|
||||
use Symfony\Component\Uid\UuidV6;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
use Symfony\Component\Uid\Ulid;
|
||||
|
||||
covers(App::class);
|
||||
test('App returns correct version for endpoint', function (string $endpoint, string $class): void {
|
||||
$app = new App('https://example.com/');
|
||||
$request = Request::create('/' . $endpoint);
|
||||
|
||||
$response = $app->handle($request);
|
||||
|
||||
expect($response->getStatusCode())->toBe(200);
|
||||
expect($response->headers->get('Content-Type'))->toBe('text/plain');
|
||||
|
||||
$uuid = $response->getContent();
|
||||
expect(Uuid::isValid($uuid))->toBeTrue();
|
||||
expect(Uuid::fromString($uuid))->toBeInstanceOf($class);
|
||||
})->with([
|
||||
[
|
||||
'endpoint' => 'v1',
|
||||
'class' => UuidV1::class,
|
||||
],
|
||||
[
|
||||
'endpoint' => 'v4',
|
||||
'class' => UuidV4::class,
|
||||
],
|
||||
[
|
||||
'endpoint' => 'v6',
|
||||
'class' => UuidV6::class,
|
||||
],
|
||||
[
|
||||
'endpoint' => 'v7',
|
||||
'class' => UuidV7::class,
|
||||
],
|
||||
]);
|
||||
|
||||
test('App returns ULID for /ulid endpoint', function (): void {
|
||||
$app = new App('https://example.com/');
|
||||
$request = Request::create('/ulid');
|
||||
|
||||
$response = $app->handle($request);
|
||||
|
||||
expect($response->getStatusCode())->toBe(200);
|
||||
expect($response->headers->get('Content-Type'))->toBe('text/plain');
|
||||
|
||||
$ulid = $response->getContent();
|
||||
expect(Ulid::isValid($ulid))->toBeTrue();
|
||||
});
|
||||
|
||||
test('App returns help text for default endpoint', function (): void {
|
||||
$base = 'https://example.com/';
|
||||
$app = new App($base);
|
||||
$request = Request::create('/');
|
||||
|
||||
$response = $app->handle($request);
|
||||
|
||||
expect($response->getStatusCode())->toBe(200);
|
||||
expect($response->headers->get('Content-Type'))->toBe('text/plain');
|
||||
|
||||
$content = $response->getContent();
|
||||
expect($content)->toContain(
|
||||
'UUID/ULID Webservice',
|
||||
'Available Endpoints:',
|
||||
'v1',
|
||||
'v4',
|
||||
'v6',
|
||||
'v7',
|
||||
'ulid',
|
||||
$base,
|
||||
);
|
||||
});
|
||||
|
||||
test('App run method processes the request with provided request', function (): void {
|
||||
$app = new App('https://example.com/');
|
||||
$request = Request::create('/v4');
|
||||
|
||||
// Use output buffering to capture the output
|
||||
ob_start();
|
||||
$app->run($request);
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Verify the output is a valid UUID v4
|
||||
expect(Uuid::isValid($output))->toBeTrue();
|
||||
expect(Uuid::fromString($output))->toBeInstanceOf(UuidV4::class);
|
||||
});
|
||||
|
||||
test('App run method processes the request with createFromGlobals when no request provided', function (): void {
|
||||
// Save current server state
|
||||
$originalServer = $_SERVER;
|
||||
|
||||
// Set up $_SERVER to simulate a request to /v4
|
||||
$_SERVER['REQUEST_URI'] = '/v4';
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_SERVER['HTTP_HOST'] = 'example.com';
|
||||
|
||||
$app = new App('https://example.com/');
|
||||
|
||||
// Use output buffering to capture the output
|
||||
ob_start();
|
||||
$app->run();
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Restore original server state
|
||||
$_SERVER = $originalServer;
|
||||
|
||||
// Verify the output is a valid UUID v4
|
||||
expect(Uuid::isValid($output))->toBeTrue();
|
||||
expect(Uuid::fromString($output))->toBeInstanceOf(UuidV4::class);
|
||||
});
|
||||
|
||||
test('App run method sends the response for help page', function (): void {
|
||||
$app = new App('https://example.com/');
|
||||
$request = Request::create('/');
|
||||
|
||||
// Use output buffering to capture the output
|
||||
ob_start();
|
||||
$app->run($request);
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Verify the output contains the help text
|
||||
expect($output)->toContain('UUID/ULID Webservice');
|
||||
expect($output)->toContain('Available Endpoints:');
|
||||
expect($output)->toContain('/v1');
|
||||
expect($output)->toContain('/v4');
|
||||
expect($output)->toContain('/v6');
|
||||
expect($output)->toContain('/v7');
|
||||
expect($output)->toContain('/ulid');
|
||||
expect($output)->toContain('https://example.com/');
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue