2020-02-02 21:13:32 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
|
|
|
|
|
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use PHPUnit\Framework\Assert;
|
2022-10-23 23:56:12 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-02-02 21:13:32 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2020-02-08 14:22:07 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware;
|
2020-02-02 21:13:32 +03:00
|
|
|
|
2020-02-08 14:22:07 +03:00
|
|
|
class DropDefaultDomainFromRequestMiddlewareTest extends TestCase
|
2020-02-02 21:13:32 +03:00
|
|
|
{
|
2020-02-08 14:22:07 +03:00
|
|
|
private DropDefaultDomainFromRequestMiddleware $middleware;
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & RequestHandlerInterface $next;
|
2020-02-02 21:13:32 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2020-02-02 21:13:32 +03:00
|
|
|
{
|
2022-10-23 23:56:12 +03:00
|
|
|
$this->next = $this->createMock(RequestHandlerInterface::class);
|
2023-01-19 11:05:52 +03:00
|
|
|
$this->middleware = new DropDefaultDomainFromRequestMiddleware('s.test');
|
2020-02-02 21:13:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideQueryParams
|
|
|
|
*/
|
2020-02-08 14:22:07 +03:00
|
|
|
public function domainIsDroppedWhenDefaultOneIsProvided(array $providedPayload, array $expectedPayload): void
|
2020-02-02 21:13:32 +03:00
|
|
|
{
|
2020-02-08 14:22:07 +03:00
|
|
|
$req = ServerRequestFactory::fromGlobals()->withQueryParams($providedPayload)->withParsedBody($providedPayload);
|
2020-02-02 21:13:32 +03:00
|
|
|
|
2022-10-23 23:56:12 +03:00
|
|
|
$this->next->expects($this->once())->method('handle')->with($this->callback(
|
|
|
|
function (ServerRequestInterface $request) use ($expectedPayload) {
|
|
|
|
Assert::assertEquals($expectedPayload, $request->getQueryParams());
|
|
|
|
Assert::assertEquals($expectedPayload, $request->getParsedBody());
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
))->willReturn(new Response());
|
2020-02-02 21:13:32 +03:00
|
|
|
|
2022-10-23 23:56:12 +03:00
|
|
|
$this->middleware->process($req, $this->next);
|
2020-02-02 21:13:32 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideQueryParams(): iterable
|
2020-02-02 21:13:32 +03:00
|
|
|
{
|
|
|
|
yield [[], []];
|
|
|
|
yield [['foo' => 'bar'], ['foo' => 'bar']];
|
2023-01-19 11:05:52 +03:00
|
|
|
yield [['foo' => 'bar', 'domain' => 's.test'], ['foo' => 'bar']];
|
2020-02-02 21:13:32 +03:00
|
|
|
yield [['foo' => 'bar', 'domain' => 'not_default'], ['foo' => 'bar', 'domain' => 'not_default']];
|
2023-01-19 11:05:52 +03:00
|
|
|
yield [['domain' => 's.test'], []];
|
2020-02-02 21:13:32 +03:00
|
|
|
}
|
|
|
|
}
|