Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Partial migration to support Slim 3 (and full phpunit 9 migration) #46

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e94f02e
Installed rector
Patabugen Jan 26, 2022
4dee633
Added .phpunit.result.cache to .gitignore
Patabugen Jan 26, 2022
5b6d8e1
Updated to php 7.4 and phpunit 9 suppoert
Patabugen Jan 26, 2022
0c93ed7
Replaced Slim\Slim with Slim\App
Patabugen Jan 26, 2022
cbeff65
Updated Slim from 2 to 3
Patabugen Jan 26, 2022
41836e6
Migrated Slim\Environment to Slim\Http\Environment
Patabugen Jan 26, 2022
b6dda99
Migrated config() calls in SlimController
Patabugen Jan 26, 2022
fa3f440
Migrated: Move HttpMethod to Slim\App::map first parameter instead of…
Patabugen Jan 26, 2022
ca0cc29
Migrated $app->container to $app->getContainer
Patabugen Jan 26, 2022
3615e69
Update addControllerRoute to pass the method
Patabugen Jan 27, 2022
5677096
Removed old tests
Patabugen Jan 27, 2022
d9c0662
Replaced app->router() with app->container->get('router')
Patabugen Jan 27, 2022
3429f9e
Fixed capitalisation of httpMethod and updated $route->name() to setN…
Patabugen Jan 27, 2022
bf17681
Updated Environment:mock and removed defaults
Patabugen Jan 27, 2022
bb49f89
Updated testAddSimpleRoutes
Patabugen Jan 27, 2022
75cce47
Migrated testAddingRoutesToSameMethod
Patabugen Jan 27, 2022
b2f16b2
Migrated testAddingroutesWithOldSyntaxWithoutMiddlewares
Patabugen Jan 27, 2022
d9e7be3
Migrated testAddRoutesWithOldSyntaxWithoutMiddlewareArray
Patabugen Jan 27, 2022
43bb50b
Migrated testAddRoutesWithOldSyntaxWithMiddlewareArray
Patabugen Jan 27, 2022
ffd4c59
Migrated testAddRoutesWithVariables
Patabugen Jan 27, 2022
03b0509
Migrted testAddRoutesInExtendedFormat
Patabugen Jan 27, 2022
7aa4daa
Corrected $app->container to $app->getContainer()
Patabugen Jan 27, 2022
6adc471
Migrated testNamedRoutes
Patabugen Jan 27, 2022
94c6d35
Migrated testNamedRoutesThrowsExceptionIfLookingForARouteThatDoesNotE…
Patabugen Jan 27, 2022
5f49a50
Migrated container to getContainer in SlimController\Slim
Patabugen Jan 27, 2022
226de05
Migrated Settings into it's sub-key in the App constructor
Patabugen Jan 27, 2022
1d5bc36
Fixed container() to getContainer()
Patabugen Jan 27, 2022
83ac302
Migrated testRouteCallbacksAreFiredOnDispatch
Patabugen Jan 27, 2022
a6f2bc0
Migrated testEmptyButNotNullMethodSuffixAccepted
Patabugen Jan 27, 2022
bf3a5de
Migrated testAddControllerRouteSimple
Patabugen Jan 27, 2022
bd2c0cb
Migrated testServiceControllersAreFetchedSimple
Patabugen Jan 27, 2022
8b4b18d
Started attempting to migrate testServiceControllersAreFetchedWithParams
Patabugen Jan 27, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor/
composer.lock
composer.lock
.phpunit.result.cache
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# unreleased
- BREAKING: You can no longer add routes after the app has been dispatched
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
],
"require": {
"php": ">=5.3.0",
"slim/slim": "2.*"
"slim/slim": "3.*"
},
"require-dev": {
"phpunit/phpunit": "4.3.5",
"mockery/mockery": "0.8.*"
"phpunit/phpunit": "^9",
"mockery/mockery": "0.8.*",
"rector/rector": "^0.12.15"
},
"autoload": {
"psr-0": {
Expand Down
7 changes: 0 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Unit Tests">
<directory>./tests/SlimController/Tests</directory>
<exclude>./tests/SlimController/Tests/Old/</exclude>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
28 changes: 28 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\PHPUnit\Set\PHPUnitLevelSetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
$parameters = $containerConfigurator->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(PHPUnitLevelSetList::UP_TO_PHPUNIT_90 );

// get services (needed for register a single rule)
// $services = $containerConfigurator->services();

// register a single rule
// $services->set(TypedPropertyRector::class);
};
41 changes: 22 additions & 19 deletions src/SlimController/Slim.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Extended Slim base
*/
class Slim extends \Slim\Slim
class Slim extends \Slim\App
{

/**
Expand Down Expand Up @@ -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);
$route->setName($classRoute);
$this->routeNames[$classRoute] = 1;
}

if ('any' === $httpMethod) {
call_user_func_array(array($route, 'via'), static::$ALLOWED_HTTP_METHODS);
} else {
$route->via($httpMethod);
}
}
}

Expand All @@ -131,11 +132,11 @@ public function addRoutes(array $routes, $globalMiddlewares = array())
* Add a new controller route
*
* <code>
* $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(..);
* </code>
*
* @param string $path
Expand All @@ -144,11 +145,13 @@ 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);

$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);
Expand All @@ -165,13 +168,13 @@ 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
$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);
};
Expand All @@ -189,14 +192,14 @@ 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) !== '\\')) {
$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';
}
Expand Down
26 changes: 11 additions & 15 deletions src/SlimController/SlimController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class SlimController
/**
* @const string
*/
const VERSION = '0.1.4';
public const VERSION = '0.1.4';

/**
* @var Slim
Expand All @@ -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
Expand All @@ -67,22 +67,22 @@ 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')) {
if ($renderTemplateSuffix = $app->getContainer()->get('settings')['controller.template_suffix']) {
$this->renderTemplateSuffix = $renderTemplateSuffix;
}
if (!is_null($paramPrefix = $app->config('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->config('controller.cleanup_params')) {
if ($app->getContainer()->get('settings')['controller.cleanup_params']) {
$this->paramCleanup = true;
}
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<bar>', '<other>Notgood');
echo json_encode($this->cleanupParam($messedUp));
echo json_encode($this->cleanupParam($messedUp), JSON_THROW_ON_ERROR);
}

public function renderAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
44 changes: 0 additions & 44 deletions tests/SlimController/Tests/Old/ControllerTest.php

This file was deleted.

Loading