Skip to content

Commit

Permalink
Tests: cover more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jun 9, 2023
1 parent 2cb81df commit 9fc1504
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/DI/FrameXExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getConfigSchema(): Schema
),
'routing' => Expect::array(
Expect::structure([
'method' => Expect::string()->required(),
'method' => Expect::anyOf('get', 'post', 'put', 'delete', 'options')->before(fn ($method) => strtolower($method))->required(),
'path' => Expect::string()->required(),
'controller' => $expectService,
])->required()
Expand Down Expand Up @@ -78,12 +78,16 @@ public function loadConfiguration(): void
}
}

$builder->addDefinition($this->prefix('application'))
->setFactory(Application::class)
$applicationDef = $builder->addDefinition($this->prefix('application'));
$applicationDef->setFactory(Application::class)
->setArguments([
$this->prefix('@container'),
array_map(fn (string $service) => $builder->getDefinition($service), array_keys($builder->findByTag(self::MIDDLEWARE_TAG))),
]);

foreach ($config->routing as $route) {
$applicationDef->addSetup(strtolower($route['method']), [$route['path'], $route['controller']]);
}
}

}
60 changes: 60 additions & 0 deletions tests/Cases/DI/FrameXExtension.E2E.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

use Contributte\FrameX\Application;
use Contributte\FrameX\DI\FrameXExtension;
use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Contributte\Tester\Utils\Liberator;
use Contributte\Tester\Utils\Neonkit;
use Nette\DI\Compiler;
use React\Http\Message\ServerRequest;
use Tester\Assert;
use Tests\Fixtures\TestHandler;

require_once __DIR__ . '/../../bootstrap.php';

// E2E
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('framex', new FrameXExtension());
$compiler->addConfig(Neonkit::load(<<<'NEON'
framex:
middlewares: []
routing:
- { path: /test, method: get, controller: Tests\Fixtures\TestController }
services:
- Tests\Fixtures\TestController
NEON
));
})
->build();

/** @var Application $app */
$app = $container->getByType(Application::class);

// Test to /test endpoint
$sapi = TestHandler::of(new ServerRequest('GET', '/test'));
Liberator::of(Liberator::of($app)->app)->sapi = $sapi;
$app->run();

Assert::equal(200, $sapi->response->getStatusCode());
Assert::equal('test', $sapi->response->getBody()->getContents());

// Send to fake address
$sapi = TestHandler::of(new ServerRequest('GET', '/fake'));
Liberator::of(Liberator::of($app)->app)->sapi = $sapi;
$app->run();

Assert::equal(404, $sapi->response->getStatusCode());
Assert::match('%A%Error 404: Page Not Found%A%', $sapi->response->getBody()->getContents());

// Send to invalid HTTP method
$sapi = TestHandler::of(new ServerRequest('POST', '/test'));
Liberator::of(Liberator::of($app)->app)->sapi = $sapi;
$app->run();

Assert::equal(405, $sapi->response->getStatusCode());
Assert::match('%A%Error 405: Method Not Allowed%A%', $sapi->response->getBody()->getContents());
});
24 changes: 24 additions & 0 deletions tests/Cases/DI/FrameXExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Contributte\FrameX\Application;
use Contributte\FrameX\DI\FrameXExtension;
use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Contributte\Tester\Utils\Liberator;
use Contributte\Tester\Utils\Neonkit;
use Nette\DI\Compiler;
use Nette\DI\InvalidConfigurationException;
Expand Down Expand Up @@ -52,6 +53,29 @@ Toolkit::test(function (): void {
Assert::count(0, $container->findByTag(FrameXExtension::MIDDLEWARE_TAG));
});

// Middlewares
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('framex', new FrameXExtension());
$compiler->addConfig([
'parameters' => [
'debugMode' => false,
],
]);
$compiler->addConfig(Neonkit::load(<<<'NEON'
framex:
routing:
- { path: test, method: get, controller: test }
NEON
));
})
->build();

$tracyMiddleware = $container->getService('framex.middleware.tracy');
Assert::false(Liberator::of($tracyMiddleware)->enable);
});

// No routing
Toolkit::test(function (): void {
Assert::exception(
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

class TestController
{

public function __invoke(ServerRequestInterface $request): ResponseInterface
{
return Response::html('test');
}

}
29 changes: 29 additions & 0 deletions tests/Fixtures/TestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\ServerRequest;

class TestHandler
{

public ServerRequestInterface $request;

public ResponseInterface $response;

public static function of(ServerRequest $param): self
{
$self = new self();
$self->request = $param;

return $self;
}

public function run(callable $handler): void
{
$this->response = $handler($this->request);
}

}

0 comments on commit 9fc1504

Please sign in to comment.