You've already forked Php-Template
initial
This commit is contained in:
46
tests/Helpers/EnvTest.php
Normal file
46
tests/Helpers/EnvTest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Helpers;
|
||||
|
||||
use Siteworxpro\App\Helpers\Env;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class EnvTest extends Unit
|
||||
{
|
||||
public function testGetReturnsStringByDefault(): void
|
||||
{
|
||||
putenv('TEST_KEY=example');
|
||||
$result = Env::get('TEST_KEY');
|
||||
$this->assertSame('example', $result);
|
||||
}
|
||||
|
||||
public function testGetReturnsDefaultIfKeyNotSet(): void
|
||||
{
|
||||
putenv('TEST_KEY'); // Unset the environment variable
|
||||
$result = Env::get('TEST_KEY', 'default_value');
|
||||
$this->assertSame('default_value', $result);
|
||||
}
|
||||
|
||||
public function testGetCastsToBoolean(): void
|
||||
{
|
||||
putenv('TEST_KEY=true');
|
||||
$result = Env::get('TEST_KEY', null, 'bool');
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testGetCastsToInteger(): void
|
||||
{
|
||||
putenv('TEST_KEY=123');
|
||||
$result = Env::get('TEST_KEY', null, 'int');
|
||||
$this->assertSame(123, $result);
|
||||
}
|
||||
|
||||
public function testGetCastsToFloat(): void
|
||||
{
|
||||
putenv('TEST_KEY=123.45');
|
||||
$result = Env::get('TEST_KEY', null, 'float');
|
||||
$this->assertSame(123.45, $result);
|
||||
}
|
||||
}
|
||||
43
tests/Http/JsonResponseFactoryTest.php
Normal file
43
tests/Http/JsonResponseFactoryTest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Http;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
|
||||
class JsonResponseFactoryTest extends TestCase
|
||||
{
|
||||
public function testCreateJsonResponseReturnsValidResponse(): void
|
||||
{
|
||||
$data = ['key' => 'value'];
|
||||
$statusCode = 200;
|
||||
|
||||
$response = JsonResponseFactory::createJsonResponse($data, $statusCode);
|
||||
|
||||
$this->assertSame($statusCode, $response->getStatusCode());
|
||||
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertSame(json_encode($data), (string) $response->getBody());
|
||||
}
|
||||
|
||||
public function testCreateJsonResponseHandlesEmptyData(): void
|
||||
{
|
||||
$data = [];
|
||||
$statusCode = 204;
|
||||
|
||||
$response = JsonResponseFactory::createJsonResponse($data, $statusCode);
|
||||
|
||||
$this->assertSame($statusCode, $response->getStatusCode());
|
||||
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertSame(json_encode($data), (string) $response->getBody());
|
||||
}
|
||||
|
||||
public function testCreateJsonResponseThrowsExceptionOnInvalidData(): void
|
||||
{
|
||||
$this->expectException(\JsonException::class);
|
||||
|
||||
$data = ["invalid" => "\xB1\x31"];
|
||||
JsonResponseFactory::createJsonResponse($data);
|
||||
}
|
||||
}
|
||||
92
tests/Http/Middleware/CorsMiddlewareTest.php
Normal file
92
tests/Http/Middleware/CorsMiddlewareTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Http\Middleware;
|
||||
|
||||
use Nyholm\Psr7\Response;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Siteworxpro\App\Facades\Config;
|
||||
use Siteworxpro\App\Http\Middleware\CorsMiddleware;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class CorsMiddlewareTest extends Unit
|
||||
{
|
||||
public function testAllowsConfiguredOrigin(): void
|
||||
{
|
||||
Config::shouldReceive('get')
|
||||
->with('CORS_ALLOWED_ORIGINS', 'https://example.com,https://another.com')
|
||||
->andReturn('https://example.com,https://another.com');
|
||||
|
||||
$middleware = new CorsMiddleware();
|
||||
$request = new ServerRequest('GET', '/')->withHeader('Origin', 'https://example.com');
|
||||
$handler = $this->mockHandler(new Response(200));
|
||||
|
||||
$response = $middleware->process($request, $handler);
|
||||
|
||||
$this->assertEquals('https://example.com', $response->getHeaderLine('Access-Control-Allow-Origin'));
|
||||
}
|
||||
|
||||
public function testBlocksUnconfiguredOrigin(): void
|
||||
{
|
||||
Config::shouldReceive('get')
|
||||
->with('CORS_ALLOWED_ORIGINS', 'https://example.com,https://another.com')
|
||||
->andReturn('https://example.com,https://another.com');
|
||||
|
||||
$middleware = new CorsMiddleware();
|
||||
$request = new ServerRequest('GET', '/')->withHeader('Origin', 'https://unauthorized.com');
|
||||
$handler = $this->mockHandler(new Response(200));
|
||||
|
||||
$response = $middleware->process($request, $handler);
|
||||
|
||||
$this->assertEquals('null', $response->getHeaderLine('Access-Control-Allow-Origin'));
|
||||
}
|
||||
|
||||
public function testHandlesOptionsRequest(): void
|
||||
{
|
||||
Config::shouldReceive('get')->with('CORS_ALLOWED_ORIGINS', '...')->andReturn('https://example.com');
|
||||
Config::shouldReceive('get')->with('CORS_ALLOW_CREDENTIALS', 'bool')->andReturn(false);
|
||||
Config::shouldReceive('get')->with('CORS_MAX_AGE')->andReturn('86400');
|
||||
|
||||
$middleware = new CorsMiddleware();
|
||||
$request = new ServerRequest('OPTIONS', '/')->withHeader('Origin', 'https://example.com');
|
||||
$handler = $this->mockHandler(new Response(200));
|
||||
|
||||
$response = $middleware->process($request, $handler);
|
||||
|
||||
$this->assertEquals(204, $response->getStatusCode());
|
||||
$this->assertEquals('86400', $response->getHeaderLine('Access-Control-Max-Age'));
|
||||
}
|
||||
|
||||
public function testAddsAllowCredentialsHeader(): void
|
||||
{
|
||||
Config::shouldReceive('get')->with('CORS_ALLOWED_ORIGINS', '...')->andReturn('https://example.com');
|
||||
Config::shouldReceive('get')->with('CORS_ALLOW_CREDENTIALS', 'bool')->andReturn(true);
|
||||
|
||||
$middleware = new CorsMiddleware();
|
||||
$request = new ServerRequest('GET', '/')->withHeader('Origin', 'https://example.com');
|
||||
$handler = $this->mockHandler(new Response(200));
|
||||
|
||||
$response = $middleware->process($request, $handler);
|
||||
|
||||
$this->assertEquals('true', $response->getHeaderLine('Access-Control-Allow-Credentials'));
|
||||
}
|
||||
|
||||
private function mockHandler(Response $response): RequestHandlerInterface
|
||||
{
|
||||
return new class($response) implements RequestHandlerInterface {
|
||||
private Response $response;
|
||||
|
||||
public function __construct(Response $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function handle(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
12
tests/Unit.php
Normal file
12
tests/Unit.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class Unit extends TestCase
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user