From f9d0e57f913d55dedfb8c5175ae12dfe0b88d557 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 19 Jan 2020 22:38:33 +0100 Subject: [PATCH] Fix tests & Router is now using Dependency Injection instead of DI Registry --- src/App/Router.php | 14 +++++--- tests/src/App/ModuleTest.php | 6 +++- tests/src/App/RouterTest.php | 33 +++++++++++++------ .../ConfigCacheTest.php => CacheTest.php} | 4 +-- .../{ConfigurationTest.php => ConfigTest.php} | 2 +- ...onfigurationTest.php => JitConfigTest.php} | 2 +- ...gurationTest.php => PreloadConfigTest.php} | 2 +- tests/src/Core/Lock/SemaphoreLockTest.php | 2 +- .../CacheTest.php} | 4 +-- .../JitPConfigTest.php} | 5 +-- .../PConfigTest.php} | 4 +-- .../PreloadPConfigTest.php} | 5 +-- 12 files changed, 54 insertions(+), 29 deletions(-) rename tests/src/Core/Config/{Cache/ConfigCacheTest.php => CacheTest.php} (98%) rename tests/src/Core/Config/{ConfigurationTest.php => ConfigTest.php} (99%) rename tests/src/Core/Config/{JitConfigurationTest.php => JitConfigTest.php} (98%) rename tests/src/Core/Config/{PreloadConfigurationTest.php => PreloadConfigTest.php} (98%) rename tests/src/Core/{Config/Cache/PConfigCacheTest.php => PConfig/CacheTest.php} (98%) rename tests/src/Core/{Config/JitPConfigurationTest.php => PConfig/JitPConfigTest.php} (96%) rename tests/src/Core/{Config/PConfigurationTest.php => PConfig/PConfigTest.php} (99%) rename tests/src/Core/{Config/PreloadPConfigurationTest.php => PConfig/PreloadPConfigTest.php} (96%) diff --git a/src/App/Router.php b/src/App/Router.php index 6ae42bb141..450822cf86 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -8,7 +8,7 @@ use FastRoute\Dispatcher; use FastRoute\RouteCollector; use FastRoute\RouteParser\Std; use Friendica\Core\Hook; -use Friendica\DI; +use Friendica\Core\L10n; use Friendica\Network\HTTPException; /** @@ -44,12 +44,18 @@ class Router */ private $parameters = []; + /** @var L10n */ + private $l10n; + /** * @param array $server The $_SERVER variable + * @param L10n $l10n * @param RouteCollector|null $routeCollector Optional the loaded Route collector */ - public function __construct(array $server, RouteCollector $routeCollector = null) + public function __construct(array $server, L10n $l10n, RouteCollector $routeCollector = null) { + $this->l10n = $l10n; + $httpMethod = $server['REQUEST_METHOD'] ?? self::GET; $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET; @@ -181,9 +187,9 @@ class Router $moduleClass = $routeInfo[1]; $this->parameters = $routeInfo[2]; } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { - throw new HTTPException\MethodNotAllowedException(DI::l10n()->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); + throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); } else { - throw new HTTPException\NotFoundException(DI::l10n()->t('Page not found.')); + throw new HTTPException\NotFoundException($this->l10n->t('Page not found.')); } return $moduleClass; diff --git a/tests/src/App/ModuleTest.php b/tests/src/App/ModuleTest.php index f76d51843c..5b8bdcd3d6 100644 --- a/tests/src/App/ModuleTest.php +++ b/tests/src/App/ModuleTest.php @@ -4,6 +4,7 @@ namespace Friendica\Test\src\App; use Friendica\App; use Friendica\Core\Config\IConfig; +use Friendica\Core\L10n; use Friendica\LegacyModule; use Friendica\Module\HTTPException\PageNotFound; use Friendica\Module\WellKnown\HostMeta; @@ -152,7 +153,10 @@ class ModuleTest extends DatabaseTest $config = \Mockery::mock(IConfig::class); $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once(); - $router = (new App\Router([]))->loadRoutes(include __DIR__ . '/../../../static/routes.config.php'); + $l10n = \Mockery::mock(L10n::class); + $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; }); + + $router = (new App\Router([], $l10n))->loadRoutes(include __DIR__ . '/../../../static/routes.config.php'); $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config); diff --git a/tests/src/App/RouterTest.php b/tests/src/App/RouterTest.php index 102808f6ac..fc34df9575 100644 --- a/tests/src/App/RouterTest.php +++ b/tests/src/App/RouterTest.php @@ -3,16 +3,29 @@ namespace Friendica\Test\src\App; use Friendica\App\Router; +use Friendica\Core\L10n; use Friendica\Module; use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\NotFoundException; +use Mockery\MockInterface; use PHPUnit\Framework\TestCase; class RouterTest extends TestCase { + /** @var L10n|MockInterface */ + private $l10n; + + protected function setUp() + { + parent::setUp(); + + $this->l10n = \Mockery::mock(L10n::class); + $this->l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; }); + } + public function testGetModuleClass() { - $router = new Router(['REQUEST_METHOD' => Router::GET]); + $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $routeCollector = $router->getRouteCollector(); $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName'); @@ -36,7 +49,7 @@ class RouterTest extends TestCase public function testPostModuleClass() { - $router = new Router(['REQUEST_METHOD' => Router::POST]); + $router = new Router(['REQUEST_METHOD' => Router::POST], $this->l10n); $routeCollector = $router->getRouteCollector(); $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName'); @@ -62,7 +75,7 @@ class RouterTest extends TestCase { $this->expectException(NotFoundException::class); - $router = new Router(['REQUEST_METHOD' => Router::GET]); + $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $router->getModuleClass('/unsupported'); } @@ -71,7 +84,7 @@ class RouterTest extends TestCase { $this->expectException(NotFoundException::class); - $router = new Router(['REQUEST_METHOD' => Router::GET]); + $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $routeCollector = $router->getRouteCollector(); $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); @@ -83,7 +96,7 @@ class RouterTest extends TestCase { $this->expectException(NotFoundException::class); - $router = new Router(['REQUEST_METHOD' => Router::GET]); + $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $routeCollector = $router->getRouteCollector(); $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName'); @@ -95,7 +108,7 @@ class RouterTest extends TestCase { $this->expectException(NotFoundException::class); - $router = new Router(['REQUEST_METHOD' => Router::GET]); + $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $routeCollector = $router->getRouteCollector(); $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName'); @@ -107,7 +120,7 @@ class RouterTest extends TestCase { $this->expectException(MethodNotAllowedException::class); - $router = new Router(['REQUEST_METHOD' => Router::POST]); + $router = new Router(['REQUEST_METHOD' => Router::POST], $this->l10n); $routeCollector = $router->getRouteCollector(); $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); @@ -119,7 +132,7 @@ class RouterTest extends TestCase { $this->expectException(MethodNotAllowedException::class); - $router = new Router(['REQUEST_METHOD' => Router::GET]); + $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n); $routeCollector = $router->getRouteCollector(); $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName'); @@ -159,7 +172,7 @@ class RouterTest extends TestCase { $router = (new Router([ 'REQUEST_METHOD' => Router::GET - ]))->loadRoutes($routes); + ], $this->l10n))->loadRoutes($routes); $this->assertEquals(Module\Home::class, $router->getModuleClass('/')); $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route')); @@ -174,7 +187,7 @@ class RouterTest extends TestCase { $router = (new Router([ 'REQUEST_METHOD' => Router::POST - ]))->loadRoutes($routes); + ], $this->l10n))->loadRoutes($routes); // Don't find GET $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it')); diff --git a/tests/src/Core/Config/Cache/ConfigCacheTest.php b/tests/src/Core/Config/CacheTest.php similarity index 98% rename from tests/src/Core/Config/Cache/ConfigCacheTest.php rename to tests/src/Core/Config/CacheTest.php index 6d0b045db4..9a398f5d5d 100644 --- a/tests/src/Core/Config/Cache/ConfigCacheTest.php +++ b/tests/src/Core/Config/CacheTest.php @@ -1,12 +1,12 @@ shouldReceive('get') - ->with('system', 'temppath', NULL, false) + ->with('system', 'temppath') ->andReturn('/tmp/'); $dice->shouldReceive('create')->with(IConfig::class)->andReturn($configMock); diff --git a/tests/src/Core/Config/Cache/PConfigCacheTest.php b/tests/src/Core/PConfig/CacheTest.php similarity index 98% rename from tests/src/Core/Config/Cache/PConfigCacheTest.php rename to tests/src/Core/PConfig/CacheTest.php index 1d5a96f695..62f6d22dfa 100644 --- a/tests/src/Core/Config/Cache/PConfigCacheTest.php +++ b/tests/src/Core/PConfig/CacheTest.php @@ -1,11 +1,11 @@