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;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2020-02-02 21:13:32 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
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-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2020-02-08 14:22:07 +03:00
|
|
|
private DropDefaultDomainFromRequestMiddleware $middleware;
|
2020-02-02 21:13:32 +03:00
|
|
|
private ObjectProphecy $next;
|
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2020-02-02 21:13:32 +03:00
|
|
|
{
|
|
|
|
$this->next = $this->prophesize(RequestHandlerInterface::class);
|
2020-02-08 14:22:07 +03:00
|
|
|
$this->middleware = new DropDefaultDomainFromRequestMiddleware('doma.in');
|
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
|
|
|
|
2020-02-08 14:22:07 +03:00
|
|
|
$handle = $this->next->handle(Argument::that(function (ServerRequestInterface $request) use ($expectedPayload) {
|
|
|
|
Assert::assertEquals($expectedPayload, $request->getQueryParams());
|
|
|
|
Assert::assertEquals($expectedPayload, $request->getParsedBody());
|
2020-02-02 21:13:32 +03:00
|
|
|
return $request;
|
|
|
|
}))->willReturn(new Response());
|
|
|
|
|
|
|
|
$this->middleware->process($req, $this->next->reveal());
|
|
|
|
|
|
|
|
$handle->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideQueryParams(): iterable
|
|
|
|
{
|
|
|
|
yield [[], []];
|
|
|
|
yield [['foo' => 'bar'], ['foo' => 'bar']];
|
|
|
|
yield [['foo' => 'bar', 'domain' => 'doma.in'], ['foo' => 'bar']];
|
|
|
|
yield [['foo' => 'bar', 'domain' => 'not_default'], ['foo' => 'bar', 'domain' => 'not_default']];
|
|
|
|
yield [['domain' => 'doma.in'], []];
|
|
|
|
}
|
|
|
|
}
|