2017-11-01 14:47:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Http;
|
|
|
|
|
2018-08-13 16:30:27 +02:00
|
|
|
use Engelsystem\Config\Config;
|
|
|
|
use Engelsystem\Container\ServiceProvider;
|
2017-11-01 14:47:09 +01:00
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
use Engelsystem\Http\RequestServiceProvider;
|
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-09-02 02:09:56 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
|
2017-11-01 14:47:09 +01:00
|
|
|
|
|
|
|
class RequestServiceProviderTest extends ServiceProviderTest
|
|
|
|
{
|
2022-02-12 01:16:50 +01:00
|
|
|
public function provideRegister(): array
|
2017-11-01 14:47:09 +01:00
|
|
|
{
|
2018-08-13 16:30:27 +02:00
|
|
|
return [
|
|
|
|
['', []],
|
|
|
|
[[], []],
|
|
|
|
['192.168.10.99', ['192.168.10.99']],
|
|
|
|
[' 234.234.234.234 ', ['234.234.234.234']],
|
|
|
|
['123.234.123.234,10.0.0.0/8', ['123.234.123.234', '10.0.0.0/8']],
|
|
|
|
['123.123.234.234 , ' . PHP_EOL . ' 11.22.33.44/22 ', ['123.123.234.234', '11.22.33.44/22']],
|
|
|
|
[['10.100.20.0/24'], ['10.100.20.0/24']],
|
|
|
|
];
|
|
|
|
}
|
2017-11-01 14:47:09 +01:00
|
|
|
|
2018-08-13 16:30:27 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideRegister
|
2022-02-12 01:16:50 +01:00
|
|
|
* @covers \Engelsystem\Http\RequestServiceProvider::register
|
2018-08-13 16:30:27 +02:00
|
|
|
*/
|
2022-02-12 01:16:50 +01:00
|
|
|
public function testRegister(string|array $configuredProxies, array $trustedProxies): void
|
2018-08-13 16:30:27 +02:00
|
|
|
{
|
2022-02-12 01:16:50 +01:00
|
|
|
$config = new Config([
|
|
|
|
'trusted_proxies' => $configuredProxies,
|
|
|
|
]);
|
2018-08-13 16:30:27 +02:00
|
|
|
/** @var Request|MockObject $request */
|
|
|
|
$request = $this->getMockBuilder(Request::class)->getMock();
|
|
|
|
|
|
|
|
$app = $this->getApp(['call', 'get', 'instance']);
|
2017-11-01 14:47:09 +01:00
|
|
|
|
|
|
|
$this->setExpects($app, 'call', [[Request::class, 'createFromGlobals']], $request);
|
2018-08-13 16:30:27 +02:00
|
|
|
$this->setExpects($app, 'get', ['config'], $config);
|
2017-11-01 14:47:09 +01:00
|
|
|
|
2018-09-02 02:09:56 +02:00
|
|
|
$app->expects($this->exactly(3))
|
|
|
|
->method('instance')
|
|
|
|
->withConsecutive(
|
|
|
|
[Request::class, $request],
|
|
|
|
[SymfonyRequest::class, $request],
|
|
|
|
['request', $request]
|
|
|
|
);
|
2017-11-01 14:47:09 +01:00
|
|
|
|
2018-08-13 16:30:27 +02:00
|
|
|
/** @var ServiceProvider|MockObject $serviceProvider */
|
|
|
|
$serviceProvider = $this->getMockBuilder(RequestServiceProvider::class)
|
|
|
|
->setConstructorArgs([$app])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['setTrustedProxies'])
|
2018-08-13 16:30:27 +02:00
|
|
|
->getMock();
|
2022-02-12 01:16:50 +01:00
|
|
|
$this->setExpects($serviceProvider, 'setTrustedProxies', [$request, $trustedProxies]);
|
2017-11-01 14:47:09 +01:00
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
2022-02-12 01:16:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\RequestServiceProvider::register
|
|
|
|
*/
|
|
|
|
public function testRegisterRewritingPrefix(): void
|
|
|
|
{
|
|
|
|
$config = new Config([
|
|
|
|
'url' => 'https://some.app/subpath',
|
|
|
|
]);
|
|
|
|
$this->app->instance('config', $config);
|
|
|
|
$request = new Request();
|
|
|
|
|
|
|
|
/** @var ServiceProvider|MockObject $serviceProvider */
|
|
|
|
$serviceProvider = $this->getMockBuilder(RequestServiceProvider::class)
|
|
|
|
->setConstructorArgs([$this->app])
|
|
|
|
->onlyMethods(['createRequestWithoutPrefix'])
|
|
|
|
->getMock();
|
|
|
|
$this->setExpects($serviceProvider, 'createRequestWithoutPrefix', null, $request);
|
|
|
|
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide test data: [requested uri; expected rewrite, configured app url]
|
|
|
|
*
|
|
|
|
* @return string[][]
|
|
|
|
*/
|
|
|
|
public function provideRequestPathPrefix(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['/', '/'],
|
|
|
|
['/sub', '/sub'],
|
|
|
|
['/subpath2', '/subpath2'],
|
|
|
|
['/subpath2/test', '/subpath2/test'],
|
|
|
|
['/subpath', '/'],
|
|
|
|
['/subpath/', '/'],
|
|
|
|
['/subpath/test', '/test'],
|
|
|
|
['/subpath/foo/bar', '/foo/bar'],
|
|
|
|
['/path/foo/bar', '/foo/bar', 'https://some.app/path/'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\RequestServiceProvider::createRequestWithoutPrefix
|
|
|
|
* @dataProvider provideRequestPathPrefix
|
|
|
|
*/
|
|
|
|
public function testCreateRequestWithoutPrefix(string $requestUri, string $expected, string $url = null): void
|
|
|
|
{
|
|
|
|
$_SERVER['REQUEST_URI'] = $requestUri;
|
|
|
|
$config = new Config([
|
|
|
|
'url' => $url ?: 'https://some.app/subpath',
|
|
|
|
]);
|
|
|
|
$this->app->instance('config', $config);
|
|
|
|
$serviceProvider = new RequestServiceProvider($this->app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
|
|
|
|
/** @var Request $request */
|
|
|
|
$request = $this->app->get('request');
|
|
|
|
$this->assertEquals($expected, $request->getPathInfo());
|
|
|
|
}
|
2017-11-01 14:47:09 +01:00
|
|
|
}
|