mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Migrated UrlShortenerOptions to immutable object
This commit is contained in:
@@ -34,12 +34,10 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private ExtraPathRedirectMiddleware $middleware;
|
||||
private ObjectProphecy $resolver;
|
||||
private ObjectProphecy $requestTracker;
|
||||
private ObjectProphecy $redirectionBuilder;
|
||||
private ObjectProphecy $redirectResponseHelper;
|
||||
private UrlShortenerOptions $options;
|
||||
private ObjectProphecy $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
@@ -48,16 +46,6 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
|
||||
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
|
||||
$this->redirectionBuilder = $this->prophesize(ShortUrlRedirectionBuilderInterface::class);
|
||||
$this->redirectResponseHelper = $this->prophesize(RedirectResponseHelperInterface::class);
|
||||
$this->options = new UrlShortenerOptions(['append_extra_path' => true]);
|
||||
|
||||
$this->middleware = new ExtraPathRedirectMiddleware(
|
||||
$this->resolver->reveal(),
|
||||
$this->requestTracker->reveal(),
|
||||
$this->redirectionBuilder->reveal(),
|
||||
$this->redirectResponseHelper->reveal(),
|
||||
$this->options,
|
||||
);
|
||||
|
||||
$this->handler = $this->prophesize(RequestHandlerInterface::class);
|
||||
$this->handler->handle(Argument::cetera())->willReturn(new RedirectResponse(''));
|
||||
}
|
||||
@@ -71,10 +59,12 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
|
||||
bool $multiSegmentEnabled,
|
||||
ServerRequestInterface $request,
|
||||
): void {
|
||||
$this->options->appendExtraPath = $appendExtraPath;
|
||||
$this->options->multiSegmentSlugsEnabled = $multiSegmentEnabled;
|
||||
$options = new UrlShortenerOptions(
|
||||
appendExtraPath: $appendExtraPath,
|
||||
multiSegmentSlugsEnabled: $multiSegmentEnabled,
|
||||
);
|
||||
|
||||
$this->middleware->process($request, $this->handler->reveal());
|
||||
$this->middleware($options)->process($request, $this->handler->reveal());
|
||||
|
||||
$this->handler->handle($request)->shouldHaveBeenCalledOnce();
|
||||
$this->resolver->resolveEnabledShortUrl(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
@@ -123,7 +113,7 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
|
||||
bool $multiSegmentEnabled,
|
||||
int $expectedResolveCalls,
|
||||
): void {
|
||||
$this->options->multiSegmentSlugsEnabled = $multiSegmentEnabled;
|
||||
$options = new UrlShortenerOptions(appendExtraPath: true, multiSegmentSlugsEnabled: $multiSegmentEnabled);
|
||||
|
||||
$type = $this->prophesize(NotFoundType::class);
|
||||
$type->isRegularNotFound()->willReturn(true);
|
||||
@@ -135,7 +125,7 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
|
||||
Argument::that(fn (ShortUrlIdentifier $identifier) => str_starts_with($identifier->shortCode, 'shortCode')),
|
||||
)->willThrow(ShortUrlNotFoundException::class);
|
||||
|
||||
$this->middleware->process($request, $this->handler->reveal());
|
||||
$this->middleware($options)->process($request, $this->handler->reveal());
|
||||
|
||||
$resolve->shouldHaveBeenCalledTimes($expectedResolveCalls);
|
||||
$this->requestTracker->trackIfApplicable(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
@@ -152,7 +142,7 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
|
||||
int $expectedResolveCalls,
|
||||
?string $expectedExtraPath,
|
||||
): void {
|
||||
$this->options->multiSegmentSlugsEnabled = $multiSegmentEnabled;
|
||||
$options = new UrlShortenerOptions(appendExtraPath: true, multiSegmentSlugsEnabled: $multiSegmentEnabled);
|
||||
|
||||
$type = $this->prophesize(NotFoundType::class);
|
||||
$type->isRegularNotFound()->willReturn(true);
|
||||
@@ -181,7 +171,7 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
|
||||
new RedirectResponse(''),
|
||||
);
|
||||
|
||||
$this->middleware->process($request, $this->handler->reveal());
|
||||
$this->middleware($options)->process($request, $this->handler->reveal());
|
||||
|
||||
$resolve->shouldHaveBeenCalledTimes($expectedResolveCalls);
|
||||
$buildLongUrl->shouldHaveBeenCalledOnce();
|
||||
@@ -194,4 +184,15 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
|
||||
yield [false, 1, '/bar/baz'];
|
||||
yield [true, 3, null];
|
||||
}
|
||||
|
||||
private function middleware(?UrlShortenerOptions $options = null): ExtraPathRedirectMiddleware
|
||||
{
|
||||
return new ExtraPathRedirectMiddleware(
|
||||
$this->resolver->reveal(),
|
||||
$this->requestTracker->reveal(),
|
||||
$this->redirectionBuilder->reveal(),
|
||||
$this->redirectResponseHelper->reveal(),
|
||||
$options ?? new UrlShortenerOptions(appendExtraPath: true),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,15 +23,11 @@ class UrlValidatorTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private UrlValidator $urlValidator;
|
||||
private ObjectProphecy $httpClient;
|
||||
private UrlShortenerOptions $options;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->httpClient = $this->prophesize(ClientInterface::class);
|
||||
$this->options = new UrlShortenerOptions(['validate_url' => true]);
|
||||
$this->urlValidator = new UrlValidator($this->httpClient->reveal(), $this->options);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -42,7 +38,7 @@ class UrlValidatorTest extends TestCase
|
||||
$request->shouldBeCalledOnce();
|
||||
$this->expectException(InvalidUrlException::class);
|
||||
|
||||
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar', true);
|
||||
$this->urlValidator()->validateUrl('http://foobar.com/12345/hello?foo=bar', true);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -65,7 +61,7 @@ class UrlValidatorTest extends TestCase
|
||||
}),
|
||||
)->willReturn(new Response());
|
||||
|
||||
$this->urlValidator->validateUrl($expectedUrl, true);
|
||||
$this->urlValidator()->validateUrl($expectedUrl, true);
|
||||
|
||||
$request->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
@@ -75,7 +71,7 @@ class UrlValidatorTest extends TestCase
|
||||
{
|
||||
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response());
|
||||
|
||||
$this->urlValidator->validateUrl('', false);
|
||||
$this->urlValidator()->validateUrl('', false);
|
||||
|
||||
$request->shouldNotHaveBeenCalled();
|
||||
}
|
||||
@@ -84,9 +80,8 @@ class UrlValidatorTest extends TestCase
|
||||
public function validateUrlWithTitleReturnsNullWhenRequestFailsAndValidationIsDisabled(): void
|
||||
{
|
||||
$request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class);
|
||||
$this->options->autoResolveTitles = true;
|
||||
|
||||
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
|
||||
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
|
||||
|
||||
self::assertNull($result);
|
||||
$request->shouldHaveBeenCalledOnce();
|
||||
@@ -96,9 +91,8 @@ class UrlValidatorTest extends TestCase
|
||||
public function validateUrlWithTitleReturnsNullWhenAutoResolutionIsDisabled(): void
|
||||
{
|
||||
$request = $this->httpClient->request(Argument::cetera())->willReturn($this->respWithTitle());
|
||||
$this->options->autoResolveTitles = false;
|
||||
|
||||
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
|
||||
$result = $this->urlValidator()->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', false);
|
||||
|
||||
self::assertNull($result);
|
||||
$request->shouldNotHaveBeenCalled();
|
||||
@@ -110,9 +104,8 @@ class UrlValidatorTest extends TestCase
|
||||
$request = $this->httpClient->request(RequestMethodInterface::METHOD_HEAD, Argument::cetera())->willReturn(
|
||||
$this->respWithTitle(),
|
||||
);
|
||||
$this->options->autoResolveTitles = false;
|
||||
|
||||
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
||||
$result = $this->urlValidator()->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
||||
|
||||
self::assertNull($result);
|
||||
$request->shouldHaveBeenCalledOnce();
|
||||
@@ -124,9 +117,8 @@ class UrlValidatorTest extends TestCase
|
||||
$request = $this->httpClient->request(RequestMethodInterface::METHOD_GET, Argument::cetera())->willReturn(
|
||||
$this->respWithTitle(),
|
||||
);
|
||||
$this->options->autoResolveTitles = true;
|
||||
|
||||
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
||||
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
||||
|
||||
self::assertEquals('Resolved "title"', $result);
|
||||
$request->shouldHaveBeenCalledOnce();
|
||||
@@ -138,9 +130,8 @@ class UrlValidatorTest extends TestCase
|
||||
$request = $this->httpClient->request(RequestMethodInterface::METHOD_GET, Argument::cetera())->willReturn(
|
||||
new Response('php://memory', 200, ['Content-Type' => 'application/octet-stream']),
|
||||
);
|
||||
$this->options->autoResolveTitles = true;
|
||||
|
||||
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
||||
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
||||
|
||||
self::assertNull($result);
|
||||
$request->shouldHaveBeenCalledOnce();
|
||||
@@ -152,9 +143,8 @@ class UrlValidatorTest extends TestCase
|
||||
$request = $this->httpClient->request(RequestMethodInterface::METHOD_GET, Argument::cetera())->willReturn(
|
||||
new Response($this->createStreamWithContent('<body>No title</body>'), 200, ['Content-Type' => 'text/html']),
|
||||
);
|
||||
$this->options->autoResolveTitles = true;
|
||||
|
||||
$result = $this->urlValidator->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
||||
$result = $this->urlValidator(true)->validateUrlWithTitle('http://foobar.com/12345/hello?foo=bar', true);
|
||||
|
||||
self::assertNull($result);
|
||||
$request->shouldHaveBeenCalledOnce();
|
||||
@@ -174,4 +164,12 @@ class UrlValidatorTest extends TestCase
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public function urlValidator(bool $autoResolveTitles = false): UrlValidator
|
||||
{
|
||||
return new UrlValidator(
|
||||
$this->httpClient->reveal(),
|
||||
new UrlShortenerOptions(autoResolveTitles: $autoResolveTitles),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user