Fix tests & Router is now using Dependency Injection instead of DI Registry

This commit is contained in:
Philipp Holzer 2020-01-19 22:38:33 +01:00
parent d5a473abda
commit f9d0e57f91
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
12 changed files with 54 additions and 29 deletions

View file

@ -8,7 +8,7 @@ use FastRoute\Dispatcher;
use FastRoute\RouteCollector; use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std; use FastRoute\RouteParser\Std;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\DI; use Friendica\Core\L10n;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
/** /**
@ -44,12 +44,18 @@ class Router
*/ */
private $parameters = []; private $parameters = [];
/** @var L10n */
private $l10n;
/** /**
* @param array $server The $_SERVER variable * @param array $server The $_SERVER variable
* @param L10n $l10n
* @param RouteCollector|null $routeCollector Optional the loaded Route collector * @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; $httpMethod = $server['REQUEST_METHOD'] ?? self::GET;
$this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET; $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET;
@ -181,9 +187,9 @@ class Router
$moduleClass = $routeInfo[1]; $moduleClass = $routeInfo[1];
$this->parameters = $routeInfo[2]; $this->parameters = $routeInfo[2];
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { } 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 { } else {
throw new HTTPException\NotFoundException(DI::l10n()->t('Page not found.')); throw new HTTPException\NotFoundException($this->l10n->t('Page not found.'));
} }
return $moduleClass; return $moduleClass;

View file

@ -4,6 +4,7 @@ namespace Friendica\Test\src\App;
use Friendica\App; use Friendica\App;
use Friendica\Core\Config\IConfig; use Friendica\Core\Config\IConfig;
use Friendica\Core\L10n;
use Friendica\LegacyModule; use Friendica\LegacyModule;
use Friendica\Module\HTTPException\PageNotFound; use Friendica\Module\HTTPException\PageNotFound;
use Friendica\Module\WellKnown\HostMeta; use Friendica\Module\WellKnown\HostMeta;
@ -152,7 +153,10 @@ class ModuleTest extends DatabaseTest
$config = \Mockery::mock(IConfig::class); $config = \Mockery::mock(IConfig::class);
$config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once(); $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); $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);

View file

@ -3,16 +3,29 @@
namespace Friendica\Test\src\App; namespace Friendica\Test\src\App;
use Friendica\App\Router; use Friendica\App\Router;
use Friendica\Core\L10n;
use Friendica\Module; use Friendica\Module;
use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\MethodNotAllowedException;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\NotFoundException;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class RouterTest extends 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() public function testGetModuleClass()
{ {
$router = new Router(['REQUEST_METHOD' => Router::GET]); $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName'); $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName');
@ -36,7 +49,7 @@ class RouterTest extends TestCase
public function testPostModuleClass() public function testPostModuleClass()
{ {
$router = new Router(['REQUEST_METHOD' => Router::POST]); $router = new Router(['REQUEST_METHOD' => Router::POST], $this->l10n);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName'); $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName');
@ -62,7 +75,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET]); $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n);
$router->getModuleClass('/unsupported'); $router->getModuleClass('/unsupported');
} }
@ -71,7 +84,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET]); $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
@ -83,7 +96,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET]); $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName'); $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
@ -95,7 +108,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET]); $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName'); $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
@ -107,7 +120,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(MethodNotAllowedException::class); $this->expectException(MethodNotAllowedException::class);
$router = new Router(['REQUEST_METHOD' => Router::POST]); $router = new Router(['REQUEST_METHOD' => Router::POST], $this->l10n);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
@ -119,7 +132,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(MethodNotAllowedException::class); $this->expectException(MethodNotAllowedException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET]); $router = new Router(['REQUEST_METHOD' => Router::GET], $this->l10n);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
@ -159,7 +172,7 @@ class RouterTest extends TestCase
{ {
$router = (new Router([ $router = (new Router([
'REQUEST_METHOD' => Router::GET 'REQUEST_METHOD' => Router::GET
]))->loadRoutes($routes); ], $this->l10n))->loadRoutes($routes);
$this->assertEquals(Module\Home::class, $router->getModuleClass('/')); $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
$this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route')); $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
@ -174,7 +187,7 @@ class RouterTest extends TestCase
{ {
$router = (new Router([ $router = (new Router([
'REQUEST_METHOD' => Router::POST 'REQUEST_METHOD' => Router::POST
]))->loadRoutes($routes); ], $this->l10n))->loadRoutes($routes);
// Don't find GET // Don't find GET
$this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it')); $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));

View file

@ -1,12 +1,12 @@
<?php <?php
namespace Friendica\Test\src\Core\Config\Cache; namespace Friendica\Test\src\Core\Config;
use Friendica\Core\Config\Cache; use Friendica\Core\Config\Cache;
use Friendica\Test\MockedTest; use Friendica\Test\MockedTest;
use ParagonIE\HiddenString\HiddenString; use ParagonIE\HiddenString\HiddenString;
class ConfigCacheTest extends MockedTest class CacheTest extends MockedTest
{ {
public function dataTests() public function dataTests()
{ {

View file

@ -9,7 +9,7 @@ use Friendica\Test\MockedTest;
use Mockery\MockInterface; use Mockery\MockInterface;
use Mockery; use Mockery;
abstract class ConfigurationTest extends MockedTest abstract class ConfigTest extends MockedTest
{ {
/** @var ConfigModel|MockInterface */ /** @var ConfigModel|MockInterface */
protected $configModel; protected $configModel;

View file

@ -4,7 +4,7 @@ namespace Friendica\Test\src\Core\Config;
use Friendica\Core\Config\JitConfig; use Friendica\Core\Config\JitConfig;
class JitConfigurationTest extends ConfigurationTest class JitConfigTest extends ConfigTest
{ {
public function getInstance() public function getInstance()
{ {

View file

@ -4,7 +4,7 @@ namespace Friendica\Test\src\Core\Config;
use Friendica\Core\Config\PreloadConfig; use Friendica\Core\Config\PreloadConfig;
class PreloadConfigurationTest extends ConfigurationTest class PreloadConfigTest extends ConfigTest
{ {
public function getInstance() public function getInstance()
{ {

View file

@ -24,7 +24,7 @@ class SemaphoreLockTest extends LockTest
$configMock = \Mockery::mock(JitConfig::class); $configMock = \Mockery::mock(JitConfig::class);
$configMock $configMock
->shouldReceive('get') ->shouldReceive('get')
->with('system', 'temppath', NULL, false) ->with('system', 'temppath')
->andReturn('/tmp/'); ->andReturn('/tmp/');
$dice->shouldReceive('create')->with(IConfig::class)->andReturn($configMock); $dice->shouldReceive('create')->with(IConfig::class)->andReturn($configMock);

View file

@ -1,11 +1,11 @@
<?php <?php
namespace Friendica\Test\src\Core\Config\Cache; namespace Friendica\Test\src\Core\PConfig;
use Friendica\Core\PConfig\Cache; use Friendica\Core\PConfig\Cache;
use Friendica\Test\MockedTest; use Friendica\Test\MockedTest;
class PConfigCacheTest extends MockedTest class CacheTest extends MockedTest
{ {
public function dataTests() public function dataTests()
{ {

View file

@ -1,10 +1,11 @@
<?php <?php
namespace Friendica\Test\src\Core\Config; namespace Friendica\Test\src\Core\PConfig;
use Friendica\Core\PConfig\JitPConfig; use Friendica\Core\PConfig\JitPConfig;
use Friendica\Test\src\Core\PConfig\PConfigTest;
class JitPConfigurationTest extends PConfigurationTest class JitPConfigTest extends PConfigTest
{ {
public function getInstance() public function getInstance()
{ {

View file

@ -1,6 +1,6 @@
<?php <?php
namespace Friendica\Test\src\Core\Config; namespace Friendica\Test\src\Core\PConfig;
use Friendica\Core\PConfig\Cache; use Friendica\Core\PConfig\Cache;
use Friendica\Core\BasePConfig; use Friendica\Core\BasePConfig;
@ -9,7 +9,7 @@ use Friendica\Test\MockedTest;
use Mockery; use Mockery;
use Mockery\MockInterface; use Mockery\MockInterface;
abstract class PConfigurationTest extends MockedTest abstract class PConfigTest extends MockedTest
{ {
/** @var PConfigModel|MockInterface */ /** @var PConfigModel|MockInterface */
protected $configModel; protected $configModel;

View file

@ -1,10 +1,11 @@
<?php <?php
namespace Friendica\Test\src\Core\Config; namespace Friendica\Test\src\Core\PConfig;
use Friendica\Core\PConfig\PreloadPConfig; use Friendica\Core\PConfig\PreloadPConfig;
use Friendica\Test\src\Core\PConfig\PConfigTest;
class PreloadPConfigurationTest extends PConfigurationTest class PreloadPConfigTest extends PConfigTest
{ {
public function getInstance() public function getInstance()
{ {