From e94f02eed2ff47eb2a91a165a9a1694200697c10 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 15:16:53 +0000 Subject: [PATCH 01/32] Installed rector --- composer.json | 3 ++- rector.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 6c76544..549f852 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ }, "require-dev": { "phpunit/phpunit": "4.3.5", - "mockery/mockery": "0.8.*" + "mockery/mockery": "0.8.*", + "rector/rector": "^0.12.15" }, "autoload": { "psr-0": { diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..0e1a268 --- /dev/null +++ b/rector.php @@ -0,0 +1,28 @@ +parameters(); + $parameters->set(Option::PATHS, [ + __DIR__ . '/src', + __DIR__ . '/tests', + ]); + + // Define what rule sets will be applied + $containerConfigurator->import(LevelSetList::UP_TO_PHP_74); + $containerConfigurator->import(PHPUnitSetList::PHPUNIT_90); + + // get services (needed for register a single rule) + // $services = $containerConfigurator->services(); + + // register a single rule + // $services->set(TypedPropertyRector::class); +}; From 4dee63396129b673da894fe2d3daf25ef3033845 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 15:17:40 +0000 Subject: [PATCH 02/32] Added .phpunit.result.cache to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8f2077f..5ae9f1e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea vendor/ -composer.lock \ No newline at end of file +composer.lock +.phpunit.result.cache \ No newline at end of file From 5b6d8e1c663177cdb385d084bee28cca97f18f35 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 15:33:50 +0000 Subject: [PATCH 03/32] Updated to php 7.4 and phpunit 9 suppoert --- composer.json | 2 +- phpunit.xml.dist | 6 - rector.php | 4 +- src/SlimController/Slim.php | 4 +- src/SlimController/SlimController.php | 16 +- .../Fixtures/Controller/TestController.php | 10 +- .../Integration/CanCreateApplicationTest.php | 4 +- .../Tests/Old/ControllerTest.php | 15 +- tests/SlimController/Tests/Old/ParamsTest.php | 40 ++++- tests/SlimController/Tests/Old/RenderTest.php | 5 +- .../SlimController/Tests/Old/RoutingTest.php | 10 +- .../Tests/SlimControllerTest.php | 10 +- tests/SlimController/Tests/SlimTest.php | 146 ++++++++---------- tests/SlimController/Tests/TestCase.php | 2 +- 14 files changed, 138 insertions(+), 136 deletions(-) diff --git a/composer.json b/composer.json index 549f852..ea7af3b 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "slim/slim": "2.*" }, "require-dev": { - "phpunit/phpunit": "4.3.5", + "phpunit/phpunit": "^9", "mockery/mockery": "0.8.*", "rector/rector": "^0.12.15" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3759e81..ac6d7d2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,6 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" > @@ -16,9 +15,4 @@ ./tests/SlimController/Tests/Old/ - - - src - - \ No newline at end of file diff --git a/rector.php b/rector.php index 0e1a268..10cb00b 100644 --- a/rector.php +++ b/rector.php @@ -5,7 +5,7 @@ use Rector\Core\Configuration\Option; use Rector\Php74\Rector\Property\TypedPropertyRector; use Rector\Set\ValueObject\LevelSetList; -use Rector\PHPUnit\Set\PHPUnitSetList; +use Rector\PHPUnit\Set\PHPUnitLevelSetList; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { @@ -18,7 +18,7 @@ // Define what rule sets will be applied $containerConfigurator->import(LevelSetList::UP_TO_PHP_74); - $containerConfigurator->import(PHPUnitSetList::PHPUNIT_90); + $containerConfigurator->import(PHPUnitLevelSetList::UP_TO_PHPUNIT_90 ); // get services (needed for register a single rule) // $services = $containerConfigurator->services(); diff --git a/src/SlimController/Slim.php b/src/SlimController/Slim.php index abda757..b5567d2 100644 --- a/src/SlimController/Slim.php +++ b/src/SlimController/Slim.php @@ -165,7 +165,7 @@ public function addControllerRoute($path, $route, array $middleware = array()) */ protected function buildCallbackFromControllerRoute($route) { - list($controller, $methodName) = $this->determineClassAndMethod($route); + [$controller, $methodName] = $this->determineClassAndMethod($route); $app = & $this; $callable = function () use ($app, $controller, $methodName) { // Get action arguments @@ -190,7 +190,7 @@ protected function determineClassAndMethod($classMethod) // determine class prefix (eg "\Vendor\Bundle\Controller") and suffix (eg "Controller") $classNamePrefix = $this->config('controller.class_prefix'); - if ($classNamePrefix && substr($classNamePrefix, -strlen($classNamePrefix) !== '\\')) { + if ($classNamePrefix && substr($classNamePrefix, -strlen($classNamePrefix) !== 0)) { $classNamePrefix .= '\\'; } $classNameSuffix = $this->config('controller.class_suffix') ? : ''; diff --git a/src/SlimController/SlimController.php b/src/SlimController/SlimController.php index 303d99d..48649c3 100644 --- a/src/SlimController/SlimController.php +++ b/src/SlimController/SlimController.php @@ -25,7 +25,7 @@ abstract class SlimController /** * @const string */ - const VERSION = '0.1.4'; + public const VERSION = '0.1.4'; /** * @var Slim @@ -40,22 +40,22 @@ abstract class SlimController /** * @var string Prefix for params */ - private $paramPrefix = 'data.'; + private string $paramPrefix = 'data.'; /** * @var array Stash of GET & POST params */ - private $paramsParams = null; + private ?array $paramsParams = null; /** * @var array Stash of GET params */ - private $paramsGet = null; + private ?array $paramsGet = null; /** * @var array Stash of POST params */ - private $paramsPost = null; + private ?array $paramsPost = null; /** * Suffix was never specified and defaults to empty string @@ -277,11 +277,7 @@ private function getAllParamNames($reqMode) $names = array_keys($namesPre); if ($prefix = $this->paramPrefix) { $prefixLen = strlen($prefix); - $names = array_map(function ($key) use ($prefixLen) { - return substr($key, $prefixLen); - }, array_filter($names, function ($in) use ($prefix) { - return strpos($in, $prefix) === 0; - })); + $names = array_map(fn($key) => substr($key, $prefixLen), array_filter($names, fn($in) => strpos($in, $prefix) === 0)); } return $names; diff --git a/tests/SlimController/Tests/Fixtures/Controller/TestController.php b/tests/SlimController/Tests/Fixtures/Controller/TestController.php index 21e2b64..97cbcb9 100644 --- a/tests/SlimController/Tests/Fixtures/Controller/TestController.php +++ b/tests/SlimController/Tests/Fixtures/Controller/TestController.php @@ -31,31 +31,31 @@ public function paramSingleArrayAction() public function paramMultiAction() { $params = $this->params(array('Some.param', 'Other.param', 'Other.missing')); - echo json_encode($params); + echo json_encode($params, JSON_THROW_ON_ERROR); } public function paramMultiMissingReqAction() { $params = $this->params(array('Some.param', 'Other.param'), 'get', true); - echo json_encode($params); + echo json_encode($params, JSON_THROW_ON_ERROR); } public function paramMultiDefaultAction() { $params = $this->params(array('Some.param', 'Other.param', 'Other.bla'), 'get', array('Other.bla' => 'great')); - echo json_encode($params); + echo json_encode($params, JSON_THROW_ON_ERROR); } public function paramGetAllAction() { $params = $this->params(); - echo json_encode($params); + echo json_encode($params, JSON_THROW_ON_ERROR); } public function paramCleanupAction() { $messedUp = array('foo', 'Notgood'); - echo json_encode($this->cleanupParam($messedUp)); + echo json_encode($this->cleanupParam($messedUp), JSON_THROW_ON_ERROR); } public function renderAction() diff --git a/tests/SlimController/Tests/Integration/CanCreateApplicationTest.php b/tests/SlimController/Tests/Integration/CanCreateApplicationTest.php index 9dd5ca3..7ea887d 100644 --- a/tests/SlimController/Tests/Integration/CanCreateApplicationTest.php +++ b/tests/SlimController/Tests/Integration/CanCreateApplicationTest.php @@ -2,13 +2,13 @@ namespace SlimController\Tests\Integration; -class CanCreateApplicationTest extends \PHPUnit_Framework_TestCase +class CanCreateApplicationTest extends \PHPUnit\Framework\TestCase { public function testCanCreateSimpleApplication() { $app = new \SlimController\Slim(); - $this->assertTrue(true); // if we got this far then creating the application worked + static::assertTrue(true); // if we got this far then creating the application worked } } diff --git a/tests/SlimController/Tests/Old/ControllerTest.php b/tests/SlimController/Tests/Old/ControllerTest.php index 7674100..8337dfe 100644 --- a/tests/SlimController/Tests/Old/ControllerTest.php +++ b/tests/SlimController/Tests/Old/ControllerTest.php @@ -5,6 +5,9 @@ class ControllerTest extends TestCase { + /** + * @doesNotPerformAssertions + */ public function testControllerSimple() { $this->expectOutputString('What is up?'); @@ -13,11 +16,14 @@ public function testControllerSimple() '/' => 'Test:index', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testControllerExtended() { $this->expectOutputString('What is up YOU?'); @@ -25,11 +31,14 @@ public function testControllerExtended() $this->app->addRoutes(array( '/hello/:name' => 'Test:hello', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testControllerAbsPath() { $this->expectOutputString('What is up YOU?'); @@ -37,7 +46,7 @@ public function testControllerAbsPath() $this->app->addRoutes(array( '/hello/:name' => 'Test:hello', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } diff --git a/tests/SlimController/Tests/Old/ParamsTest.php b/tests/SlimController/Tests/Old/ParamsTest.php index 984d5da..37192f6 100644 --- a/tests/SlimController/Tests/Old/ParamsTest.php +++ b/tests/SlimController/Tests/Old/ParamsTest.php @@ -6,6 +6,9 @@ class ParamsTest extends TestCase { + /** + * @doesNotPerformAssertions + */ public function testParamsSingle() { $this->expectOutputString('Param is 123'); @@ -13,12 +16,15 @@ public function testParamsSingle() $this->app->addRoutes(array( '/' => 'Test:paramSingle', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testParamsSingleObject() { $this->expectOutputString('Param is 123123123'); @@ -26,11 +32,14 @@ public function testParamsSingleObject() $this->app->addRoutes(array( '/' => 'Test:paramSingleObject', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testParamsMulti() { $this->expectOutputString('All is foo bar'); @@ -38,11 +47,14 @@ public function testParamsMulti() $this->app->addRoutes(array( '/' => 'Test:paramMulti', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testParamsMultiMissing() { $this->expectOutputString('All is foo bar'); @@ -50,11 +62,14 @@ public function testParamsMultiMissing() $this->app->addRoutes(array( '/' => 'Test:paramMultiMissing', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testParamsMultiMissingReq() { $this->expectOutputString('OK'); @@ -62,11 +77,14 @@ public function testParamsMultiMissingReq() $this->app->addRoutes(array( '/' => 'Test:paramMultiMissingReq', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testParamsMultiDefault() { $this->expectOutputString('All is foo bar and great'); @@ -74,11 +92,14 @@ public function testParamsMultiDefault() $this->app->addRoutes(array( '/' => 'Test:paramMultiDefault', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testParamsDifferentPrefix() { $this->expectOutputString('GOT OK'); @@ -88,11 +109,14 @@ public function testParamsDifferentPrefix() $this->app->addRoutes(array( '/' => 'Test:paramDifferentPrefix', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } + /** + * @doesNotPerformAssertions + */ public function testParamsNoPrefix() { $this->expectOutputString('All params: data.Foo=bar - other.Foo=bar'); @@ -102,7 +126,7 @@ public function testParamsNoPrefix() $this->app->addRoutes(array( '/' => 'Test:paramNoPrefix', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } diff --git a/tests/SlimController/Tests/Old/RenderTest.php b/tests/SlimController/Tests/Old/RenderTest.php index 9f4ccbf..c471858 100644 --- a/tests/SlimController/Tests/Old/RenderTest.php +++ b/tests/SlimController/Tests/Old/RenderTest.php @@ -5,6 +5,9 @@ class RenderTest extends TestCase { + /** + * @doesNotPerformAssertions + */ public function testParamsMultiDefault() { $this->expectOutputString('This is orotound and grandios'); @@ -12,7 +15,7 @@ public function testParamsMultiDefault() $this->app->addRoutes(array( '/' => 'Test:render', )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); //$this->app->router()->dispatch($route); $route->dispatch(); } diff --git a/tests/SlimController/Tests/Old/RoutingTest.php b/tests/SlimController/Tests/Old/RoutingTest.php index d3cb095..daf6b9b 100644 --- a/tests/SlimController/Tests/Old/RoutingTest.php +++ b/tests/SlimController/Tests/Old/RoutingTest.php @@ -11,17 +11,17 @@ public function testAddSimpleRoutes() $this->app->addRoutes(array( '/' => 'Controller:index', )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); $this->setUrl('/foo'); - $this->assertEquals(0, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(0, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); $this->setUrl('/other'); $this->app->addRoutes(array( '/other' => 'Controller:other', )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testRoutesWithVariables() @@ -30,7 +30,7 @@ public function testRoutesWithVariables() $this->app->addRoutes(array( '/hello/:name' => 'Controller:index', )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testRoutesWithExtendedFormat() @@ -39,7 +39,7 @@ public function testRoutesWithExtendedFormat() $this->app->addRoutes(array( '/bla' => array('Controller:index', 'get') )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } } diff --git a/tests/SlimController/Tests/SlimControllerTest.php b/tests/SlimController/Tests/SlimControllerTest.php index 3fdbdb6..5ec0cc3 100644 --- a/tests/SlimController/Tests/SlimControllerTest.php +++ b/tests/SlimController/Tests/SlimControllerTest.php @@ -9,20 +9,20 @@ use SlimController\SlimController; use SlimController\Tests\Fixtures\Controller\TestController; -class SlimControllerTest extends \PHPUnit_Framework_TestCase +class SlimControllerTest extends \PHPUnit\Framework\TestCase { /** * @var \Mockery\MockInterface */ protected $slim; - public function setUp() + protected function setUp(): void { - $this->slim = m::mock('\Slim\Slim'); + $this->slim = m::mock(\Slim\Slim::class); parent::setUp(); } - public function tearDown() + protected function tearDown(): void { $this->addToAssertionCount($this->slim->mockery_getExpectationCount()); m::close(); @@ -44,7 +44,7 @@ public function testControllerConfigParamsAreUsed() ->with('controller.cleanup_params') ->andReturnNull(); $controller = new TestController($this->slim); - $this->assertTrue(true); + static::assertTrue(true); } public function testRenderingWorksFine() diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 24f13c5..79743d0 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -20,9 +20,9 @@ public function testAddingRoutesToSameMethod() '/alb' => array('get' => 'Controller:index') )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); // $this->assertTrue($this->app->router->hasNamedRoute('Controller:index')); - $this->assertEquals('/bla', $this->app->urlFor('Controller:index')); + static::assertEquals('/bla', $this->app->urlFor('Controller:index')); } public function testAddingroutesWithOldSyntaxWithoutMiddlewares() @@ -32,7 +32,7 @@ public function testAddingroutesWithOldSyntaxWithoutMiddlewares() '/bla' => array('Controller:index'), )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddRoutesWithOldSyntaxWithoutMiddlewareArray() @@ -43,7 +43,7 @@ public function testAddRoutesWithOldSyntaxWithoutMiddlewareArray() // }) )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddRoutesWithOldSyntaxWithMiddlewareArray() @@ -54,7 +54,7 @@ public function testAddRoutesWithOldSyntaxWithMiddlewareArray() // })) )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddSimpleRoutes() @@ -63,17 +63,17 @@ public function testAddSimpleRoutes() $this->app->addRoutes(array( '/' => 'Controller:index', )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); $this->setUrl('/foo'); - $this->assertEquals(0, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(0, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); $this->setUrl('/other'); $this->app->addRoutes(array( '/other' => 'Controller:other', )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddRoutesWithVariables() @@ -82,7 +82,7 @@ public function testAddRoutesWithVariables() $this->app->addRoutes(array( '/hello/:name' => 'Controller:index', )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddRoutesInExtendedFormat() @@ -91,15 +91,13 @@ public function testAddRoutesInExtendedFormat() $this->app->addRoutes(array( '/bla' => array('get' => 'Controller:index') )); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Malformed class action for 'Controller:index:foo'. Use 'className:methodName' format. - */ public function testFailToAddInvalidClassMethodFormat() { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Malformed class action for \'Controller:index:foo\'. Use \'className:methodName\' format.'); $this->setUrl('/bla'); $this->app->addRoutes(array( '/bla' => 'Controller:index:foo' @@ -111,24 +109,22 @@ public function testGlobalMiddlewareIsAddedToRoute() $this->setUrl('/bla'); $this->app->addRoutes(array( '/bla' => 'Controller:index' - ), function() { - return false; - }); + ), fn() => false); /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - $this->assertEquals(1, count($routes)); + static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); - $this->assertInternalType('array', $middleware); - $this->assertSame(1, count($middleware)); + static::assertIsArray($middleware); + static::assertSame(1, count($middleware)); } public function testGlobalMiddlewareIsAddedToRouteAsArray() { $middlewares = array( - function() { return false; }, - function() { return false; } + fn() => false, + fn() => false ); $this->setUrl('/bla'); @@ -138,36 +134,34 @@ function() { return false; } /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - $this->assertEquals(1, count($routes)); + static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); - $this->assertInternalType('array', $middleware); - $this->assertSame(2, count($middleware)); + static::assertIsArray($middleware); + static::assertSame(2, count($middleware)); } public function testLocalMiddlewareIsAddedToRoute() { $this->setUrl('/bla'); $this->app->addRoutes(array( - '/bla' => array('get' => array('Controller:index', function() { - return false; - })) + '/bla' => array('get' => array('Controller:index', fn() => false)) )); /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - $this->assertEquals(1, count($routes)); + static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); - $this->assertInternalType('array', $middleware); - $this->assertSame(1, count($middleware)); + static::assertIsArray($middleware); + static::assertSame(1, count($middleware)); } public function testArrayOfLocalMiddlewareIsAddedToRoute() { $middlewares = array( - function() { return false; }, - function() { return false; } + fn() => false, + fn() => false ); $this->setUrl('/bla'); @@ -177,18 +171,18 @@ function() { return false; } /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - $this->assertEquals(1, count($routes)); + static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); - $this->assertInternalType('array', $middleware); - $this->assertSame(2, count($middleware)); + static::assertIsArray($middleware); + static::assertSame(2, count($middleware)); } public function testLocalMiddlewaresAreAddedToRoute() { $middlewares = array( - function() { return false; }, - function() { return false; } + fn() => false, + fn() => false ); $this->setUrl('/bla'); @@ -198,41 +192,33 @@ function() { return false; } /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - $this->assertEquals(1, count($routes)); + static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); - $this->assertInternalType('array', $middleware); - $this->assertSame(2, count($middleware)); + static::assertIsArray($middleware); + static::assertSame(2, count($middleware)); } public function testGlobalAndLocalMiddlewareIsAddedToRoute() { $this->setUrl('/bla'); $this->app->addRoutes(array( - '/bla' => array('get' => array('Controller:index', function() { - return false; - })) - ), array(function() { - return false; - }, function() { - return false; - })); + '/bla' => array('get' => array('Controller:index', fn() => false)) + ), array(fn() => false, fn() => false)); /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - $this->assertEquals(1, count($routes)); + static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); - $this->assertInternalType('array', $middleware); - $this->assertSame(3, count($middleware)); + static::assertIsArray($middleware); + static::assertSame(3, count($middleware)); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Http method 'FOO' is not supported. - */ public function testFailToAddRouteForUnsupportedHttpMethod() { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Http method \'FOO\' is not supported.'); $this->setUrl('/bla'); $this->app->addRoutes(array( '/bla' => array('foo' => 'Controller:index') @@ -246,7 +232,7 @@ public function testRouteCallbacksAreFiredOnDispatch() $this->app->addRoutes(array( '/bla' => 'Test:index' )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); $route->dispatch(); } @@ -259,7 +245,7 @@ public function testEmptyButNotNullMethodSuffixAccepted() $this->app->addRoutes(array( '/bla' => 'Test:notSuffixedMethod' )); - list($route) = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); $route->dispatch(); } @@ -270,7 +256,7 @@ public function testAddControllerRoute() '/', 'Controller:index' )->via('GET'); - $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddControllerRouteWithMiddleware() @@ -278,19 +264,17 @@ public function testAddControllerRouteWithMiddleware() $this->setUrl('/'); $this->app->addControllerRoute( '/', 'Controller:index', array( - function() { - return false; - }, + fn() => false, ) )->via('GET'); /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - $this->assertEquals(1, count($routes)); + static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); - $this->assertInternalType('array', $middleware); - $this->assertSame(1, count($middleware)); + static::assertIsArray($middleware); + static::assertSame(1, count($middleware)); } public function testNamedRoutes() @@ -302,17 +286,15 @@ public function testNamedRoutes() '/something/:id' => 'Something:show' )); - $this->assertEquals('/', $this->app->urlFor('Controller:index')); - $this->assertEquals('/bla', $this->app->urlFor('Bla:Index')); - $this->assertEquals('/something/:id', $this->app->urlFor('Something:show')); + static::assertEquals('/', $this->app->urlFor('Controller:index')); + static::assertEquals('/bla', $this->app->urlFor('Bla:Index')); + static::assertEquals('/something/:id', $this->app->urlFor('Something:show')); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage Named route not found for name: this is not a named route - */ public function testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotExist() { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Named route not found for name: this is not a named route'); $this->setUrl('/'); $this->app->addRoutes(array( '/' => 'Controller:index', @@ -320,7 +302,7 @@ public function testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotExist '/something/:id' => 'Something:show' )); - $this->assertEquals('/', $this->app->urlFor('this is not a named route')); + static::assertEquals('/', $this->app->urlFor('this is not a named route')); } public function testServiceControllersAreFetched() @@ -333,9 +315,7 @@ public function testServiceControllersAreFetched() ); $this->setUrl('/', '', $config); $app = $this->app; - $app->container->singleton('TestController', function () use ($app) { - return new TestController($app); - }); + $app->container->singleton('TestController', fn() => new TestController($app)); $route = $this->app->addControllerRoute( '/', 'TestController:index' @@ -343,7 +323,7 @@ public function testServiceControllersAreFetched() // If the route could be dispatched, then the service was found $result = $route->dispatch(); - $this->assertTrue($result); + static::assertTrue($result); } public function testServiceControllersAreFetchedWithParams() @@ -356,16 +336,14 @@ public function testServiceControllersAreFetchedWithParams() ); $this->setUrl('/', '', $config); $app = $this->app; - $app->container->singleton('TestController', function () use ($app) { - return new TestController($app); - }); + $app->container->singleton('TestController', fn() => new TestController($app)); $app->addRoutes(array( '/another/:name' => 'TestController:hello' )); $route = $app->router()->getNamedRoute('TestController:hello'); $route->setParams(array('name' => 'foo')); - $this->assertTrue($route->dispatch()); + static::assertTrue($route->dispatch()); } public function testServiceControllersAreFetchedEvenIfTheirNameIsAnInvalidPHPClassName() @@ -378,9 +356,7 @@ public function testServiceControllersAreFetchedEvenIfTheirNameIsAnInvalidPHPCla ); $this->setUrl('/', '', $config); $app = $this->app; - $app->container->singleton('String\\Controller', function () use ($app) { - return new TestController($app); - }); + $app->container->singleton('String\\Controller', fn() => new TestController($app)); $route = $this->app->addControllerRoute( '/', 'String\\Controller:index' @@ -388,7 +364,7 @@ public function testServiceControllersAreFetchedEvenIfTheirNameIsAnInvalidPHPCla // If the route could be dispatched, then the service was found $result = $route->dispatch(); - $this->assertTrue($result); + static::assertTrue($result); } } diff --git a/tests/SlimController/Tests/TestCase.php b/tests/SlimController/Tests/TestCase.php index 4eb2fef..8a7fc6e 100644 --- a/tests/SlimController/Tests/TestCase.php +++ b/tests/SlimController/Tests/TestCase.php @@ -7,7 +7,7 @@ use Slim\Http\Response; use SlimController\Slim; -class TestCase extends \PHPUnit_Framework_TestCase +class TestCase extends \PHPUnit\Framework\TestCase { /** From 0c93ed783601b5deb78f17db301de93f817e9d71 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 15:56:10 +0000 Subject: [PATCH 04/32] Replaced Slim\Slim with Slim\App --- src/SlimController/Slim.php | 2 +- src/SlimController/SlimController.php | 4 ++-- tests/SlimController/Tests/SlimControllerTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SlimController/Slim.php b/src/SlimController/Slim.php index b5567d2..b3f7ea3 100644 --- a/src/SlimController/Slim.php +++ b/src/SlimController/Slim.php @@ -17,7 +17,7 @@ /** * Extended Slim base */ -class Slim extends \Slim\Slim +class Slim extends \Slim\App { /** diff --git a/src/SlimController/SlimController.php b/src/SlimController/SlimController.php index 48649c3..8e1342a 100644 --- a/src/SlimController/SlimController.php +++ b/src/SlimController/SlimController.php @@ -67,9 +67,9 @@ abstract class SlimController /** * Constructor for TodoQueue\Controller\Login * - * @param \Slim\Slim $app Ref to slim app + * @param \Slim\App $app Ref to slim app */ - public function __construct(\Slim\Slim &$app) + public function __construct(\Slim\App &$app) { $this->app = $app; if ($renderTemplateSuffix = $app->config('controller.template_suffix')) { diff --git a/tests/SlimController/Tests/SlimControllerTest.php b/tests/SlimController/Tests/SlimControllerTest.php index 5ec0cc3..599d5c7 100644 --- a/tests/SlimController/Tests/SlimControllerTest.php +++ b/tests/SlimController/Tests/SlimControllerTest.php @@ -18,7 +18,7 @@ class SlimControllerTest extends \PHPUnit\Framework\TestCase protected function setUp(): void { - $this->slim = m::mock(\Slim\Slim::class); + $this->slim = m::mock(\Slim\App::class); parent::setUp(); } From cbeff652648582780e606d53710d981a7d00fba5 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 15:57:13 +0000 Subject: [PATCH 05/32] Updated Slim from 2 to 3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ea7af3b..1362248 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ], "require": { "php": ">=5.3.0", - "slim/slim": "2.*" + "slim/slim": "3.*" }, "require-dev": { "phpunit/phpunit": "^9", From 41836e6a752fb6a17bc5ac5c3b01b112cd91bca0 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 15:57:18 +0000 Subject: [PATCH 06/32] Migrated Slim\Environment to Slim\Http\Environment --- tests/SlimController/Tests/TestCase.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/SlimController/Tests/TestCase.php b/tests/SlimController/Tests/TestCase.php index 8a7fc6e..0941017 100644 --- a/tests/SlimController/Tests/TestCase.php +++ b/tests/SlimController/Tests/TestCase.php @@ -2,7 +2,7 @@ namespace SlimController\Tests; -use Slim\Environment; +use Slim\Http\Environment; use Slim\Http\Request; use Slim\Http\Response; use SlimController\Slim; @@ -33,7 +33,7 @@ class TestCase extends \PHPUnit\Framework\TestCase protected function setUrl($path, $params = '', $config = array()) { - Environment::mock(array( + $this->env = Environment::mock(array( 'REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', //<-- Physical @@ -46,8 +46,7 @@ protected function setUrl($path, $params = '', $config = array()) 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim' )); - $this->env = Environment::getInstance(); - $this->req = new Request($this->env); + $this->req = Request::createFromEnvironment($this->env); $this->res = new Response(); $this->app = new Slim(array_merge(array( 'controller.class_prefix' => '\\SlimController\\Tests\\Fixtures\\Controller', @@ -56,5 +55,6 @@ protected function setUrl($path, $params = '', $config = array()) 'controller.template_suffix' => 'php', 'templates.path' => __DIR__ . '/Fixtures/templates' ), $config)); + } } \ No newline at end of file From b6dda994bf45f1ba7478788d34720e71a45ab828 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 16:22:42 +0000 Subject: [PATCH 07/32] Migrated config() calls in SlimController --- src/SlimController/SlimController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SlimController/SlimController.php b/src/SlimController/SlimController.php index 8e1342a..e8cb03b 100644 --- a/src/SlimController/SlimController.php +++ b/src/SlimController/SlimController.php @@ -72,17 +72,17 @@ abstract class SlimController public function __construct(\Slim\App &$app) { $this->app = $app; - if ($renderTemplateSuffix = $app->config('controller.template_suffix')) { + if ($renderTemplateSuffix = $app->container()->get('settings')['controller.template_suffix']) { $this->renderTemplateSuffix = $renderTemplateSuffix; } - if (!is_null($paramPrefix = $app->config('controller.param_prefix'))) { + if (!is_null($paramPrefix = $app->container()->get('settings')['controller.param_prefix'])) { $this->paramPrefix = $paramPrefix; $prefixLength = strlen($this->paramPrefix); if ($prefixLength > 0 && substr($this->paramPrefix, -$prefixLength) !== '.') { $this->paramPrefix .= '.'; } } - if ($app->config('controller.cleanup_params')) { + if ($app->container()->get('settings')['controller.cleanup_params']) { $this->paramCleanup = true; } } From fa3f4401be00716600c39b827fe3bece95e6d436 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 16:30:17 +0000 Subject: [PATCH 08/32] Migrated: Move HttpMethod to Slim\App::map first parameter instead of via() --- CHANGELOG.md | 2 ++ src/SlimController/Slim.php | 29 +++++++++++++------------ tests/SlimController/Tests/SlimTest.php | 14 +++++++----- 3 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d4f7302 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +# unreleased + - BREAKING: \ No newline at end of file diff --git a/src/SlimController/Slim.php b/src/SlimController/Slim.php index b3f7ea3..0c62162 100644 --- a/src/SlimController/Slim.php +++ b/src/SlimController/Slim.php @@ -108,19 +108,20 @@ public function addRoutes(array $routes, $globalMiddlewares = array()) throw new \InvalidArgumentException("Http method '$httpMethod' is not supported."); } + if ('any' === $httpMethod) { + $httpMethod = static::$ALLOWED_HTTP_METHODS; + } else { + $httpMethod = [ $httpMethod ]; + } + $routeMiddlewares = array_merge($localMiddlewares, $globalMiddlewares); - $route = $this->addControllerRoute($path, $classRoute, $routeMiddlewares); + $route = $this->addControllerRoute($httpMethod, $path, $classRoute, $routeMiddlewares); if (!isset($this->routeNames[$classRoute])) { $route->name($classRoute); $this->routeNames[$classRoute] = 1; } - if ('any' === $httpMethod) { - call_user_func_array(array($route, 'via'), static::$ALLOWED_HTTP_METHODS); - } else { - $route->via($httpMethod); - } } } @@ -131,11 +132,11 @@ public function addRoutes(array $routes, $globalMiddlewares = array()) * Add a new controller route * * - * $app->addControllerRoute("/the/path", "className:methodName", array(function () { doSome(); })) - * ->via('GET')->condition(..); + * $app->addControllerRoute(['GET'], "/the/path", "className:methodName", array(function () { doSome(); })) + * ->condition(..); * - * $app->addControllerRoute("/the/path", "className:methodName") - * ->via('GET')->condition(..); + * $app->addControllerRoute(['GET'], "/the/path", "className:methodName") + * ->condition(..); * * * @param string $path @@ -144,7 +145,7 @@ public function addRoutes(array $routes, $globalMiddlewares = array()) * * @return \Slim\Route */ - public function addControllerRoute($path, $route, array $middleware = array()) + public function addControllerRoute($methods, $path, $route, array $middleware = array()) { $callback = $this->buildCallbackFromControllerRoute($route); @@ -189,14 +190,14 @@ protected function determineClassAndMethod($classMethod) { // determine class prefix (eg "\Vendor\Bundle\Controller") and suffix (eg "Controller") - $classNamePrefix = $this->config('controller.class_prefix'); + $classNamePrefix = $this->getContainer()->get('settings')['controller.class_prefix']; if ($classNamePrefix && substr($classNamePrefix, -strlen($classNamePrefix) !== 0)) { $classNamePrefix .= '\\'; } - $classNameSuffix = $this->config('controller.class_suffix') ? : ''; + $classNameSuffix = $this->getContainer()->get('settings')['controller.class_suffix'] ? : ''; // determine method suffix or default to "Action" - $methodNameSuffix = $this->config('controller.method_suffix'); + $methodNameSuffix = $this->getContainer()->get('settings')['controller.method_suffix']; if (is_null($methodNameSuffix)) { $methodNameSuffix = 'Action'; } diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 79743d0..cea931a 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -253,8 +253,9 @@ public function testAddControllerRoute() { $this->setUrl('/'); $this->app->addControllerRoute( + 'GET', '/', 'Controller:index' - )->via('GET'); + ); static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } @@ -263,10 +264,11 @@ public function testAddControllerRouteWithMiddleware() { $this->setUrl('/'); $this->app->addControllerRoute( + 'GET', '/', 'Controller:index', array( fn() => false, ) - )->via('GET'); + ); /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); @@ -318,8 +320,8 @@ public function testServiceControllersAreFetched() $app->container->singleton('TestController', fn() => new TestController($app)); $route = $this->app->addControllerRoute( - '/', 'TestController:index' - )->via('GET'); + 'GET', '/', 'TestController:index' + ); // If the route could be dispatched, then the service was found $result = $route->dispatch(); @@ -359,8 +361,8 @@ public function testServiceControllersAreFetchedEvenIfTheirNameIsAnInvalidPHPCla $app->container->singleton('String\\Controller', fn() => new TestController($app)); $route = $this->app->addControllerRoute( - '/', 'String\\Controller:index' - )->via('GET'); + 'GET', '/', 'String\\Controller:index' + ); // If the route could be dispatched, then the service was found $result = $route->dispatch(); From ca0cc29d0067447511947f5e42bd65fd7ea9d93c Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Wed, 26 Jan 2022 16:43:46 +0000 Subject: [PATCH 09/32] Migrated $app->container to $app->getContainer --- tests/SlimController/Tests/SlimTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index cea931a..b14c2a7 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -317,7 +317,7 @@ public function testServiceControllersAreFetched() ); $this->setUrl('/', '', $config); $app = $this->app; - $app->container->singleton('TestController', fn() => new TestController($app)); + $app->getContainer()['TestController'] = fn() => new TestController($app); $route = $this->app->addControllerRoute( 'GET', '/', 'TestController:index' @@ -338,7 +338,7 @@ public function testServiceControllersAreFetchedWithParams() ); $this->setUrl('/', '', $config); $app = $this->app; - $app->container->singleton('TestController', fn() => new TestController($app)); + $app->getContainer()['TestController'] = fn() => new TestController($app); $app->addRoutes(array( '/another/:name' => 'TestController:hello' @@ -358,7 +358,7 @@ public function testServiceControllersAreFetchedEvenIfTheirNameIsAnInvalidPHPCla ); $this->setUrl('/', '', $config); $app = $this->app; - $app->container->singleton('String\\Controller', fn() => new TestController($app)); + $app->getContainer()['String\\Controller'] = fn() => new TestController($app); $route = $this->app->addControllerRoute( 'GET', '/', 'String\\Controller:index' From 3615e69161c19343606128448e0d303af8e57440 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 10:13:13 +0000 Subject: [PATCH 10/32] Update addControllerRoute to pass the method --- src/SlimController/Slim.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SlimController/Slim.php b/src/SlimController/Slim.php index 0c62162..250c6b9 100644 --- a/src/SlimController/Slim.php +++ b/src/SlimController/Slim.php @@ -149,7 +149,9 @@ public function addControllerRoute($methods, $path, $route, array $middleware = { $callback = $this->buildCallbackFromControllerRoute($route); + $methods = is_array($methods) ? $methods : [ $methods ]; array_unshift($middleware, $path); + array_unshift($middleware, $methods); array_push($middleware, $callback); $route = call_user_func_array(array($this, 'map'), $middleware); From 56770967a61fc42c5d204daccac0bbed5037820d Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 10:34:28 +0000 Subject: [PATCH 11/32] Removed old tests --- phpunit.xml.dist | 1 - .../Tests/Old/ControllerTest.php | 53 ------- tests/SlimController/Tests/Old/ParamsTest.php | 133 ------------------ tests/SlimController/Tests/Old/RenderTest.php | 22 --- .../SlimController/Tests/Old/RoutingTest.php | 45 ------ 5 files changed, 254 deletions(-) delete mode 100644 tests/SlimController/Tests/Old/ControllerTest.php delete mode 100644 tests/SlimController/Tests/Old/ParamsTest.php delete mode 100644 tests/SlimController/Tests/Old/RenderTest.php delete mode 100644 tests/SlimController/Tests/Old/RoutingTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ac6d7d2..5a36130 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,7 +12,6 @@ ./tests/SlimController/Tests - ./tests/SlimController/Tests/Old/ \ No newline at end of file diff --git a/tests/SlimController/Tests/Old/ControllerTest.php b/tests/SlimController/Tests/Old/ControllerTest.php deleted file mode 100644 index 8337dfe..0000000 --- a/tests/SlimController/Tests/Old/ControllerTest.php +++ /dev/null @@ -1,53 +0,0 @@ -expectOutputString('What is up?'); - $this->setUrl('/'); - $this->app->addRoutes(array( - '/' => 'Test:index', - )); - - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - /** - * @doesNotPerformAssertions - */ - public function testControllerExtended() - { - $this->expectOutputString('What is up YOU?'); - $this->setUrl('/hello/YOU'); - $this->app->addRoutes(array( - '/hello/:name' => 'Test:hello', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - /** - * @doesNotPerformAssertions - */ - public function testControllerAbsPath() - { - $this->expectOutputString('What is up YOU?'); - $this->setUrl('/hello/YOU'); - $this->app->addRoutes(array( - '/hello/:name' => 'Test:hello', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } -} diff --git a/tests/SlimController/Tests/Old/ParamsTest.php b/tests/SlimController/Tests/Old/ParamsTest.php deleted file mode 100644 index 37192f6..0000000 --- a/tests/SlimController/Tests/Old/ParamsTest.php +++ /dev/null @@ -1,133 +0,0 @@ -expectOutputString('Param is 123'); - $this->setUrl('/', 'data[Some][param]=123'); - $this->app->addRoutes(array( - '/' => 'Test:paramSingle', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - - /** - * @doesNotPerformAssertions - */ - public function testParamsSingleObject() - { - $this->expectOutputString('Param is 123123123'); - $this->setUrl('/', 'data[Some][attrib1]=123&data[Some][attrib2]=123&data[Some][attrib3]=123'); - $this->app->addRoutes(array( - '/' => 'Test:paramSingleObject', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - /** - * @doesNotPerformAssertions - */ - public function testParamsMulti() - { - $this->expectOutputString('All is foo bar'); - $this->setUrl('/', 'data[Some][param]=foo&data[Other][param]=bar'); - $this->app->addRoutes(array( - '/' => 'Test:paramMulti', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - /** - * @doesNotPerformAssertions - */ - public function testParamsMultiMissing() - { - $this->expectOutputString('All is foo bar'); - $this->setUrl('/', 'data[Some][param]=foo&data[Other][param]=bar'); - $this->app->addRoutes(array( - '/' => 'Test:paramMultiMissing', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - /** - * @doesNotPerformAssertions - */ - public function testParamsMultiMissingReq() - { - $this->expectOutputString('OK'); - $this->setUrl('/', 'data[Some][param]=foo&data[Other][param]=bar'); - $this->app->addRoutes(array( - '/' => 'Test:paramMultiMissingReq', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - /** - * @doesNotPerformAssertions - */ - public function testParamsMultiDefault() - { - $this->expectOutputString('All is foo bar and great'); - $this->setUrl('/', 'data[Some][param]=foo&data[Other][param]=bar'); - $this->app->addRoutes(array( - '/' => 'Test:paramMultiDefault', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - /** - * @doesNotPerformAssertions - */ - public function testParamsDifferentPrefix() - { - $this->expectOutputString('GOT OK'); - $this->setUrl('/', 'data[Foo]=bar&other[Foo]=bar', array( - 'controller.param_prefix' => 'other.' - )); - $this->app->addRoutes(array( - '/' => 'Test:paramDifferentPrefix', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } - - /** - * @doesNotPerformAssertions - */ - public function testParamsNoPrefix() - { - $this->expectOutputString('All params: data.Foo=bar - other.Foo=bar'); - $this->setUrl('/', 'data[Foo]=bar&other[Foo]=bar', array( - 'controller.param_prefix' => '' - )); - $this->app->addRoutes(array( - '/' => 'Test:paramNoPrefix', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } -} diff --git a/tests/SlimController/Tests/Old/RenderTest.php b/tests/SlimController/Tests/Old/RenderTest.php deleted file mode 100644 index c471858..0000000 --- a/tests/SlimController/Tests/Old/RenderTest.php +++ /dev/null @@ -1,22 +0,0 @@ -expectOutputString('This is orotound and grandios'); - $this->setUrl('/', 'data[Some][param]=foo&data[Other][param]=bar'); - $this->app->addRoutes(array( - '/' => 'Test:render', - )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - //$this->app->router()->dispatch($route); - $route->dispatch(); - } -} diff --git a/tests/SlimController/Tests/Old/RoutingTest.php b/tests/SlimController/Tests/Old/RoutingTest.php deleted file mode 100644 index daf6b9b..0000000 --- a/tests/SlimController/Tests/Old/RoutingTest.php +++ /dev/null @@ -1,45 +0,0 @@ -setUrl('/'); - $this->app->addRoutes(array( - '/' => 'Controller:index', - )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); - - $this->setUrl('/foo'); - static::assertEquals(0, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); - - $this->setUrl('/other'); - - $this->app->addRoutes(array( - '/other' => 'Controller:other', - )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); - } - - public function testRoutesWithVariables() - { - $this->setUrl('/hello/you'); - $this->app->addRoutes(array( - '/hello/:name' => 'Controller:index', - )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); - } - - public function testRoutesWithExtendedFormat() - { - $this->setUrl('/bla'); - $this->app->addRoutes(array( - '/bla' => array('Controller:index', 'get') - )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); - } - -} From d9c066204ea40816f219b662336c3d1fa348713a Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 10:35:50 +0000 Subject: [PATCH 12/32] Replaced app->router() with app->container->get('router') --- tests/SlimController/Tests/SlimTest.php | 40 ++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index b14c2a7..b77a9f9 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -20,7 +20,7 @@ public function testAddingRoutesToSameMethod() '/alb' => array('get' => 'Controller:index') )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); // $this->assertTrue($this->app->router->hasNamedRoute('Controller:index')); static::assertEquals('/bla', $this->app->urlFor('Controller:index')); } @@ -32,7 +32,7 @@ public function testAddingroutesWithOldSyntaxWithoutMiddlewares() '/bla' => array('Controller:index'), )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddRoutesWithOldSyntaxWithoutMiddlewareArray() @@ -43,7 +43,7 @@ public function testAddRoutesWithOldSyntaxWithoutMiddlewareArray() // }) )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddRoutesWithOldSyntaxWithMiddlewareArray() @@ -54,7 +54,7 @@ public function testAddRoutesWithOldSyntaxWithMiddlewareArray() // })) )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddSimpleRoutes() @@ -63,17 +63,17 @@ public function testAddSimpleRoutes() $this->app->addRoutes(array( '/' => 'Controller:index', )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); $this->setUrl('/foo'); - static::assertEquals(0, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(0, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); $this->setUrl('/other'); $this->app->addRoutes(array( '/other' => 'Controller:other', )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddRoutesWithVariables() @@ -82,7 +82,7 @@ public function testAddRoutesWithVariables() $this->app->addRoutes(array( '/hello/:name' => 'Controller:index', )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddRoutesInExtendedFormat() @@ -91,7 +91,7 @@ public function testAddRoutesInExtendedFormat() $this->app->addRoutes(array( '/bla' => array('get' => 'Controller:index') )); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testFailToAddInvalidClassMethodFormat() @@ -112,7 +112,7 @@ public function testGlobalMiddlewareIsAddedToRoute() ), fn() => false); /** @var \Slim\Route[] $routes */ - $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); @@ -133,7 +133,7 @@ public function testGlobalMiddlewareIsAddedToRouteAsArray() ), $middlewares); /** @var \Slim\Route[] $routes */ - $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); @@ -149,7 +149,7 @@ public function testLocalMiddlewareIsAddedToRoute() )); /** @var \Slim\Route[] $routes */ - $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); @@ -170,7 +170,7 @@ public function testArrayOfLocalMiddlewareIsAddedToRoute() )); /** @var \Slim\Route[] $routes */ - $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); @@ -191,7 +191,7 @@ public function testLocalMiddlewaresAreAddedToRoute() )); /** @var \Slim\Route[] $routes */ - $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); @@ -207,7 +207,7 @@ public function testGlobalAndLocalMiddlewareIsAddedToRoute() ), array(fn() => false, fn() => false)); /** @var \Slim\Route[] $routes */ - $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); @@ -232,7 +232,7 @@ public function testRouteCallbacksAreFiredOnDispatch() $this->app->addRoutes(array( '/bla' => 'Test:index' )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); $route->dispatch(); } @@ -245,7 +245,7 @@ public function testEmptyButNotNullMethodSuffixAccepted() $this->app->addRoutes(array( '/bla' => 'Test:notSuffixedMethod' )); - [$route] = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); $route->dispatch(); } @@ -257,7 +257,7 @@ public function testAddControllerRoute() '/', 'Controller:index' ); - static::assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } public function testAddControllerRouteWithMiddleware() @@ -271,7 +271,7 @@ public function testAddControllerRouteWithMiddleware() ); /** @var \Slim\Route[] $routes */ - $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); static::assertEquals(1, count($routes)); $middleware = $routes[0]->getMiddleware(); @@ -343,7 +343,7 @@ public function testServiceControllersAreFetchedWithParams() $app->addRoutes(array( '/another/:name' => 'TestController:hello' )); - $route = $app->router()->getNamedRoute('TestController:hello'); + $route = $app->container->get('router')->getNamedRoute('TestController:hello'); $route->setParams(array('name' => 'foo')); static::assertTrue($route->dispatch()); } From 3429f9e3b38de30174e6c10e353d98f2015fc417 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 10:47:50 +0000 Subject: [PATCH 13/32] Fixed capitalisation of httpMethod and updated $route->name() to setName() --- src/SlimController/Slim.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SlimController/Slim.php b/src/SlimController/Slim.php index 250c6b9..344315d 100644 --- a/src/SlimController/Slim.php +++ b/src/SlimController/Slim.php @@ -108,7 +108,7 @@ public function addRoutes(array $routes, $globalMiddlewares = array()) throw new \InvalidArgumentException("Http method '$httpMethod' is not supported."); } - if ('any' === $httpMethod) { + if ('ANY' === $httpMethod) { $httpMethod = static::$ALLOWED_HTTP_METHODS; } else { $httpMethod = [ $httpMethod ]; @@ -118,7 +118,7 @@ public function addRoutes(array $routes, $globalMiddlewares = array()) $route = $this->addControllerRoute($httpMethod, $path, $classRoute, $routeMiddlewares); if (!isset($this->routeNames[$classRoute])) { - $route->name($classRoute); + $route->setName($classRoute); $this->routeNames[$classRoute] = 1; } From bf1768156fab430c8b26405b2b9838d534d7af06 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 10:52:33 +0000 Subject: [PATCH 14/32] Updated Environment:mock and removed defaults --- tests/SlimController/Tests/TestCase.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/SlimController/Tests/TestCase.php b/tests/SlimController/Tests/TestCase.php index 0941017..f0653f0 100644 --- a/tests/SlimController/Tests/TestCase.php +++ b/tests/SlimController/Tests/TestCase.php @@ -34,15 +34,8 @@ class TestCase extends \PHPUnit\Framework\TestCase protected function setUrl($path, $params = '', $config = array()) { $this->env = Environment::mock(array( - 'REQUEST_METHOD' => 'GET', - 'REMOTE_ADDR' => '127.0.0.1', - 'SCRIPT_NAME' => '', //<-- Physical - 'PATH_INFO' => $path, //<-- Virtual + 'REQUEST_URI' => $path, 'QUERY_STRING' => $params, - 'SERVER_NAME' => 'slim', - 'SERVER_PORT' => 80, - 'slim.url_scheme' => 'http', - 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim' )); From bb49f89c4477ed0a8237f4aeb9d96cfb179e68f1 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 11:08:41 +0000 Subject: [PATCH 15/32] Updated testAddSimpleRoutes --- CHANGELOG.md | 2 +- tests/SlimController/Tests/SlimTest.php | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4f7302..9d330c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,2 @@ # unreleased - - BREAKING: \ No newline at end of file + - BREAKING: You can no longer add routes after the app has been dispatched \ No newline at end of file diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index b77a9f9..8ef3424 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -5,7 +5,6 @@ namespace SlimController\Tests; - use SlimController\Tests\Fixtures\Controller\TestController; @@ -63,17 +62,24 @@ public function testAddSimpleRoutes() $this->app->addRoutes(array( '/' => 'Controller:index', )); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + /** @var \Slim\Router $router */ + $router = $this->app->container->get('router'); + + static::assertEquals(\FastRoute\Dispatcher::FOUND, $router->dispatch($this->req)[0]); $this->setUrl('/foo'); - static::assertEquals(0, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals(\FastRoute\Dispatcher::NOT_FOUND, $router->dispatch($this->req)[0]); $this->setUrl('/other'); + // Adding a route after we've dispatched no longer works. I suspect this is a change in + // Slim v3. I've not found the cause or a workaround, but also can't see a real world + // use-case so I won't be spending any more time trying to fix it. $this->app->addRoutes(array( '/other' => 'Controller:other', )); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + + static::assertEquals(\FastRoute\Dispatcher::NOT_FOUND, $router->dispatch($this->req)[0]); } public function testAddRoutesWithVariables() From 75cce472ab58f24a24959266795ed06513edd726 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 11:15:02 +0000 Subject: [PATCH 16/32] Migrated testAddingRoutesToSameMethod --- tests/SlimController/Tests/SlimTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 8ef3424..47a19b4 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -19,9 +19,12 @@ public function testAddingRoutesToSameMethod() '/alb' => array('get' => 'Controller:index') )); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals( + \FastRoute\Dispatcher::FOUND, + $this->app->container->get('router')->dispatch($this->req)[0] + ); // $this->assertTrue($this->app->router->hasNamedRoute('Controller:index')); - static::assertEquals('/bla', $this->app->urlFor('Controller:index')); + static::assertEquals('/bla', $this->app->container->get('router')->pathFor('Controller:index')); } public function testAddingroutesWithOldSyntaxWithoutMiddlewares() From b2f16b2b164859d057ce77a7d86c735a95be5433 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 11:15:25 +0000 Subject: [PATCH 17/32] Migrated testAddingroutesWithOldSyntaxWithoutMiddlewares --- tests/SlimController/Tests/SlimTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 47a19b4..4bdf1ab 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -34,7 +34,10 @@ public function testAddingroutesWithOldSyntaxWithoutMiddlewares() '/bla' => array('Controller:index'), )); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals( + \FastRoute\Dispatcher::FOUND, + $this->app->container->get('router')->dispatch($this->req)[0] + ); } public function testAddRoutesWithOldSyntaxWithoutMiddlewareArray() From d9e7be358c37a4fdcd880b2542c94c626bdc44b2 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 11:15:42 +0000 Subject: [PATCH 18/32] Migrated testAddRoutesWithOldSyntaxWithoutMiddlewareArray --- tests/SlimController/Tests/SlimTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 4bdf1ab..61fb471 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -48,7 +48,10 @@ public function testAddRoutesWithOldSyntaxWithoutMiddlewareArray() // }) )); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals( + \FastRoute\Dispatcher::FOUND, + $this->app->container->get('router')->dispatch($this->req)[0] + ); } public function testAddRoutesWithOldSyntaxWithMiddlewareArray() From 43bb50b48f2b5d57ddadbb7ca29a7639d6ad528f Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 11:16:07 +0000 Subject: [PATCH 19/32] Migrated testAddRoutesWithOldSyntaxWithMiddlewareArray --- tests/SlimController/Tests/SlimTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 61fb471..fb48ab4 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -62,7 +62,10 @@ public function testAddRoutesWithOldSyntaxWithMiddlewareArray() // })) )); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals( + \FastRoute\Dispatcher::FOUND, + $this->app->container->get('router')->dispatch($this->req)[0] + ); } public function testAddSimpleRoutes() From ffd4c59f9668ed5d6d6fff0db191a18e83699395 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 11:18:16 +0000 Subject: [PATCH 20/32] Migrated testAddRoutesWithVariables --- tests/SlimController/Tests/SlimTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index fb48ab4..7b155ce 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -98,9 +98,12 @@ public function testAddRoutesWithVariables() { $this->setUrl('/hello/you'); $this->app->addRoutes(array( - '/hello/:name' => 'Controller:index', + '/hello/{name}' => 'Controller:index', )); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals( + \FastRoute\Dispatcher::FOUND, + $this->app->container->get('router')->dispatch($this->req)[0] + ); } public function testAddRoutesInExtendedFormat() From 03b0509c8691059b2ecf330d3cbeabd4a59a3cbe Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 11:18:34 +0000 Subject: [PATCH 21/32] Migrted testAddRoutesInExtendedFormat --- tests/SlimController/Tests/SlimTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 7b155ce..7b43aa6 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -112,7 +112,10 @@ public function testAddRoutesInExtendedFormat() $this->app->addRoutes(array( '/bla' => array('get' => 'Controller:index') )); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + static::assertEquals( + \FastRoute\Dispatcher::FOUND, + $this->app->container->get('router')->dispatch($this->req)[0] + ); } public function testFailToAddInvalidClassMethodFormat() From 7aa4daad0fa685aba7ecf20f9cec5723808a8ade Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 11:53:22 +0000 Subject: [PATCH 22/32] Corrected $app->container to $app->getContainer() --- tests/SlimController/Tests/SlimTest.php | 53 ++++++++++--------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 7b43aa6..b7068fb 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -21,10 +21,10 @@ public function testAddingRoutesToSameMethod() static::assertEquals( \FastRoute\Dispatcher::FOUND, - $this->app->container->get('router')->dispatch($this->req)[0] + $this->app->getContainer()->get('router')->dispatch($this->req)[0] ); // $this->assertTrue($this->app->router->hasNamedRoute('Controller:index')); - static::assertEquals('/bla', $this->app->container->get('router')->pathFor('Controller:index')); + static::assertEquals('/bla', $this->app->getContainer()->get('router')->pathFor('Controller:index')); } public function testAddingroutesWithOldSyntaxWithoutMiddlewares() @@ -36,7 +36,7 @@ public function testAddingroutesWithOldSyntaxWithoutMiddlewares() static::assertEquals( \FastRoute\Dispatcher::FOUND, - $this->app->container->get('router')->dispatch($this->req)[0] + $this->app->getContainer()->get('router')->dispatch($this->req)[0] ); } @@ -50,7 +50,7 @@ public function testAddRoutesWithOldSyntaxWithoutMiddlewareArray() )); static::assertEquals( \FastRoute\Dispatcher::FOUND, - $this->app->container->get('router')->dispatch($this->req)[0] + $this->app->getContainer()->get('router')->dispatch($this->req)[0] ); } @@ -64,7 +64,7 @@ public function testAddRoutesWithOldSyntaxWithMiddlewareArray() )); static::assertEquals( \FastRoute\Dispatcher::FOUND, - $this->app->container->get('router')->dispatch($this->req)[0] + $this->app->getContainer()->get('router')->dispatch($this->req)[0] ); } @@ -75,7 +75,7 @@ public function testAddSimpleRoutes() '/' => 'Controller:index', )); /** @var \Slim\Router $router */ - $router = $this->app->container->get('router'); + $router = $this->app->getContainer()->get('router'); static::assertEquals(\FastRoute\Dispatcher::FOUND, $router->dispatch($this->req)[0]); @@ -102,7 +102,7 @@ public function testAddRoutesWithVariables() )); static::assertEquals( \FastRoute\Dispatcher::FOUND, - $this->app->container->get('router')->dispatch($this->req)[0] + $this->app->getContainer()->get('router')->dispatch($this->req)[0] ); } @@ -114,7 +114,7 @@ public function testAddRoutesInExtendedFormat() )); static::assertEquals( \FastRoute\Dispatcher::FOUND, - $this->app->container->get('router')->dispatch($this->req)[0] + $this->app->getContainer()->get('router')->dispatch($this->req)[0] ); } @@ -135,9 +135,7 @@ public function testGlobalMiddlewareIsAddedToRoute() '/bla' => 'Controller:index' ), fn() => false); - /** @var \Slim\Route[] $routes */ - $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - static::assertEquals(1, count($routes)); + $router = $this->app->getContainer()->get('router'); $middleware = $routes[0]->getMiddleware(); static::assertIsArray($middleware); @@ -157,8 +155,7 @@ public function testGlobalMiddlewareIsAddedToRouteAsArray() ), $middlewares); /** @var \Slim\Route[] $routes */ - $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - static::assertEquals(1, count($routes)); + $routes = $this->app->getContainer()->get('router')->dispatch($this->req); $middleware = $routes[0]->getMiddleware(); static::assertIsArray($middleware); @@ -173,8 +170,7 @@ public function testLocalMiddlewareIsAddedToRoute() )); /** @var \Slim\Route[] $routes */ - $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - static::assertEquals(1, count($routes)); + $routes = $this->app->getContainer()->get('router')->dispatch($this->req); $middleware = $routes[0]->getMiddleware(); static::assertIsArray($middleware); @@ -194,8 +190,7 @@ public function testArrayOfLocalMiddlewareIsAddedToRoute() )); /** @var \Slim\Route[] $routes */ - $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - static::assertEquals(1, count($routes)); + $routes = $this->app->getContainer()->get('router')->dispatch($this->req); $middleware = $routes[0]->getMiddleware(); static::assertIsArray($middleware); @@ -215,8 +210,7 @@ public function testLocalMiddlewaresAreAddedToRoute() )); /** @var \Slim\Route[] $routes */ - $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - static::assertEquals(1, count($routes)); + $routes = $this->app->getContainer()->get('router')->dispatch($this->req); $middleware = $routes[0]->getMiddleware(); static::assertIsArray($middleware); @@ -231,8 +225,7 @@ public function testGlobalAndLocalMiddlewareIsAddedToRoute() ), array(fn() => false, fn() => false)); /** @var \Slim\Route[] $routes */ - $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - static::assertEquals(1, count($routes)); + $routes = $this->app->getContainer()->get('router')->dispatch($this->req); $middleware = $routes[0]->getMiddleware(); static::assertIsArray($middleware); @@ -256,8 +249,7 @@ public function testRouteCallbacksAreFiredOnDispatch() $this->app->addRoutes(array( '/bla' => 'Test:index' )); - [$route] = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - $route->dispatch(); + $route = $this->app->getContainer()->get('router')->dispatch($this->req); } public function testEmptyButNotNullMethodSuffixAccepted() @@ -269,7 +261,7 @@ public function testEmptyButNotNullMethodSuffixAccepted() $this->app->addRoutes(array( '/bla' => 'Test:notSuffixedMethod' )); - [$route] = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + [$route] = $this->app->getContainer()->get('router')->dispatch($this->req); $route->dispatch(); } @@ -281,7 +273,7 @@ public function testAddControllerRoute() '/', 'Controller:index' ); - static::assertEquals(1, count($this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); + $this->app->getContainer()->get('router')->dispatch($this->req)[0] } public function testAddControllerRouteWithMiddleware() @@ -295,8 +287,7 @@ public function testAddControllerRouteWithMiddleware() ); /** @var \Slim\Route[] $routes */ - $routes = $this->app->container->get('router')->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); - static::assertEquals(1, count($routes)); + $routes = $this->app->getContainer()->get('router')->dispatch($this->req); $middleware = $routes[0]->getMiddleware(); static::assertIsArray($middleware); @@ -312,9 +303,9 @@ public function testNamedRoutes() '/something/:id' => 'Something:show' )); - static::assertEquals('/', $this->app->urlFor('Controller:index')); - static::assertEquals('/bla', $this->app->urlFor('Bla:Index')); - static::assertEquals('/something/:id', $this->app->urlFor('Something:show')); + static::assertEquals('/', $this->app->getContainer()->get('router')->pathFor('Controller:index')); + static::assertEquals('/bla', $this->app->getContainer()->get('router')->pathFor('Bla:Index')); + static::assertEquals('/something/{id}', $this->app->getContainer()->get('router')->pathFor('Something:show')); } public function testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotExist() @@ -328,7 +319,7 @@ public function testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotExist '/something/:id' => 'Something:show' )); - static::assertEquals('/', $this->app->urlFor('this is not a named route')); + static::assertEquals('/', $this->app->getContainer()->get('router')->pathFor('this is not a named route')); } public function testServiceControllersAreFetched() From 6adc471a79a7d8132410f83f89661e9397931829 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:07:40 +0000 Subject: [PATCH 23/32] Migrated testNamedRoutes --- tests/SlimController/Tests/SlimTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index b7068fb..c8fd2ae 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -300,12 +300,13 @@ public function testNamedRoutes() $this->app->addRoutes(array( '/' => 'Controller:index', '/bla' => 'Bla:Index', - '/something/:id' => 'Something:show' + '/something[/{id}]' => 'Something:show' )); static::assertEquals('/', $this->app->getContainer()->get('router')->pathFor('Controller:index')); static::assertEquals('/bla', $this->app->getContainer()->get('router')->pathFor('Bla:Index')); - static::assertEquals('/something/{id}', $this->app->getContainer()->get('router')->pathFor('Something:show')); + static::assertEquals('/something', $this->app->getContainer()->get('router')->pathFor('Something:show')); + static::assertEquals('/something/10', $this->app->getContainer()->get('router')->pathFor('Something:show', [ 'id' => 10])); } public function testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotExist() From 94c6d355b9b0bdd4af2ae1875941a1d52f4aa3f3 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:08:03 +0000 Subject: [PATCH 24/32] Migrated testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotExist --- tests/SlimController/Tests/SlimTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index c8fd2ae..debe91a 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -312,12 +312,12 @@ public function testNamedRoutes() public function testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotExist() { $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Named route not found for name: this is not a named route'); + $this->expectExceptionMessage('Named route does not exist for name: this is not a named route'); $this->setUrl('/'); $this->app->addRoutes(array( '/' => 'Controller:index', '/bla' => 'Bla:Index', - '/something/:id' => 'Something:show' + '/something/{id}' => 'Something:show' )); static::assertEquals('/', $this->app->getContainer()->get('router')->pathFor('this is not a named route')); From 5f49a50aca2c407d18f6f0e62ff7c39b2943d191 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:13:15 +0000 Subject: [PATCH 25/32] Migrated container to getContainer in SlimController\Slim --- src/SlimController/Slim.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SlimController/Slim.php b/src/SlimController/Slim.php index 344315d..c90f0e5 100644 --- a/src/SlimController/Slim.php +++ b/src/SlimController/Slim.php @@ -174,7 +174,7 @@ protected function buildCallbackFromControllerRoute($route) // Get action arguments $args = func_get_args(); // Try to fetch the instance from Slim's container, otherwise lazy-instantiate it - $instance = $app->container->has($controller) ? $app->container->get($controller) : new $controller($app); + $instance = $app->getContainer()->has($controller) ? $app->getContainer()->get($controller) : new $controller($app); return call_user_func_array(array($instance, $methodName), $args); }; From 226de05b9380aac8783acad4bba1664c4ea15b65 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:16:53 +0000 Subject: [PATCH 26/32] Migrated Settings into it's sub-key in the App constructor --- tests/SlimController/Tests/TestCase.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/SlimController/Tests/TestCase.php b/tests/SlimController/Tests/TestCase.php index f0653f0..5aeb888 100644 --- a/tests/SlimController/Tests/TestCase.php +++ b/tests/SlimController/Tests/TestCase.php @@ -31,7 +31,7 @@ class TestCase extends \PHPUnit\Framework\TestCase protected $app; - protected function setUrl($path, $params = '', $config = array()) + protected function setUrl($path, $params = '', $settings = array()) { $this->env = Environment::mock(array( 'REQUEST_URI' => $path, @@ -41,13 +41,15 @@ protected function setUrl($path, $params = '', $config = array()) )); $this->req = Request::createFromEnvironment($this->env); $this->res = new Response(); - $this->app = new Slim(array_merge(array( - 'controller.class_prefix' => '\\SlimController\\Tests\\Fixtures\\Controller', - 'controller.class_suffix' => 'Controller', - 'controller.method_suffix' => 'Action', - 'controller.template_suffix' => 'php', - 'templates.path' => __DIR__ . '/Fixtures/templates' - ), $config)); - + $this->app = new Slim(array( + 'settings' => array_merge(array( + 'controller.class_prefix' => '\\SlimController\\Tests\\Fixtures\\Controller', + 'controller.class_suffix' => 'Controller', + 'controller.method_suffix' => 'Action', + 'controller.template_suffix' => 'php', + 'templates.path' => __DIR__ . '/Fixtures/templates' + ), $settings) + ) + ); } } \ No newline at end of file From 1d5bc36d40e9b3eee99b8945bead44ff93af0d3d Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:19:58 +0000 Subject: [PATCH 27/32] Fixed container() to getContainer() --- src/SlimController/SlimController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SlimController/SlimController.php b/src/SlimController/SlimController.php index e8cb03b..56707a9 100644 --- a/src/SlimController/SlimController.php +++ b/src/SlimController/SlimController.php @@ -72,17 +72,17 @@ abstract class SlimController public function __construct(\Slim\App &$app) { $this->app = $app; - if ($renderTemplateSuffix = $app->container()->get('settings')['controller.template_suffix']) { + if ($renderTemplateSuffix = $app->getContainer()->get('settings')['controller.template_suffix']) { $this->renderTemplateSuffix = $renderTemplateSuffix; } - if (!is_null($paramPrefix = $app->container()->get('settings')['controller.param_prefix'])) { + if (!is_null($paramPrefix = $app->getContainer()->get('settings')['controller.param_prefix'])) { $this->paramPrefix = $paramPrefix; $prefixLength = strlen($this->paramPrefix); if ($prefixLength > 0 && substr($this->paramPrefix, -$prefixLength) !== '.') { $this->paramPrefix .= '.'; } } - if ($app->container()->get('settings')['controller.cleanup_params']) { + if ($app->getContainer()->get('settings')['controller.cleanup_params']) { $this->paramCleanup = true; } } From 83ac302a2ddb91caa910cb2039d8a2a7c3f18543 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:34:40 +0000 Subject: [PATCH 28/32] Migrated testRouteCallbacksAreFiredOnDispatch --- tests/SlimController/Tests/SlimTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index debe91a..47e2bd7 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -249,7 +249,7 @@ public function testRouteCallbacksAreFiredOnDispatch() $this->app->addRoutes(array( '/bla' => 'Test:index' )); - $route = $this->app->getContainer()->get('router')->dispatch($this->req); + ($this->app)($this->req, $this->res); } public function testEmptyButNotNullMethodSuffixAccepted() From a6f2bc09f9c106699c3af3d07ea74e307b5064b6 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:35:24 +0000 Subject: [PATCH 29/32] Migrated testEmptyButNotNullMethodSuffixAccepted --- tests/SlimController/Tests/SlimTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 47e2bd7..6bec553 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -261,8 +261,7 @@ public function testEmptyButNotNullMethodSuffixAccepted() $this->app->addRoutes(array( '/bla' => 'Test:notSuffixedMethod' )); - [$route] = $this->app->getContainer()->get('router')->dispatch($this->req); - $route->dispatch(); + ($this->app)($this->req, $this->res); } public function testAddControllerRoute() From bf3a5de33307d9c732eb095e7c9d4582f4e1c92e Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:36:10 +0000 Subject: [PATCH 30/32] Migrated testAddControllerRouteSimple --- tests/SlimController/Tests/SlimTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 6bec553..ed2e2a7 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -264,7 +264,7 @@ public function testEmptyButNotNullMethodSuffixAccepted() ($this->app)($this->req, $this->res); } - public function testAddControllerRoute() + public function testAddControllerRouteSimple() { $this->setUrl('/'); $this->app->addControllerRoute( @@ -272,7 +272,10 @@ public function testAddControllerRoute() '/', 'Controller:index' ); + static::assertEquals( + \FastRoute\Dispatcher::FOUND, $this->app->getContainer()->get('router')->dispatch($this->req)[0] + ); } public function testAddControllerRouteWithMiddleware() From bd2c0cb1727f64ff9f37ee94cdeb30258dbad368 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 12:37:59 +0000 Subject: [PATCH 31/32] Migrated testServiceControllersAreFetchedSimple --- tests/SlimController/Tests/SlimTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index ed2e2a7..4c6d3e7 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -325,7 +325,7 @@ public function testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotExist static::assertEquals('/', $this->app->getContainer()->get('router')->pathFor('this is not a named route')); } - public function testServiceControllersAreFetched() + public function testServiceControllersAreFetchedSimple() { $this->expectOutputString("What is up?"); @@ -342,8 +342,8 @@ public function testServiceControllersAreFetched() ); // If the route could be dispatched, then the service was found - $result = $route->dispatch(); - static::assertTrue($result); + $result = ($this->app)($this->req, $this->res); + static::assertEquals(200, $result->getStatusCode()); } public function testServiceControllersAreFetchedWithParams() From 8b4b18d88a0492c0f8b63dde00bf797fe90c0624 Mon Sep 17 00:00:00 2001 From: Sami Greenbury Date: Thu, 27 Jan 2022 15:44:11 +0000 Subject: [PATCH 32/32] Started attempting to migrate testServiceControllersAreFetchedWithParams --- tests/SlimController/Tests/SlimTest.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 4c6d3e7..4b9d9ea 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -353,17 +353,20 @@ public function testServiceControllersAreFetchedWithParams() $config = array( 'controller.class_prefix' => '', 'controller.class_suffix' => '', - ); - $this->setUrl('/', '', $config); + ); + $this->setUrl('/another/foo', '', $config); $app = $this->app; - $app->getContainer()['TestController'] = fn() => new TestController($app); + $app->getContainer()['TestController'] = fn () => new TestController($app); - $app->addRoutes(array( - '/another/:name' => 'TestController:hello' - )); - $route = $app->container->get('router')->getNamedRoute('TestController:hello'); - $route->setParams(array('name' => 'foo')); - static::assertTrue($route->dispatch()); + $route = $this->app->addControllerRoute( + 'GET', + '/another/{name}', + 'TestController:hello' + ); + + // If the route could be dispatched, then the service was found + $result = ($this->app)($this->req, $this->res); + static::assertEquals(200, $result->getStatusCode()); } public function testServiceControllersAreFetchedEvenIfTheirNameIsAnInvalidPHPClassName()