You've already forked php-auth
generated from siteworxpro/Php-Template
Initial commit
This commit is contained in:
49
tests/Attributes/Guards/JwtTest.php
Normal file
49
tests/Attributes/Guards/JwtTest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Attributes\Guards;
|
||||
|
||||
use Siteworxpro\App\Attributes\Guards\Jwt;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class JwtTest extends Unit
|
||||
{
|
||||
public function testGetsClassFromConfig(): void
|
||||
{
|
||||
Config::set('jwt.issuer', 'default-issuer');
|
||||
Config::set('jwt.audience', 'default-audience');
|
||||
|
||||
$reflection = new \ReflectionClass(TestClass::class);
|
||||
$attributes = $reflection->getAttributes(Jwt::class);
|
||||
$this->assertCount(1, $attributes);
|
||||
|
||||
/** @var Jwt $instance */
|
||||
$instance = $attributes[0]->newInstance();
|
||||
$this->assertEquals('default-audience', $instance->getAudience());
|
||||
$this->assertEquals('default-issuer', $instance->getIssuer());
|
||||
}
|
||||
|
||||
public function testGetsClassFromCustom(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(TestClassSpecific::class);
|
||||
$attributes = $reflection->getAttributes(Jwt::class);
|
||||
$this->assertCount(1, $attributes);
|
||||
|
||||
/** @var Jwt $instance */
|
||||
$instance = $attributes[0]->newInstance();
|
||||
$this->assertEquals('custom-audience', $instance->getAudience());
|
||||
$this->assertEquals('custom-issuer', $instance->getIssuer());
|
||||
}
|
||||
}
|
||||
|
||||
#[Jwt]
|
||||
class TestClass // @codingStandardsIgnoreLine
|
||||
{
|
||||
}
|
||||
|
||||
#[Jwt('custom-issuer', 'custom-audience')]
|
||||
class TestClassSpecific // @codingStandardsIgnoreLine
|
||||
{
|
||||
}
|
||||
43
tests/Attributes/Guards/ScopeTest.php
Normal file
43
tests/Attributes/Guards/ScopeTest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Attributes\Guards;
|
||||
|
||||
use Siteworxpro\App\Attributes\Guards\Scope;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class ScopeTest extends Unit
|
||||
{
|
||||
public function testGetsClassSingle(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(TestClassSingle::class);
|
||||
$attributes = $reflection->getAttributes(Scope::class);
|
||||
$this->assertCount(1, $attributes);
|
||||
|
||||
/** @var Scope $instance */
|
||||
$instance = $attributes[0]->newInstance();
|
||||
$this->assertEquals(['read:users'], $instance->getScopes());
|
||||
}
|
||||
|
||||
public function testGetsClassFromCustom(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(TestClassMultiple::class);
|
||||
$attributes = $reflection->getAttributes(Scope::class);
|
||||
$this->assertCount(1, $attributes);
|
||||
|
||||
/** @var Scope $instance */
|
||||
$instance = $attributes[0]->newInstance();
|
||||
$this->assertEquals(['read:users', 'write:users'], $instance->getScopes());
|
||||
}
|
||||
}
|
||||
|
||||
#[Scope(['read:users', 'write:users'])]
|
||||
class TestClassMultiple // @codingStandardsIgnoreLine
|
||||
{
|
||||
}
|
||||
|
||||
#[Scope(['read:users'])]
|
||||
class TestClassSingle // @codingStandardsIgnoreLine
|
||||
{
|
||||
}
|
||||
25
tests/Attributes/HandlesMessageTest.php
Normal file
25
tests/Attributes/HandlesMessageTest.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Attributes;
|
||||
|
||||
use Siteworxpro\App\Attributes\Async\HandlesMessage;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class HandlesMessageTest extends Unit
|
||||
{
|
||||
public function testGetsClass(): void
|
||||
{
|
||||
$class = new #[HandlesMessage('Siteworxpro\Tests\Attributes\TestClass')] class {
|
||||
};
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$attributes = $reflection->getAttributes(HandlesMessage::class);
|
||||
$this->assertCount(1, $attributes);
|
||||
|
||||
/** @var HandlesMessage $instance */
|
||||
$instance = $attributes[0]->newInstance();
|
||||
$this->assertEquals('Siteworxpro\Tests\Attributes\TestClass', $instance->getMessageClass());
|
||||
}
|
||||
}
|
||||
36
tests/CommandBus/AttributeLocatorTest.php
Normal file
36
tests/CommandBus/AttributeLocatorTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\CommandBus;
|
||||
|
||||
use League\Tactician\Exception\CanNotInvokeHandlerException;
|
||||
use Siteworxpro\App\CommandBus\AttributeLocator;
|
||||
use Siteworxpro\App\CommandBus\Commands\ExampleCommand;
|
||||
use Siteworxpro\App\CommandBus\Handlers\ExampleHandler;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class AttributeLocatorTest extends Unit
|
||||
{
|
||||
private const array HANDLERS = [
|
||||
ExampleCommand::class => ExampleHandler::class,
|
||||
];
|
||||
|
||||
public function testResolvesFiles(): void
|
||||
{
|
||||
$attributeLocator = new AttributeLocator();
|
||||
|
||||
foreach (self::HANDLERS as $command => $handler) {
|
||||
$class = $attributeLocator->getHandlerForCommand($command);
|
||||
$this->assertInstanceOf($handler, $class);
|
||||
}
|
||||
}
|
||||
|
||||
public function testThrowsOnCannotResolve(): void
|
||||
{
|
||||
$attributeLocator = new AttributeLocator();
|
||||
|
||||
$this->expectException(CanNotInvokeHandlerException::class);
|
||||
$attributeLocator->getHandlerForCommand('NonExistentCommand');
|
||||
}
|
||||
}
|
||||
32
tests/CommandBus/Handlers/ExampleHandlerTest.php
Normal file
32
tests/CommandBus/Handlers/ExampleHandlerTest.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Siteworxpro\Tests\CommandBus\Handlers;
|
||||
|
||||
use Siteworxpro\App\CommandBus\Commands\Command;
|
||||
use Siteworxpro\App\CommandBus\Commands\ExampleCommand;
|
||||
use Siteworxpro\App\CommandBus\Handlers\ExampleHandler;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class ExampleHandlerTest extends Unit
|
||||
{
|
||||
public function testExampleCommand(): void
|
||||
{
|
||||
$command = new ExampleCommand('test payload');
|
||||
$this->assertEquals('test payload', $command->getName());
|
||||
|
||||
$handler = new ExampleHandler();
|
||||
$result = $handler($command);
|
||||
$this->assertEquals('Hello, test payload!', $result);
|
||||
}
|
||||
|
||||
public function testThrowsException(): void
|
||||
{
|
||||
$class = new readonly class extends Command
|
||||
{
|
||||
};
|
||||
|
||||
$this->expectException(\TypeError::class);
|
||||
$handler = new ExampleHandler();
|
||||
$handler($class);
|
||||
}
|
||||
}
|
||||
16
tests/Controllers/AbstractController.php
Normal file
16
tests/Controllers/AbstractController.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Controllers;
|
||||
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
abstract class AbstractController extends Unit
|
||||
{
|
||||
protected function getMockRequest(string $method = 'GET', string $uri = '/'): ServerRequest
|
||||
{
|
||||
return new ServerRequest($method, $uri);
|
||||
}
|
||||
}
|
||||
54
tests/Controllers/ControllerTest.php
Normal file
54
tests/Controllers/ControllerTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Controllers;
|
||||
|
||||
use Siteworxpro\App\Controllers\Controller;
|
||||
|
||||
class ControllerTest extends AbstractController
|
||||
{
|
||||
public function testNotFoundExceptions()
|
||||
{
|
||||
$testClass = new TestClass();
|
||||
|
||||
$this->expectException(\League\Route\Http\Exception\NotFoundException::class);
|
||||
$testClass->get($this->getMockRequest());
|
||||
}
|
||||
|
||||
public function testNotFoundExceptionPost()
|
||||
{
|
||||
$testClass = new TestClass();
|
||||
|
||||
$this->expectException(\League\Route\Http\Exception\NotFoundException::class);
|
||||
$testClass->post($this->getMockRequest());
|
||||
}
|
||||
|
||||
public function testNotFoundExceptionPut()
|
||||
{
|
||||
$testClass = new TestClass();
|
||||
|
||||
$this->expectException(\League\Route\Http\Exception\NotFoundException::class);
|
||||
$testClass->put($this->getMockRequest());
|
||||
}
|
||||
|
||||
public function testNotFoundExceptionDelete()
|
||||
{
|
||||
$testClass = new TestClass();
|
||||
|
||||
$this->expectException(\League\Route\Http\Exception\NotFoundException::class);
|
||||
$testClass->delete($this->getMockRequest());
|
||||
}
|
||||
|
||||
public function testNotFoundExceptionPatch()
|
||||
{
|
||||
$testClass = new TestClass();
|
||||
|
||||
$this->expectException(\League\Route\Http\Exception\NotFoundException::class);
|
||||
$testClass->patch($this->getMockRequest());
|
||||
}
|
||||
}
|
||||
|
||||
class TestClass extends Controller // phpcs:ignore
|
||||
{
|
||||
}
|
||||
48
tests/Controllers/IndexControllerTest.php
Normal file
48
tests/Controllers/IndexControllerTest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Controllers;
|
||||
|
||||
use League\Tactician\CommandBus;
|
||||
use Siteworxpro\App\Controllers\IndexController;
|
||||
|
||||
class IndexControllerTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @throws \JsonException|\ReflectionException
|
||||
*/
|
||||
public function testGet(): void
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
|
||||
$this->getContainer()->bind(CommandBus::class, function () {
|
||||
return \Mockery::mock(CommandBus::class)
|
||||
->shouldReceive('handle')
|
||||
->andReturn('Hello World')
|
||||
->getMock();
|
||||
});
|
||||
|
||||
$controller = new IndexController();
|
||||
|
||||
$response = $controller->get($this->getMockRequest());
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"message":"Server is running. Hello World"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testPost(): void
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
|
||||
$controller = new IndexController();
|
||||
|
||||
$response = $controller->post($this->getMockRequest());
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"message":"POST request received"}', (string)$response->getBody());
|
||||
}
|
||||
}
|
||||
33
tests/Controllers/OpenApiControllerTest.php
Normal file
33
tests/Controllers/OpenApiControllerTest.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Controllers;
|
||||
|
||||
use Siteworxpro\App\Controllers\OpenApiController;
|
||||
|
||||
class OpenApiControllerTest extends ControllerTest
|
||||
{
|
||||
public function testBuildsYaml(): void
|
||||
{
|
||||
$request = $this->getMockRequest('/.well-known/openapi.yaml');
|
||||
$controller = new OpenApiController();
|
||||
|
||||
$response = $controller->get($request);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertStringContainsString('openapi: 3.0.0', (string)$response->getBody());
|
||||
}
|
||||
|
||||
public function testBuildsJson(): void
|
||||
{
|
||||
$request = $this->getMockRequest(uri: '/.well-known/openapi.json');
|
||||
$controller = new OpenApiController();
|
||||
|
||||
$response = $controller->get($request);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertNotFalse(json_decode($response->getBody()->getContents()));
|
||||
}
|
||||
}
|
||||
177
tests/Events/DispatcherTest.php
Normal file
177
tests/Events/DispatcherTest.php
Normal file
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Events;
|
||||
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class DispatcherTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws \Throwable
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function testRegistersListeners(): void
|
||||
{
|
||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
||||
|
||||
$eventFired = false;
|
||||
$dispatcher->listen('TestEvent', function ($event) use (&$eventFired) {
|
||||
$this->assertEquals('TestEvent', $event);
|
||||
$eventFired = true;
|
||||
});
|
||||
|
||||
$dispatcher->dispatch('TestEvent');
|
||||
$this->assertTrue($eventFired, 'The TestEvent listener was not fired.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function testPushesEvents()
|
||||
{
|
||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
||||
|
||||
$eventsFired = 0;
|
||||
$dispatcher->listen('PushedEvent1', function ($event) use (&$eventsFired) {
|
||||
$eventsFired++;
|
||||
$this->assertEquals('PushedEvent1', $event);
|
||||
});
|
||||
|
||||
$dispatcher->listen('PushedEvent2', function ($event) use (&$eventsFired) {
|
||||
$eventsFired++;
|
||||
$this->assertEquals('PushedEvent2', $event);
|
||||
});
|
||||
|
||||
$dispatcher->push('PushedEvent1');
|
||||
$dispatcher->push('PushedEvent2');
|
||||
|
||||
unset($dispatcher); // Trigger destructor
|
||||
$this->assertEquals(2, $eventsFired);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function testFlushEvent(): void
|
||||
{
|
||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
||||
|
||||
$eventFired = false;
|
||||
$dispatcher->listen('FlushEvent', function ($event) use (&$eventFired) {
|
||||
$this->assertEquals('FlushEvent', $event);
|
||||
$eventFired = true;
|
||||
});
|
||||
|
||||
$dispatcher->push('FlushEvent');
|
||||
$dispatcher->flush('FlushEvent');
|
||||
|
||||
$this->assertTrue($eventFired, 'The FlushEvent listener was not fired.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function testHasListeners(): void
|
||||
{
|
||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
||||
|
||||
$this->assertFalse(
|
||||
$dispatcher->hasListeners(
|
||||
'NonExistentEvent'
|
||||
),
|
||||
'Expected no listeners for NonExistentEvent.'
|
||||
);
|
||||
|
||||
$dispatcher->listen('ExistingEvent', function () {
|
||||
// Listener logic
|
||||
});
|
||||
|
||||
$this->assertTrue(
|
||||
$dispatcher->hasListeners(
|
||||
'ExistingEvent'
|
||||
),
|
||||
'Expected listeners for ExistingEvent.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function testForgetEvent(): void
|
||||
{
|
||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
||||
|
||||
$eventFired = false;
|
||||
$dispatcher->listen('ForgetEvent', function ($event) use (&$eventFired) {
|
||||
$this->assertEquals('ForgetEvent', $event);
|
||||
$eventFired = true;
|
||||
});
|
||||
|
||||
$dispatcher->push('ForgetEvent');
|
||||
$dispatcher->forget('ForgetEvent');
|
||||
|
||||
unset($dispatcher); // Trigger destructor
|
||||
|
||||
$this->assertFalse($eventFired, 'The ForgetEvent listener was fired but should have been forgotten.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function testForgetPushed()
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
||||
|
||||
$dispatcher->listen('EventToForget', function () {
|
||||
$this->fail('The EventToForget listener was fired but should have been forgotten.');
|
||||
});
|
||||
|
||||
$dispatcher->push('EventToForget');
|
||||
$dispatcher->forgetPushed();
|
||||
|
||||
unset($dispatcher); // Trigger destructor
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function testToArray(): void
|
||||
{
|
||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
||||
|
||||
$dispatcher->listen('ArrayEvent', function () {
|
||||
// Listener logic
|
||||
});
|
||||
|
||||
$arrayRepresentation = $dispatcher->toArray();
|
||||
$this->assertArrayHasKey('ArrayEvent', $arrayRepresentation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function testSubscriber()
|
||||
{
|
||||
$subscriber = $this->getMockBuilder('Siteworxpro\App\Events\Subscribers\Subscriber')
|
||||
->onlyMethods(['handle'])
|
||||
->getMock();
|
||||
|
||||
$subscriber->expects($this->once())
|
||||
->method('handle')
|
||||
->with('SubscribedEvent', [])
|
||||
->willReturn(null);
|
||||
|
||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
||||
$dispatcher->subscribe($subscriber);
|
||||
|
||||
$dispatcher->dispatch('SubscribedEvent');
|
||||
}
|
||||
}
|
||||
47
tests/Events/Listeners/ConnectedTest.php
Normal file
47
tests/Events/Listeners/ConnectedTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Events\Listeners;
|
||||
|
||||
use Illuminate\Database\Events\ConnectionEstablished;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use Siteworxpro\App\Events\Listeners\Database\Connected;
|
||||
use Siteworxpro\App\Log\Logger;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class ConnectedTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws \ReflectionException
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$inputBuffer = fopen('php://memory', 'r+');
|
||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
||||
\Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()->bind(Logger::class, fn() => $logger);
|
||||
}
|
||||
|
||||
public function testHandlesEvent()
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
$connectedEvent = $this->createMock(ConnectionEstablished::class);
|
||||
$listener = new Connected();
|
||||
|
||||
$listener->__invoke($connectedEvent);
|
||||
}
|
||||
|
||||
public function testThrowsException()
|
||||
{
|
||||
$this->expectException(\TypeError::class);
|
||||
$listener = new Connected();
|
||||
$listener->__invoke(new \stdClass());
|
||||
}
|
||||
}
|
||||
32
tests/Facades/AbstractFacade.php
Normal file
32
tests/Facades/AbstractFacade.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Facades;
|
||||
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
abstract class AbstractFacade extends Unit
|
||||
{
|
||||
abstract protected function getFacadeClass(): string;
|
||||
abstract protected function getConcrete(): string;
|
||||
|
||||
public function testFacadeAccessor(): void
|
||||
{
|
||||
/** @var Facade | string $class */
|
||||
$class = $this->getFacadeClass();
|
||||
|
||||
$this->assertTrue(
|
||||
method_exists($class, 'getFacadeAccessor'),
|
||||
sprintf('The class %s must implement the method getFacadeAccessor.', $class)
|
||||
);
|
||||
|
||||
$facade = $class::getFacadeRoot();
|
||||
|
||||
$this->assertNotNull(
|
||||
$facade,
|
||||
sprintf('The facade %s is not properly initialized.', $this->getConcrete())
|
||||
);
|
||||
}
|
||||
}
|
||||
20
tests/Facades/DispatcherTest.php
Normal file
20
tests/Facades/DispatcherTest.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Facades;
|
||||
|
||||
use Siteworxpro\App\Services\Facades\Dispatcher;
|
||||
|
||||
class DispatcherTest extends AbstractFacade
|
||||
{
|
||||
protected function getFacadeClass(): string
|
||||
{
|
||||
return Dispatcher::class;
|
||||
}
|
||||
|
||||
protected function getConcrete(): string
|
||||
{
|
||||
return \Siteworxpro\App\Events\Dispatcher::class;
|
||||
}
|
||||
}
|
||||
21
tests/Facades/GuzzleTest.php
Normal file
21
tests/Facades/GuzzleTest.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Facades;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Siteworxpro\App\Services\Facades\Guzzle;
|
||||
|
||||
class GuzzleTest extends AbstractFacade
|
||||
{
|
||||
protected function getFacadeClass(): string
|
||||
{
|
||||
return Guzzle::class;
|
||||
}
|
||||
|
||||
protected function getConcrete(): string
|
||||
{
|
||||
return Client::class;
|
||||
}
|
||||
}
|
||||
20
tests/Facades/LoggerTest.php
Normal file
20
tests/Facades/LoggerTest.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Facades;
|
||||
|
||||
use Siteworxpro\App\Services\Facades\Logger;
|
||||
|
||||
class LoggerTest extends AbstractFacade
|
||||
{
|
||||
protected function getFacadeClass(): string
|
||||
{
|
||||
return Logger::class;
|
||||
}
|
||||
|
||||
protected function getConcrete(): string
|
||||
{
|
||||
return \Siteworxpro\App\Log\Logger::class;
|
||||
}
|
||||
}
|
||||
21
tests/Facades/RedisTest.php
Normal file
21
tests/Facades/RedisTest.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Facades;
|
||||
|
||||
use Predis\Client;
|
||||
use Siteworxpro\App\Services\Facades\Redis;
|
||||
|
||||
class RedisTest extends AbstractFacade
|
||||
{
|
||||
protected function getFacadeClass(): string
|
||||
{
|
||||
return Redis::class;
|
||||
}
|
||||
|
||||
protected function getConcrete(): string
|
||||
{
|
||||
return Client::class;
|
||||
}
|
||||
}
|
||||
36
tests/GrpcHandlers/GreeterHandlerTest.php
Normal file
36
tests/GrpcHandlers/GreeterHandlerTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\GrpcHandlers;
|
||||
|
||||
use GRPC\Greeter\HelloRequest;
|
||||
use League\Tactician\CommandBus;
|
||||
use Siteworxpro\App\GrpcHandlers\GreeterHandler;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
use Spiral\RoadRunner\GRPC\ContextInterface;
|
||||
|
||||
class GreeterHandlerTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function testSayHello(): void
|
||||
{
|
||||
$this->getContainer()->bind(CommandBus::class, function () {
|
||||
return \Mockery::mock(CommandBus::class)
|
||||
->shouldReceive('handle')
|
||||
->andReturn('Hello World')
|
||||
->getMock();
|
||||
});
|
||||
|
||||
$request = new HelloRequest();
|
||||
$request->setName('World');
|
||||
|
||||
$context = \Mockery::mock(ContextInterface::class);
|
||||
|
||||
$handler = new GreeterHandler();
|
||||
$response = $handler->SayHello($context, $request);
|
||||
$this->assertEquals('Hello World', $response->getMessage());
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
19
tests/Helpers/UlidTest.php
Normal file
19
tests/Helpers/UlidTest.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Helpers;
|
||||
|
||||
use Siteworxpro\App\Helpers\Env;
|
||||
use Siteworxpro\App\Helpers\Ulid;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class UlidTest extends Unit
|
||||
{
|
||||
public function testGetString(): void
|
||||
{
|
||||
$ulid = Ulid::generate();
|
||||
$this->assertIsString($ulid);
|
||||
$this->assertEquals(16, strlen($ulid));
|
||||
}
|
||||
}
|
||||
50
tests/Http/JsonResponseFactoryTest.php
Normal file
50
tests/Http/JsonResponseFactoryTest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Http;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
use Siteworxpro\HttpStatus\CodesEnum;
|
||||
|
||||
class JsonResponseFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testCreateJsonResponseReturnsValidResponse(): void
|
||||
{
|
||||
$data = ['key' => 'value'];
|
||||
$statusCode = CodesEnum::OK;
|
||||
|
||||
$response = JsonResponseFactory::createJsonResponse($data, $statusCode);
|
||||
|
||||
$this->assertSame($statusCode->value, $response->getStatusCode());
|
||||
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertSame(json_encode($data), (string) $response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testCreateJsonResponseHandlesEmptyData(): void
|
||||
{
|
||||
$data = [];
|
||||
$statusCode = CodesEnum::NO_CONTENT;
|
||||
|
||||
$response = JsonResponseFactory::createJsonResponse($data, $statusCode);
|
||||
|
||||
$this->assertSame($statusCode->value, $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);
|
||||
}
|
||||
}
|
||||
83
tests/Http/Middleware/CorsMiddlewareTest.php
Normal file
83
tests/Http/Middleware/CorsMiddlewareTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?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\Http\Middleware\CorsMiddleware;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class CorsMiddlewareTest extends Middleware
|
||||
{
|
||||
public function testAllowsConfiguredOrigin(): void
|
||||
{
|
||||
Config::shouldReceive('get')
|
||||
->with('cors.allowed_origins')
|
||||
->andReturn('https://example.com,https://another.com');
|
||||
|
||||
Config::shouldReceive('get')->with('cors.allow_credentials')->andReturn(false);
|
||||
Config::shouldReceive('get')->with('cors.max_age')->andReturn('');
|
||||
|
||||
$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')
|
||||
->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->assertEmpty($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')->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.allowed_origins')->andReturn('https://example.com');
|
||||
Config::shouldReceive('get')->with('cors.allow_credentials')->andReturn(true);
|
||||
Config::shouldReceive('get')->with('cors.max_age')->andReturn('86400');
|
||||
|
||||
$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'));
|
||||
}
|
||||
}
|
||||
366
tests/Http/Middleware/JwtMiddlewareTest.php
Normal file
366
tests/Http/Middleware/JwtMiddlewareTest.php
Normal file
@@ -0,0 +1,366 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Http\Middleware;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Lcobucci\JWT\JwtFacade;
|
||||
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
||||
use Lcobucci\JWT\Signer\Key\InMemory;
|
||||
use Lcobucci\JWT\Token\Builder;
|
||||
use League\Route\Dispatcher;
|
||||
use Nyholm\Psr7\Response;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Siteworxpro\App\Attributes\Guards\Jwt;
|
||||
use Siteworxpro\App\Http\Middleware\JwtMiddleware;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
use Siteworxpro\App\Services\Facades\Guzzle;
|
||||
use Siteworxpro\App\Services\Facades\Redis;
|
||||
use Siteworxpro\HttpStatus\CodesEnum;
|
||||
|
||||
class JwtMiddlewareTest extends Middleware
|
||||
{
|
||||
private const string TEST_SIGNING_KEY = 'test_signing_key_123456444478901234';
|
||||
|
||||
private const string TEST_RSA_PRIVATE_KEY = <<<EOD
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpQIBAAKCAQEAqTheAdlelxJL0K15BqUEo0lBzY06P7J0PhMfPlg2fgIJH+ng
|
||||
ZmrpYFhBkj2L5Fnvxz0y58eu9WhhokwpS0GzgFIw+KfLV/WLX4PgionsQshrt0Pi
|
||||
XvthaSH1xuYtg2N13dVVTv3Au0BBFLUHMrQ+bO5hgvowHBNfFf0GaHLW2m0eZ2Um
|
||||
hWbtdv4HxrXBO5gI2N4UevyQ+inczN7RBZR6ZzyNoDO6Up6kS23/58zOruO+PGi7
|
||||
q9eb7hU+getpVgA29wEWMgT+N6c5n5AcENgM1sHxZK43GR5vhMGbVJqnrUsMGof7
|
||||
rT9Lxey3gjPS2r5nz2PNFcQ1i07QKDzvQHp2wwIDAQABAoIBAFMAC9QaWzP8TGWJ
|
||||
gNBKhnDU0MrSl5yAmlWMKYn52JiLxQ/7Ng7mJ5wTDe5986zIlDyEfwCCyAUk8qaZ
|
||||
drOsATBSoCSGoM1+6aKq26r4JYNILNVSHal64XegqZ2qbu6ADWMGbXZ2Ll9qD8Hp
|
||||
XSN4lxn0/q0wrAJJWh094zO+CDZP+zBbX9oHxb5JAVxjCaNW84sI6/6agXM5zzgK
|
||||
wcBt5Y0i8V8f7n9kg+CPNqY6BKg7o2ONFYTEVKuuEnVS/eupHQwBWExPCdxc85Tb
|
||||
YqFL0dmgehE0OTQ6FrEN7Xh6jE4GMJtWmTvBNpqhsMZ0i08tAZSPs+Us9rnppKkK
|
||||
T1SC2xECgYEA7yOv4C7dtHmFbn0YfnbBEfgvGAubv5jPDtZ5u6tUEhhU3rOcWexM
|
||||
Xhj7OFV4I8lbu2t7GY+2BR7Y2ikOLW9MrOGo6qWhsjTQuZs6QaRKObcPvl2s0LYY
|
||||
GxD1u84VjHPzID2pKVPqxaQ7KdcIaujAedWwAf4PV/uK2prKdGvzIksCgYEAtSau
|
||||
4Ml1UpXvKxiBcVKsHIoEO0g3NL1+wAbdStg8TFi+leCMJoPwZ01t64BTtHF+pgDP
|
||||
vn6VEgDSP3J4+W3dVhoajQeKBioT3MpDRP/qKDsImi2zJrg+hh9DMTlZd0Ab3EXv
|
||||
ycjw3FWRcpcU/1l261fA/m3QPwZikF2VlO/0cmkCgYEAvtefCuy718RHHObOPlZt
|
||||
O/bxNmJFOEEttOyql39iB1LNoDB8bTLruwh6q/lheEXAZDChO8P5gdqdOnUbMF0r
|
||||
Nqib0i6+fOYzUHw1oJ8I8UhLUyOUv7ciQ69kPC15+u2psCglMKscp/+pi3lk6VS4
|
||||
DkLfRKfI/PDsXgq72O8xSEMCgYEApukSnvngyQxvR1UYB7N19AHTLlA21bh4LjTk
|
||||
905QGMR4Lp6sY9yTyIsWabRe69bbK9d5kvsNHX52OpGeF6z8EJaSujklGtLwZDJV
|
||||
UyE9vn3OSkkrVdTTfz8U6Sj/XxpJ0Wb7LwCftVR+ZIgCh9kF8ohzwbqq8zdN39jq
|
||||
t0V1BWkCgYEA2Mk2gOdYAN8aZgydFYKhogY5UNK/CFpq7hhekEyt73uxzxguVpZn
|
||||
AJ9mq2L1CVJ5WqAUk2IzioeR7XAndntesbOafDuR4mhCUJhX+m/YQlKbTrs2dScR
|
||||
S88z05AnmQmr5eCbQmVULZGo9xeLDB+GDWvvjpQ+NWcha2uO0O0RTQY=
|
||||
-----END RSA PRIVATE KEY-----
|
||||
EOD;
|
||||
|
||||
private const string TEST_JWKS_JSON = <<<EOD
|
||||
{
|
||||
"keys": [
|
||||
{
|
||||
"alg": "RS256",
|
||||
"e": "AQAB",
|
||||
"ext": true,
|
||||
"key_ops": [
|
||||
"verify"
|
||||
],
|
||||
"kty": "RSA",
|
||||
"n": "qTheAdlelxJL0K15BqUEo0lBzY06P7J0PhMfPlg2fgIJH-ngZmrpYFhBkj2L5Fnvxz0y58eu9WhhokwpS0GzgFIw-KfLV_WLX4PgionsQshrt0PiXvthaSH1xuYtg2N13dVVTv3Au0BBFLUHMrQ-bO5hgvowHBNfFf0GaHLW2m0eZ2UmhWbtdv4HxrXBO5gI2N4UevyQ-inczN7RBZR6ZzyNoDO6Up6kS23_58zOruO-PGi7q9eb7hU-getpVgA29wEWMgT-N6c5n5AcENgM1sHxZK43GR5vhMGbVJqnrUsMGof7rT9Lxey3gjPS2r5nz2PNFcQ1i07QKDzvQHp2ww",
|
||||
"kid": "2o5IaHnjxYtkpNWEcdPlwnaRJnaCJ2k2LY2nR4z6cN4=",
|
||||
"use": "sig"
|
||||
}
|
||||
]
|
||||
}
|
||||
EOD;
|
||||
|
||||
public function getClass(): object
|
||||
{
|
||||
return new class {
|
||||
public function getCallable(): array
|
||||
{
|
||||
return [$this, 'index'];
|
||||
}
|
||||
|
||||
#[Jwt]
|
||||
public function index()
|
||||
{
|
||||
// Dummy method for testing
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Config::set('jwt.signing_key', self::TEST_SIGNING_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testIgnoresNoJwtAttribute()
|
||||
{
|
||||
$class = new class {
|
||||
public function getCallable(): array
|
||||
{
|
||||
return [$this, 'index'];
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
// Dummy method for testing
|
||||
}
|
||||
};
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$handler
|
||||
->shouldReceive('handle')
|
||||
->once()
|
||||
->andReturn(new Response(200));
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::OK->value, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testIgnoresJwtAttributeButNoToken()
|
||||
{
|
||||
$class = $this->getClass();
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::UNAUTHORIZED->value, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testInvalidToken()
|
||||
{
|
||||
$class = $this->getClass();
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$request = $request->withHeader('Authorization', 'Bearer ' . 'invalid_token_string');
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::UNAUTHORIZED->value, $response->getStatusCode());
|
||||
$this->assertStringContainsString(
|
||||
'Unauthorized: Invalid token',
|
||||
$response->getBody()->getContents()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testJwtAttributeWithTokenButWrongAud()
|
||||
{
|
||||
$class = $this->getClass();
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$request = $request->withHeader('Authorization', 'Bearer ' . $this->getJwt());
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::UNAUTHORIZED->value, $response->getStatusCode());
|
||||
$this->assertStringContainsString(
|
||||
'The token is not allowed to be used by this audience',
|
||||
$response->getBody()->getContents()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testJwtAttributeWithTokenButWrongIss()
|
||||
{
|
||||
Config::set('jwt.audience', 'https://client-app.io');
|
||||
|
||||
$class = $this->getClass();
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$request = $request->withHeader('Authorization', 'Bearer ' . $this->getJwt());
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::UNAUTHORIZED->value, $response->getStatusCode());
|
||||
$this->assertStringContainsString(
|
||||
'The token was not issued by the given issuers',
|
||||
$response->getBody()->getContents()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testJwtAttributeWithTokenWithDiffIssuer()
|
||||
{
|
||||
Config::set('jwt.audience', 'https://client-app.io');
|
||||
Config::set('jwt.issuer', 'https://different-issuer.io');
|
||||
|
||||
$class = $this->getClass();
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$request = $request->withHeader('Authorization', 'Bearer ' . $this->getJwt());
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::UNAUTHORIZED->value, $response->getStatusCode());
|
||||
$this->assertStringContainsString(
|
||||
'The token was not issued by the given issuers',
|
||||
$response->getBody()->getContents()
|
||||
);
|
||||
}
|
||||
|
||||
public function testJwtAttributeWithToken()
|
||||
{
|
||||
Config::set('jwt.audience', 'https://client-app.io');
|
||||
Config::set('jwt.issuer', 'https://api.my-awesome-app.io');
|
||||
|
||||
$class = $this->getClass();
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$handler
|
||||
->shouldReceive('handle')
|
||||
->once()
|
||||
->andReturn(new Response(200));
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$request = $request->withHeader('Authorization', 'Bearer ' . $this->getJwt());
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::OK->value, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testJwtFromJwkEndpoint()
|
||||
{
|
||||
Config::set('jwt.audience', 'https://client-app.io');
|
||||
Config::set('jwt.issuer', 'https://api.my-awesome-app.io');
|
||||
|
||||
Redis::partialMock()->shouldReceive('get')->andReturn(null);
|
||||
Redis::shouldReceive('set')->andReturn('OK');
|
||||
Guzzle::partialMock()->shouldReceive('get')
|
||||
->with('https://test.com/.well-known/openid-configuration')
|
||||
->andReturn(new Response(200, [], json_encode([
|
||||
'jwks_uri' => 'https://test.com/keys'
|
||||
], JSON_THROW_ON_ERROR)));
|
||||
|
||||
Guzzle::shouldReceive('get')
|
||||
->with('https://test.com/keys')
|
||||
->andReturn(new Response(200, [], self::TEST_JWKS_JSON));
|
||||
|
||||
Config::set('jwt.signing_key', 'https://test.com/.well-known/openid-configuration');
|
||||
|
||||
$class = $this->getClass();
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
$handler
|
||||
->shouldReceive('handle')
|
||||
->once()
|
||||
->andReturn(new Response(200));
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$request = $request->withHeader('Authorization', 'Bearer ' . $this->getJwtRsa());
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::OK->value, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testCatchesInvalidJwksUrl()
|
||||
{
|
||||
Config::set('jwt.signing_key', 'https://test.com/.well-known/openid-configuration');
|
||||
Redis::partialMock()->shouldReceive('get')->andReturn(null);
|
||||
Redis::shouldReceive('set')->andReturn('OK');
|
||||
Guzzle::partialMock()->shouldReceive('get')
|
||||
->with('https://test.com/.well-known/openid-configuration')
|
||||
->andReturn(new Response(200, [], json_encode([], JSON_THROW_ON_ERROR)));
|
||||
|
||||
|
||||
|
||||
$class = $this->getClass();
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$request = $request->withHeader('Authorization', 'Bearer ' . $this->getJwtRsa());
|
||||
$middleware = new JwtMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::INTERNAL_SERVER_ERROR->value, $response->getStatusCode());
|
||||
}
|
||||
|
||||
private function getJwtRsa(): string
|
||||
{
|
||||
$key = InMemory::plainText(self::TEST_RSA_PRIVATE_KEY);
|
||||
$signer = new \Lcobucci\JWT\Signer\Rsa\Sha256();
|
||||
$token = new JwtFacade()->issue(
|
||||
$signer,
|
||||
$key,
|
||||
static fn(
|
||||
Builder $builder,
|
||||
DateTimeImmutable $issuedAt
|
||||
): Builder => $builder
|
||||
->issuedBy('https://api.my-awesome-app.io')
|
||||
->permittedFor('https://client-app.io')
|
||||
->expiresAt($issuedAt->modify('+10 minutes'))
|
||||
);
|
||||
|
||||
return $token->toString();
|
||||
}
|
||||
|
||||
private function getJwt(): string
|
||||
{
|
||||
$key = InMemory::plainText(self::TEST_SIGNING_KEY);
|
||||
$signer = new Sha256();
|
||||
|
||||
$token = new JwtFacade()->issue(
|
||||
$signer,
|
||||
$key,
|
||||
static fn(
|
||||
Builder $builder,
|
||||
DateTimeImmutable $issuedAt
|
||||
): Builder => $builder
|
||||
->issuedBy('https://api.my-awesome-app.io')
|
||||
->permittedFor('https://client-app.io')
|
||||
->expiresAt($issuedAt->modify('+10 minutes'))
|
||||
);
|
||||
|
||||
return $token->toString();
|
||||
}
|
||||
}
|
||||
32
tests/Http/Middleware/Middleware.php
Normal file
32
tests/Http/Middleware/Middleware.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Http\Middleware;
|
||||
|
||||
use Nyholm\Psr7\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
abstract class Middleware extends Unit
|
||||
{
|
||||
protected 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(
|
||||
ServerRequestInterface $request
|
||||
): ResponseInterface {
|
||||
return $this->response;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
111
tests/Http/Middleware/ScopeMiddlewareTest.php
Normal file
111
tests/Http/Middleware/ScopeMiddlewareTest.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Http\Middleware;
|
||||
|
||||
use League\Route\Dispatcher;
|
||||
use Nyholm\Psr7\Response;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Siteworxpro\App\Attributes\Guards\Scope;
|
||||
use Siteworxpro\App\Http\Middleware\ScopeMiddleware;
|
||||
use Siteworxpro\HttpStatus\CodesEnum;
|
||||
|
||||
class ScopeMiddlewareTest extends Middleware
|
||||
{
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testHandlesNoScopes()
|
||||
{
|
||||
$class = new class {
|
||||
public function getCallable(): array
|
||||
{
|
||||
return [ $this, 'index' ];
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
// Dummy method for testing
|
||||
}
|
||||
};
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$handler
|
||||
->shouldReceive('handle')
|
||||
->once()
|
||||
->andReturn(new Response(200));
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$middleware = new ScopeMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testAllowsWithScope()
|
||||
{
|
||||
$class = new class {
|
||||
public function getCallable(): array
|
||||
{
|
||||
return [ $this, 'index' ];
|
||||
}
|
||||
|
||||
#[Scope(['admin'])]
|
||||
public function index()
|
||||
{
|
||||
// Dummy method for testing
|
||||
}
|
||||
};
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$handler
|
||||
->shouldReceive('handle')
|
||||
->once()
|
||||
->andReturn(new Response(200));
|
||||
|
||||
$request = new ServerRequest('GET', '/')->withAttribute('scope', ['admin', 'user']);
|
||||
$middleware = new ScopeMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::OK->value, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testDisallowsWithScope()
|
||||
{
|
||||
$class = new class {
|
||||
public function getCallable(): array
|
||||
{
|
||||
return [ $this, 'index' ];
|
||||
}
|
||||
|
||||
#[Scope(['admin'])]
|
||||
public function index()
|
||||
{
|
||||
// Dummy method for testing
|
||||
}
|
||||
};
|
||||
|
||||
$handler = \Mockery::mock(Dispatcher::class);
|
||||
$handler->shouldReceive('getMiddlewareStack')
|
||||
->andReturn([$class]);
|
||||
|
||||
$request = new ServerRequest('GET', '/');
|
||||
$middleware = new ScopeMiddleware();
|
||||
$response = $middleware->process($request, $handler);
|
||||
$this->assertEquals(CodesEnum::FORBIDDEN->value, $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
21
tests/Http/Responses/NotFoundResponseTest.php
Normal file
21
tests/Http/Responses/NotFoundResponseTest.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Siteworxpro\Tests\Http\Responses;
|
||||
|
||||
use Siteworxpro\App\Http\Responses\NotFoundResponse;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class NotFoundResponseTest extends Unit
|
||||
{
|
||||
public function testToArray(): void
|
||||
{
|
||||
$response = new NotFoundResponse('/api/resource', ['key' => 'value']);
|
||||
|
||||
$expected = [
|
||||
'message' => 'The requested resource /api/resource was not found.',
|
||||
'context' => ['key' => 'value'],
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $response->toArray());
|
||||
}
|
||||
}
|
||||
93
tests/Http/Responses/ServerErrorResponseTest.php
Normal file
93
tests/Http/Responses/ServerErrorResponseTest.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Siteworxpro\Tests\Http\Responses;
|
||||
|
||||
use Siteworxpro\App\Http\Responses\ServerErrorResponse;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class ServerErrorResponseTest extends Unit
|
||||
{
|
||||
public function testToArrayInDevMode(): void
|
||||
{
|
||||
Config::set('app.dev_mode', true);
|
||||
|
||||
try {
|
||||
// Simulate an exception to generate a server error response
|
||||
throw new \Exception('A Test Error occurred.');
|
||||
} catch (\Exception $e) {
|
||||
$response = new ServerErrorResponse($e, ['operation' => 'data_processing']);
|
||||
|
||||
$expected = [
|
||||
'code' => 500,
|
||||
'message' => 'A Test Error occurred.',
|
||||
'context' => [
|
||||
'operation' => 'data_processing'
|
||||
],
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'trace' => $e->getTrace(),
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $response->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
public function testToArrayNotInDevMode(): void
|
||||
{
|
||||
Config::set('app.dev_mode', false);
|
||||
|
||||
try {
|
||||
throw new \Exception('A Test Error occurred.');
|
||||
} catch (\Exception $exception) {
|
||||
$response = new ServerErrorResponse($exception);
|
||||
|
||||
$expected = [
|
||||
'code' => 500,
|
||||
'message' => 'An internal server error occurred.',
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $response->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
public function testToArrayIfCodeIsSet(): void
|
||||
{
|
||||
Config::set('app.dev_mode', false);
|
||||
|
||||
try {
|
||||
throw new \Exception('A Test Error occurred.', 1234);
|
||||
} catch (\Exception $exception) {
|
||||
$response = new ServerErrorResponse($exception);
|
||||
|
||||
$expected = [
|
||||
'code' => 1234,
|
||||
'message' => 'An internal server error occurred.',
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $response->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
public function testToArrayIfCodeIsSetDevMode(): void
|
||||
{
|
||||
Config::set('app.dev_mode', true);
|
||||
|
||||
try {
|
||||
throw new \Exception('A Test Error occurred.', 1234);
|
||||
} catch (\Exception $exception) {
|
||||
$response = new ServerErrorResponse($exception);
|
||||
|
||||
$expected = [
|
||||
'code' => 1234,
|
||||
'message' => 'A Test Error occurred.',
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
'trace' => $exception->getTrace(),
|
||||
'context' => [],
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $response->toArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
163
tests/Log/LoggerRpcTest.php
Normal file
163
tests/Log/LoggerRpcTest.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Log;
|
||||
|
||||
use Mockery;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use RoadRunner\Logger\Logger as RRLogger;
|
||||
use Siteworxpro\App\Log\Logger;
|
||||
use Siteworxpro\App\Services\Facades\Logger as LoggerFacade;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class LoggerRpcTest extends Unit
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
unset($_SERVER['RR_RPC']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function testLogsDebugMessageWhenLevelIsDebug(): void
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
||||
|
||||
$mock = Mockery::mock(LoggerInterface::class);
|
||||
$mock->expects('debug')
|
||||
->with('message', ['key' => 'value'])
|
||||
->times(1);
|
||||
|
||||
LoggerFacade::getFacadeContainer()
|
||||
->bind(RRLogger::class, function () use ($mock) {
|
||||
return $mock;
|
||||
});
|
||||
|
||||
$inputBuffer = fopen('php://memory', 'r+');
|
||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
||||
$logger->debug('message', ['key' => 'value']);
|
||||
|
||||
$mock->shouldHaveReceived('debug');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsDebugMessageWhenLevelIsInfoNotice(): void
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
||||
|
||||
$mock = Mockery::mock(LoggerInterface::class);
|
||||
$mock->expects('info')
|
||||
->with('message', ['key' => 'value'])
|
||||
->times(2);
|
||||
|
||||
LoggerFacade::getFacadeContainer()
|
||||
->bind(RRLogger::class, function () use ($mock) {
|
||||
return $mock;
|
||||
});
|
||||
|
||||
$inputBuffer = fopen('php://memory', 'r+');
|
||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
||||
$logger->info('message', ['key' => 'value']);
|
||||
$logger->notice('message', ['key' => 'value']);
|
||||
|
||||
$mock->shouldHaveReceived('info')->times(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsDebugMessageWhenLevelIsInfoWarning(): void
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
||||
|
||||
$mock = Mockery::mock(LoggerInterface::class);
|
||||
$mock->expects('warning')
|
||||
->with('message', ['key' => 'value'])
|
||||
->times(1);
|
||||
|
||||
LoggerFacade::getFacadeContainer()
|
||||
->bind(RRLogger::class, function () use ($mock) {
|
||||
return $mock;
|
||||
});
|
||||
|
||||
$inputBuffer = fopen('php://memory', 'r+');
|
||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
||||
$logger->warning('message', ['key' => 'value']);
|
||||
|
||||
$mock->shouldHaveReceived('warning');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsDebugMessageWhenLevelIsInfoError(): void
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
||||
|
||||
$mock = Mockery::mock(LoggerInterface::class);
|
||||
$mock->expects('error')
|
||||
->with('message', ['key' => 'value'])
|
||||
->times(4);
|
||||
|
||||
LoggerFacade::getFacadeContainer()
|
||||
->bind(RRLogger::class, function () use ($mock) {
|
||||
return $mock;
|
||||
});
|
||||
|
||||
$inputBuffer = fopen('php://memory', 'r+');
|
||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
||||
$logger->error('message', ['key' => 'value']);
|
||||
$logger->critical('message', ['key' => 'value']);
|
||||
$logger->alert('message', ['key' => 'value']);
|
||||
$logger->emergency('message', ['key' => 'value']);
|
||||
|
||||
$mock->shouldHaveReceived('error')->times(4);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsLog(): void
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
||||
|
||||
$mock = Mockery::mock(LoggerInterface::class);
|
||||
$mock->expects('log')
|
||||
->with('notaloglevel', 'message', ['key' => 'value']);
|
||||
|
||||
LoggerFacade::getFacadeContainer()
|
||||
->bind(RRLogger::class, function () use ($mock) {
|
||||
return $mock;
|
||||
});
|
||||
|
||||
$inputBuffer = fopen('php://memory', 'r+');
|
||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
||||
$logger->log('notaloglevel', 'message', ['key' => 'value']);
|
||||
|
||||
$mock->shouldHaveReceived('log')->times(1);
|
||||
}
|
||||
}
|
||||
229
tests/Log/LoggerTest.php
Normal file
229
tests/Log/LoggerTest.php
Normal file
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Log;
|
||||
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use Siteworxpro\App\Log\Logger;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class LoggerTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function getLoggerWithBuffer(string $logLevel): array
|
||||
{
|
||||
$inputBuffer = fopen('php://memory', 'r+');
|
||||
return [new Logger($logLevel, $inputBuffer), $inputBuffer];
|
||||
}
|
||||
|
||||
private function getContents($inputBuffer): string
|
||||
{
|
||||
return stream_get_contents($inputBuffer, -1, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function testLogLevel(string $level): void
|
||||
{
|
||||
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($level);
|
||||
$logger->$level('message', ['key' => 'value']);
|
||||
$output = $this->getContents($inputBuffer);
|
||||
|
||||
$this->assertNotEmpty($output);
|
||||
$decoded = json_decode($output, true);
|
||||
$this->assertEquals('message', $decoded['message']);
|
||||
$this->assertEquals('value', $decoded['context']['key']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function testLogLevelEmpty(string $configLevel, string $logLevel): void
|
||||
{
|
||||
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($configLevel);
|
||||
$logger->$logLevel('message', ['key' => 'value']);
|
||||
$output = $this->getContents($inputBuffer);
|
||||
|
||||
$this->assertEmpty($output);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsDebugMessageWhenLevelIsDebug(): void
|
||||
{
|
||||
$this->testLogLevel(LogLevel::DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsInfoMessageWhenLevelIsInfo(): void
|
||||
{
|
||||
$this->testLogLevel(LogLevel::INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsWarningMessageWhenLevelIsWarning(): void
|
||||
{
|
||||
$this->testLogLevel(LogLevel::WARNING);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsErrorMessageWhenLevelIsError(): void
|
||||
{
|
||||
$this->testLogLevel(LogLevel::ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsCriticalMessageWhenLevelIsCritical(): void
|
||||
{
|
||||
$this->testLogLevel(LogLevel::CRITICAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsAlertMessageWhenLevelIsAlert(): void
|
||||
{
|
||||
$this->testLogLevel(LogLevel::ALERT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsEmergencyMessageWhenLevelIsEmergency(): void
|
||||
{
|
||||
$this->testLogLevel(LogLevel::EMERGENCY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsNoticeMessageWhenLevelIsNotice(): void
|
||||
{
|
||||
$this->testLogLevel(LogLevel::NOTICE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testDoesNotLogWhenMinimumLevelIsInfo(): void
|
||||
{
|
||||
$this->testLogLevelEmpty(LogLevel::INFO, LogLevel::DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testDoesNotLogWhenMinimumLevelIsWarning(): void
|
||||
{
|
||||
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::INFO);
|
||||
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ContainerExceptionInterface
|
||||
*/
|
||||
public function testDoesNotLogWhenMinimumLevelIsError(): void
|
||||
{
|
||||
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::DEBUG);
|
||||
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::INFO);
|
||||
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::WARNING);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testDoesNotLogWhenMinimumLevelIsNotice(): void
|
||||
{
|
||||
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::DEBUG);
|
||||
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsMessageWithEmptyContext(): void
|
||||
{
|
||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
||||
|
||||
$logger->info('Message without context');
|
||||
$output = $this->getContents($buffer);
|
||||
|
||||
$this->assertNotEmpty($output);
|
||||
$decoded = json_decode($output, true);
|
||||
$this->assertEquals('Message without context', $decoded['message']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsMessageWithComplexContext(): void
|
||||
{
|
||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
||||
|
||||
$logger->info('Complex context', [
|
||||
'user_id' => 123,
|
||||
'nested' => ['key' => 'value'],
|
||||
'array' => [1, 2, 3]
|
||||
]);
|
||||
$output = $this->getContents($buffer);
|
||||
|
||||
$this->assertNotEmpty($output);
|
||||
$decoded = json_decode($output, true);
|
||||
$this->assertEquals(123, $decoded['context']['user_id']);
|
||||
$this->assertEquals('value', $decoded['context']['nested']['key']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function testLogsStringableMessage(): void
|
||||
{
|
||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
||||
|
||||
$stringable = new class implements \Stringable {
|
||||
public function __toString(): string
|
||||
{
|
||||
return 'Stringable message';
|
||||
}
|
||||
};
|
||||
|
||||
$logger->info($stringable);
|
||||
$output = $this->getContents($buffer);
|
||||
$this->assertNotEmpty($output);
|
||||
$decoded = json_decode($output, true);
|
||||
$this->assertEquals('Stringable message', $decoded['message']);
|
||||
}
|
||||
}
|
||||
30
tests/Models/UserTest.php
Normal file
30
tests/Models/UserTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Models;
|
||||
|
||||
use Siteworxpro\App\Models\User;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class UserTest extends Unit
|
||||
{
|
||||
public function testFormatsName(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->first_name = 'John';
|
||||
$user->last_name = 'Doe';
|
||||
|
||||
$this->assertEquals('John Doe', $user->full_name);
|
||||
}
|
||||
|
||||
public function testFormatsEmail(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->first_name = 'Jane';
|
||||
$user->last_name = 'Smith';
|
||||
$user->email = 'jane.smith@email.com';
|
||||
|
||||
$this->assertEquals('Jane Smith <jane.smith@email.com>', $user->formatted_email);
|
||||
}
|
||||
}
|
||||
43
tests/ServiceProviders/AbstractServiceProvider.php
Normal file
43
tests/ServiceProviders/AbstractServiceProvider.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\ServiceProviders;
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
abstract class AbstractServiceProvider extends Unit
|
||||
{
|
||||
abstract protected function getProviderClass(): string;
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function testProvider(): void
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
$providerClass = $this->getProviderClass();
|
||||
|
||||
/** @var ServiceProvider $providerClass */
|
||||
$provider = new $providerClass($container);
|
||||
|
||||
$this->assertInstanceOf($providerClass, $provider);
|
||||
$provider->register();
|
||||
|
||||
$abstract = $provider->provides()[0];
|
||||
$concrete = get_class($container->make($abstract));
|
||||
|
||||
$this->assertTrue($container->bound($abstract), "The $abstract is not bound in the container.");
|
||||
$this->assertNotNull($container->make($abstract), "The $abstract could not be resolved.");
|
||||
|
||||
$this->assertInstanceOf(
|
||||
$concrete,
|
||||
$container->make($abstract),
|
||||
"The $abstract is not an instance of $concrete."
|
||||
);
|
||||
}
|
||||
}
|
||||
15
tests/ServiceProviders/CommandBusServiceProviderTest.php
Normal file
15
tests/ServiceProviders/CommandBusServiceProviderTest.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\ServiceProviders;
|
||||
|
||||
use Siteworxpro\App\Services\ServiceProviders\CommandBusProvider;
|
||||
|
||||
class CommandBusServiceProviderTest extends AbstractServiceProvider
|
||||
{
|
||||
protected function getProviderClass(): string
|
||||
{
|
||||
return CommandBusProvider::class;
|
||||
}
|
||||
}
|
||||
15
tests/ServiceProviders/DispatcherServiceProviderTest.php
Normal file
15
tests/ServiceProviders/DispatcherServiceProviderTest.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\ServiceProviders;
|
||||
|
||||
use Siteworxpro\App\Services\ServiceProviders\DispatcherServiceProvider;
|
||||
|
||||
class DispatcherServiceProviderTest extends AbstractServiceProvider
|
||||
{
|
||||
protected function getProviderClass(): string
|
||||
{
|
||||
return DispatcherServiceProvider::class;
|
||||
}
|
||||
}
|
||||
15
tests/ServiceProviders/LoggerServiceProviderTest.php
Normal file
15
tests/ServiceProviders/LoggerServiceProviderTest.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\ServiceProviders;
|
||||
|
||||
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
||||
|
||||
class LoggerServiceProviderTest extends AbstractServiceProvider
|
||||
{
|
||||
protected function getProviderClass(): string
|
||||
{
|
||||
return LoggerServiceProvider::class;
|
||||
}
|
||||
}
|
||||
15
tests/ServiceProviders/RedisServiceProviderTest.php
Normal file
15
tests/ServiceProviders/RedisServiceProviderTest.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\ServiceProviders;
|
||||
|
||||
use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider;
|
||||
|
||||
class RedisServiceProviderTest extends AbstractServiceProvider
|
||||
{
|
||||
protected function getProviderClass(): string
|
||||
{
|
||||
return RedisServiceProvider::class;
|
||||
}
|
||||
}
|
||||
48
tests/Unit.php
Normal file
48
tests/Unit.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests;
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Siteworx\Config\Config as SWConfig;
|
||||
use Siteworxpro\App\Kernel;
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
|
||||
abstract class Unit extends TestCase
|
||||
{
|
||||
protected function getContainer(): Container
|
||||
{
|
||||
$container = Facade::getFacadeContainer();
|
||||
if ($container === null) {
|
||||
$container = new Container();
|
||||
Facade::setFacadeContainer($container);
|
||||
|
||||
return $container;
|
||||
}
|
||||
|
||||
return $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$container->bind(SWConfig::class, function () {
|
||||
return SWConfig::load(__DIR__ . '/../config.php');
|
||||
});
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Config::clearResolvedInstances();
|
||||
Facade::setFacadeContainer(null);
|
||||
Mockery::close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user