Skip to content

Commit

Permalink
UrlScript: added withPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 13, 2019
1 parent e31735c commit d0dd0bd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Http/UrlScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public function __construct($url = '/', string $scriptPath = '')
}


/**
* @return static
*/
public function withPath(string $path, string $scriptPath = '')
{
$dolly = clone $this;
$dolly->scriptPath = $scriptPath;
return call_user_func([$dolly, 'parent::withPath'], $path);
}


public function getScriptPath(): string
{
return $this->scriptPath;
Expand Down
30 changes: 30 additions & 0 deletions tests/Http/UrlScript.manipulation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Nette\Http\UrlScript;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test(function () {
$url = new UrlScript('http://nette.org:8080/test/?q=search', '/test/index.php');
Assert::same('/test/', $url->basePath);
Assert::same('/test/index.php', $url->scriptPath);

$url = $url->withPath('/index.php');
Assert::same('/', $url->basePath);
Assert::same('/index.php', $url->scriptPath);

$url = $url->withPath('/test/', '/test/index.php');
Assert::same('/test/', $url->basePath);
Assert::same('/test/index.php', $url->scriptPath);
});


Assert::exception(function () {
$url = new UrlScript('http://nette.org:8080/test/?q=search', '/test/index.php');
$url->withPath('/test/', '/test/index/');
}, Nette\InvalidArgumentException::class);

0 comments on commit d0dd0bd

Please sign in to comment.