requestStack = new RequestStack(); $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface'); $this->redirectDestination = new RedirectDestination($this->requestStack, $this->urlGenerator); } protected function setupUrlGenerator() { $this->urlGenerator->expects($this->any()) ->method('generateFromRoute') ->willReturnCallback(function($route, $parameters, $options) { $query_string = ''; if (!empty($options['query'])) { $query_string = '?' . $options['query']; } return '/current-path' . $query_string; }); } /** * @dataProvider providerGet * * @covers ::get */ public function testGet(Request $request, $expected_destination) { $this->requestStack->push($request); $this->setupUrlGenerator(); // Call in twice in order to ensure it returns the same the next time. $this->assertEquals($expected_destination, $this->redirectDestination->get()); $this->assertEquals($expected_destination, $this->redirectDestination->get()); } /** * @dataProvider providerGet * * @covers ::getAsArray */ public function testGetAsArray(Request $request, $expected_destination) { $this->requestStack->push($request); $this->setupUrlGenerator(); // Call in twice in order to ensure it returns the same the next time. $this->assertEquals(['destination' => $expected_destination], $this->redirectDestination->getAsArray()); $this->assertEquals(['destination' => $expected_destination], $this->redirectDestination->getAsArray()); } public function providerGet() { $data = []; $request = Request::create('/'); $request->query->set('destination', '/example'); // A request with a destination query. $data[] = [$request, '/example']; // A request without a destination query, $request = Request::create('/'); $data[] = [$request, '/current-path']; // A request without destination query, but other query attributes. $request = Request::create('/'); $request->query->set('other', 'value'); $data[] = [$request, '/current-path?other=value']; return $data; } /** * @covers ::set * @covers ::get */ public function testSetBeforeGetCall() { $this->redirectDestination->set('/example'); $this->assertEquals('/example', $this->redirectDestination->get()); } /** * @covers ::set * @covers ::get */ public function testSetAfterGetCall() { $request = Request::create('/'); $request->query->set('destination', '/other-example'); $this->requestStack->push($request); $this->setupUrlGenerator(); $this->redirectDestination->set('/example'); $this->assertEquals('/example', $this->redirectDestination->get()); } }