/** * @test */ public function buildResponseIfApplicableRetunsHttpRequestIfApplicableRedirectIsFound() { $this->mockHttpRequest->expects($this->atLeastOnce())->method('getRelativePath')->willReturn('some/relative/path'); $mockRedirect = $this->getMockBuilder(Redirect::class)->disableOriginalConstructor()->getMock(); $mockRedirect->expects($this->atLeastOnce())->method('getStatusCode')->willReturn(301); $this->mockRedirectStorage->expects($this->once())->method('getOneBySourceUriPathAndHost')->with('some/relative/path')->willReturn($mockRedirect); $this->inject($this->redirectService, 'redirectStorage', $this->mockRedirectStorage); $request = $this->redirectService->buildResponseIfApplicable($this->mockHttpRequest); $this->assertInstanceOf(Response::class, $request); }
/** * @param array $redirects * @return void * @throws Exception */ public function emitRedirectCreated(array $redirects) { foreach ($redirects as $redirect) { if (!$redirect instanceof RedirectInterface) { throw new Exception('Redirect should implement RedirectInterface', 1460139669); } $this->_redirectService->emitRedirectCreated($redirect); $this->_logger->log(sprintf('Redirect from %s %s -> %s (%d) added', $redirect->getHost(), $redirect->getSourceUriPath(), $redirect->getTargetUriPath(), $redirect->getStatusCode()), LOG_DEBUG); } }
/** * Check if the current request match a redirect * * @param ComponentContext $componentContext * @return void */ public function handle(ComponentContext $componentContext) { $routingMatchResults = $componentContext->getParameter(RoutingComponent::class, 'matchResults'); if ($routingMatchResults !== NULL) { return; } $httpRequest = $componentContext->getHttpRequest(); $response = $this->redirectService->buildResponseIfApplicable($httpRequest); if ($response !== null) { $componentContext->replaceHttpResponse($response); $componentContext->setParameter(ComponentChain::class, 'cancel', true); } }
/** * @test */ public function addRedirectEmitSignalAndFlushesRouterCacheForAffectedUri() { $this->mockRedirectRepository->expects($this->atLeastOnce())->method('findByTargetUriPathAndHost')->willReturn([]); $this->mockRouterCachingService->expects($this->once())->method('flushCachesForUriPath')->with('some/relative/path'); $this->redirectServiceMock->expects($this->atLeastOnce())->method('emitRedirectCreated'); $this->redirectStorage->addRedirect('some/relative/path', 'target'); }
/** * @test * @dataProvider addRedirectDataProvider * * @param array $existingRedirects * @param array $newRedirects * @param array $expectedRedirects */ public function addRedirectTests(array $existingRedirects, array $newRedirects, array $expectedRedirects) { foreach ($existingRedirects as $sourceUriPath => $targetUriPath) { $this->redirectService->addRedirect($sourceUriPath, $targetUriPath); } $this->persistenceManager->persistAll(); foreach ($newRedirects as $sourceUriPath => $targetUriPath) { $this->redirectService->addRedirect($sourceUriPath, $targetUriPath); } $this->persistenceManager->persistAll(); $resultingRedirects = []; foreach ($this->redirectRepository->findAll() as $redirect) { $resultingRedirects[$redirect->getSourceUriPath()] = $redirect->getTargetUriPath(); } $this->assertSame($expectedRedirects, $resultingRedirects); }