Skip to content

Commit

Permalink
Merge d2545a1 into 453641d
Browse files Browse the repository at this point in the history
  • Loading branch information
SenseException committed Dec 17, 2015
2 parents 453641d + d2545a1 commit d80e742
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -14,7 +14,8 @@
"php": ">=5.3.9",
"puli/repository": "1.0.*",
"puli/discovery": "^1.0.0-beta8",
"webmozart/glob": ">=2.0,<4.0"
"webmozart/glob": ">=2.0,<4.0",
"webmozart/path-util": "^2.3"
},
"require-dev": {
"phpunit/phpunit": "^4.6",
Expand Down
11 changes: 10 additions & 1 deletion src/DiscoveryUrlGenerator.php
Expand Up @@ -11,11 +11,13 @@

namespace Puli\UrlGenerator;

use InvalidArgumentException;
use Puli\Discovery\Api\Discovery;
use Puli\Discovery\Binding\ResourceBinding;
use Puli\UrlGenerator\Api\CannotGenerateUrlException;
use Puli\UrlGenerator\Api\UrlGenerator;
use Webmozart\Glob\Glob;
use Webmozart\PathUtil\Url;

/**
* A resource URL generator that uses a {@link ResourceDiscovery} as backend.
Expand Down Expand Up @@ -92,7 +94,14 @@ public function generateUrl($repositoryPath, $currentUrl = null)
$url = $this->generateUrlForBinding($matchedBinding, $repositoryPath);

if ($currentUrl) {
// TODO use Url::makeRelative() once it exists
try {
$url = Url::makeRelative($url, $currentUrl);
} catch (InvalidArgumentException $e) {
throw new CannotGenerateUrlException(sprintf(
'Cannot generate URL for "%s" to current url "%s".',
$repositoryPath, $currentUrl
), $e->getCode(), $e);
}
}

return $url;
Expand Down
46 changes: 46 additions & 0 deletions tests/DiscoveryUrlGeneratorTest.php
Expand Up @@ -221,4 +221,50 @@ public function testFailIfServerNotFound()

$this->generator->generateUrl('/path/path/style.css');
}

/**
* @covers \Puli\UrlGenerator\DiscoveryUrlGenerator::generateUrl
*/
public function testGenerateUrlRelativeUrl()
{
$binding = new ResourceBinding(
'/path{,/**/*}',
DiscoveryUrlGenerator::BINDING_TYPE,
array(
DiscoveryUrlGenerator::SERVER_PARAMETER => 'localhost',
DiscoveryUrlGenerator::PATH_PARAMETER => '/css',
)
);

$this->discovery->expects($this->once())
->method('findBindings')
->with(DiscoveryUrlGenerator::BINDING_TYPE)
->willReturn(array($binding));

$this->assertSame('path/style.css', $this->generator->generateUrl('/path/path/style.css', 'http://example.com/css'));
}

/**
* @expectedException \Puli\UrlGenerator\Api\CannotGenerateUrlException
* @expectedExceptionMessage Cannot generate URL for "/path/path/style.css" to current url "/".
* @covers \Puli\UrlGenerator\DiscoveryUrlGenerator::generateUrl
*/
public function testMakeRelativeFails()
{
$binding = new ResourceBinding(
'/path{,/**/*}',
DiscoveryUrlGenerator::BINDING_TYPE,
array(
DiscoveryUrlGenerator::SERVER_PARAMETER => 'localhost',
DiscoveryUrlGenerator::PATH_PARAMETER => '/css',
)
);

$this->discovery->expects($this->once())
->method('findBindings')
->with(DiscoveryUrlGenerator::BINDING_TYPE)
->willReturn(array($binding));

$this->generator->generateUrl('/path/path/style.css', '/');
}
}

0 comments on commit d80e742

Please sign in to comment.