Skip to content

Commit

Permalink
add test coverage for fullUrlFor
Browse files Browse the repository at this point in the history
  • Loading branch information
l0gicgate committed Apr 20, 2019
1 parent 352556f commit e0346e6
Showing 1 changed file with 45 additions and 14 deletions.
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

0 comments on commit e0346e6

Please sign in to comment.