mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
Development
- Added dependency injection support. - Added php-di composer dependency. - Added `ClassLoader` class. - Added `IClassLoader` interface. - Added unit-tests for dependency injection. - Updated documentation to reflect new features.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
|
||||
class DependencyInjectionTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testDependencyInjectionDevelopment()
|
||||
{
|
||||
$builder = new \DI\ContainerBuilder();
|
||||
$container = $builder
|
||||
->useAutowiring(true)
|
||||
->ignorePhpDocErrors(true)
|
||||
->build();
|
||||
|
||||
TestRouter::enableDependencyInjection($container);
|
||||
|
||||
$className = null;
|
||||
|
||||
TestRouter::get('/', function (DummyMiddleware $url) use (&$className) {
|
||||
$className = \get_class($url);
|
||||
});
|
||||
|
||||
TestRouter::debug('/');
|
||||
|
||||
$this->assertEquals(DummyMiddleware::class, $className);
|
||||
}
|
||||
|
||||
public function testDependencyInjectionProduction()
|
||||
{
|
||||
$cacheDir = dirname(__DIR__, 2) . '/tmp';
|
||||
|
||||
$builder = new \DI\ContainerBuilder();
|
||||
$builder
|
||||
->enableCompilation($cacheDir)
|
||||
->writeProxiesToFile(true, $cacheDir . '/proxies')
|
||||
->ignorePhpDocErrors(true)
|
||||
->useAutowiring(true);
|
||||
|
||||
$container = $builder->build();
|
||||
|
||||
TestRouter::enableDependencyInjection($container);
|
||||
|
||||
$className = null;
|
||||
|
||||
TestRouter::get('/', function (DummyMiddleware $url) use (&$className) {
|
||||
$className = \get_class($url);
|
||||
});
|
||||
|
||||
TestRouter::debug('/');
|
||||
|
||||
$this->assertEquals(DummyMiddleware::class, $className);
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,13 @@ require_once 'Dummy/DummyController.php';
|
||||
|
||||
class GroupTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected $result;
|
||||
|
||||
public function testGroupLoad()
|
||||
{
|
||||
$this->result = false;
|
||||
$result = false;
|
||||
|
||||
TestRouter::group(['prefix' => '/group'], function () {
|
||||
$this->result = true;
|
||||
TestRouter::group(['prefix' => '/group'], function () use(&$result) {
|
||||
$result = true;
|
||||
});
|
||||
|
||||
try {
|
||||
@@ -20,7 +19,7 @@ class GroupTest extends \PHPUnit\Framework\TestCase
|
||||
} catch(\Exception $e) {
|
||||
|
||||
}
|
||||
$this->assertTrue($this->result);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testNestedGroup()
|
||||
|
||||
@@ -6,21 +6,20 @@ require_once 'Dummy/Exception/ExceptionHandlerException.php';
|
||||
|
||||
class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected $result = false;
|
||||
|
||||
public function testMultiParam()
|
||||
{
|
||||
TestRouter::get('/test-{param1}-{param2}', function ($param1, $param2) {
|
||||
$result = false;
|
||||
TestRouter::get('/test-{param1}-{param2}', function ($param1, $param2) use(&$result) {
|
||||
|
||||
if ($param1 === 'param1' && $param2 === 'param2') {
|
||||
$this->result = true;
|
||||
$result = true;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
TestRouter::debug('/test-param1-param2', 'get');
|
||||
|
||||
$this->assertTrue($this->result);
|
||||
$this->assertTrue($result);
|
||||
|
||||
}
|
||||
|
||||
@@ -92,19 +91,19 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
public function testDomainAllowedRoute()
|
||||
{
|
||||
$this->result = false;
|
||||
$result = false;
|
||||
TestRouter::request()->setHost('hello.world.com');
|
||||
|
||||
TestRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
||||
TestRouter::get('/test', function ($subdomain = null) {
|
||||
$this->result = ($subdomain === 'hello');
|
||||
TestRouter::group(['domain' => '{subdomain}.world.com'], function () use(&$result) {
|
||||
TestRouter::get('/test', function ($subdomain = null) use(&$result) {
|
||||
$result = ($subdomain === 'hello');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
TestRouter::debug('/test', 'get');
|
||||
|
||||
$this->assertTrue($this->result);
|
||||
$this->assertTrue($result);
|
||||
|
||||
}
|
||||
|
||||
@@ -112,17 +111,17 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
TestRouter::request()->setHost('other.world.com');
|
||||
|
||||
$this->result = false;
|
||||
$result = false;
|
||||
|
||||
TestRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
||||
TestRouter::get('/test', function ($subdomain = null) {
|
||||
$this->result = ($subdomain === 'hello');
|
||||
TestRouter::group(['domain' => '{subdomain}.world.com'], function () use(&$result) {
|
||||
TestRouter::get('/test', function ($subdomain = null) use(&$result) {
|
||||
$result = ($subdomain === 'hello');
|
||||
});
|
||||
});
|
||||
|
||||
TestRouter::debug('/test', 'get');
|
||||
|
||||
$this->assertFalse($this->result);
|
||||
$this->assertFalse($result);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user