Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x - Port RouteCollector::fullUrlFor() #2638

Merged
merged 2 commits into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Slim/Interfaces/RouteCollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Slim\Interfaces;

use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use RuntimeException;

interface RouteCollectorInterface
Expand Down Expand Up @@ -144,6 +145,8 @@ public function relativePathFor(string $name, array $data = [], array $queryPara
/**
* Build the path for a named route including the base path
*
* This method is deprecated. Use urlFor() from now on.
*
* @param string $name Route name
* @param array $data Named argument replacement data
* @param array $queryParams Optional query string parameters
Expand All @@ -156,9 +159,7 @@ public function relativePathFor(string $name, array $data = [], array $queryPara
public function pathFor(string $name, array $data = [], array $queryParams = []): string;

/**
* Build the path for a named route.
*
* This method is deprecated. Use pathFor() from now on.
* Build the path for a named route including the base path
*
* @param string $name Route name
* @param array $data Named argument replacement data
Expand All @@ -170,4 +171,16 @@ public function pathFor(string $name, array $data = [], array $queryParams = [])
* @throws InvalidArgumentException If required data not provided
*/
public function urlFor(string $name, array $data = [], array $queryParams = []): string;

/**
* Get fully qualified URL for named route
*
* @param UriInterface $uri
* @param string $routeName Route name
* @param array $data Named argument replacement data
* @param array $queryParams Optional query string parameters
*
* @return string
*/
public function fullUrlFor(UriInterface $uri, string $routeName, array $data = [], array $queryParams = []): string;
}
19 changes: 16 additions & 3 deletions Slim/Routing/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\UriInterface;
use RuntimeException;
use Slim\Handlers\Strategies\RequestResponse;
use Slim\Interfaces\CallableResolverInterface;
Expand Down Expand Up @@ -376,6 +377,15 @@ public function relativePathFor(string $name, array $data = [], array $queryPara
* {@inheritdoc}
*/
public function pathFor(string $name, array $data = [], array $queryParams = []): string
{
trigger_error('pathFor() is deprecated. Use urlFor() instead.', E_USER_DEPRECATED);
return $this->urlFor($name, $data, $queryParams);
}

/**
* {@inheritdoc}
*/
public function urlFor(string $name, array $data = [], array $queryParams = []): string
{
$url = $this->relativePathFor($name, $data, $queryParams);

Expand All @@ -389,9 +399,12 @@ public function pathFor(string $name, array $data = [], array $queryParams = [])
/**
* {@inheritdoc}
*/
public function urlFor(string $name, array $data = [], array $queryParams = []): string
public function fullUrlFor(UriInterface $uri, string $routeName, array $data = [], array $queryParams = []): string
{
trigger_error('urlFor() is deprecated. Use pathFor() instead.', E_USER_DEPRECATED);
return $this->pathFor($name, $data, $queryParams);
$path = $this->urlFor($routeName, $data, $queryParams);
$scheme = $uri->getScheme();
$authority = $uri->getAuthority();
$protocol = ($scheme ? $scheme . ':' : '') . ($authority ? '//' . $authority : '');
return $protocol . $path;
}
}
59 changes: 45 additions & 14 deletions tests/Routing/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Slim\Tests\Routing;

use Psr\Http\Message\UriInterface;
use RuntimeException;
use Slim\CallableResolver;
use Slim\Interfaces\RouteInterface;
Expand Down Expand Up @@ -116,7 +117,7 @@ public function testPathForWithNoBasePath()

$this->assertEquals(
'/hello/josh/lockhart',
$this->routeCollector->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
$this->routeCollector->urlFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
);
}

Expand All @@ -133,7 +134,7 @@ public function testPathForWithBasePath()

$this->assertEquals(
'/base/path/hello/josh/lockhart',
$this->routeCollector->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
$this->routeCollector->urlFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
);
}

Expand All @@ -149,15 +150,15 @@ public function testPathForWithOptionalParameters()

$this->assertEquals(
'/archive/2015',
$this->routeCollector->pathFor('foo', ['year' => '2015'])
$this->routeCollector->urlFor('foo', ['year' => '2015'])
);
$this->assertEquals(
'/archive/2015/07',
$this->routeCollector->pathFor('foo', ['year' => '2015', 'month' => '07'])
$this->routeCollector->urlFor('foo', ['year' => '2015', 'month' => '07'])
);
$this->assertEquals(
'/archive/2015/07/d/19',
$this->routeCollector->pathFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19'])
$this->routeCollector->urlFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19'])
);
}

Expand All @@ -173,7 +174,7 @@ public function testPathForWithQueryParameters()

$this->assertEquals(
'/hello/josh?a=b&c=d',
$this->routeCollector->pathFor('foo', ['name' => 'josh'], ['a' => 'b', 'c' => 'd'])
$this->routeCollector->urlFor('foo', ['name' => 'josh'], ['a' => 'b', 'c' => 'd'])
);
}

Expand All @@ -190,7 +191,7 @@ public function testPathForWithMissingSegmentData()
$route = $this->routeCollector->map($methods, $pattern, $callable);
$route->setName('foo');

$this->routeCollector->pathFor('foo', ['last' => 'lockhart']);
$this->routeCollector->urlFor('foo', ['last' => 'lockhart']);
}

/**
Expand All @@ -206,7 +207,7 @@ public function testPathForRouteNotExists()
$route = $this->routeCollector->map($methods, $pattern, $callable);
$route->setName('foo');

$this->routeCollector->pathFor('bar', ['first' => 'josh', 'last' => 'lockhart']);
$this->routeCollector->urlFor('bar', ['first' => 'josh', 'last' => 'lockhart']);
}

public function testGetRouteInvocationStrategy()
Expand Down Expand Up @@ -344,10 +345,10 @@ public function testCacheFileDoesNotExistsAndDirectoryIsNotWritable()
}

/**
* Test that the router urlFor will proxy into a pathFor method, and trigger
* Test that the router pathFor will proxy into a urlFor method, and trigger
* the user deprecated warning
*/
public function testUrlForAliasesPathFor()
public function testPathForAliasesUrlFor()
{
//create a temporary error handler, store the error str in this value
$errorString = null;
Expand All @@ -365,17 +366,47 @@ public function testUrlForAliasesPathFor()
$routeCollector = $this
->getMockBuilder(RouteCollector::class)
->setConstructorArgs([$this->getResponseFactory(), new CallableResolver()])
->setMethods(['pathFor'])
->setMethods(['urlFor'])
->getMock();
$routeCollector->expects($this->once())->method('pathFor')->with($name, $data, $queryParams);
$routeCollector->urlFor($name, $data, $queryParams);
$routeCollector->expects($this->once())->method('urlFor')->with($name, $data, $queryParams);
$routeCollector->pathFor($name, $data, $queryParams);

//check that our error was triggered
$this->assertEquals($errorString, 'urlFor() is deprecated. Use pathFor() instead.');
$this->assertEquals($errorString, 'pathFor() is deprecated. Use urlFor() instead.');

restore_error_handler();
}

public function testFullUrlFor()
{
$uriProphecy = $this->prophesize(UriInterface::class);
$uriProphecy
->getScheme()
->willReturn('http')
->shouldBeCalledOnce();

$uriProphecy
->getAuthority()
->willReturn('example.com:8080')
->shouldBeCalledOnce();

$callableResolver = new CallableResolver();
$responseFactory = $this->getResponseFactory();
$routeCollector = new RouteCollector($responseFactory, $callableResolver);
$routeCollector->setBasePath('/app');

$callable = function ($request, $response) {
return $response;
};
$route = $routeCollector->map(['GET'], '/{token}', $callable);
$route->setName('test');

$expectedResult = 'http://example.com:8080/app/123';
$result = $routeCollector->fullUrlFor($uriProphecy->reveal(), 'test', ['token' => '123']);

$this->assertEquals($expectedResult, $result);
}

/**
* @expectedException \RuntimeException
*/
Expand Down