-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow decorating payload url with placeholders (#3)
- Loading branch information
Showing
8 changed files
with
135 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace WebhooksManager\UrlDecorator\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use WebhooksManager\UrlDecorator\UrlDecorator; | ||
|
||
class UrlDecoratorTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider dataProvider | ||
*/ | ||
public function testDecorateUrlWithReplacesSpecifiersWithArguments($url, $arguments, $expected) | ||
{ | ||
$urlDecorator = new UrlDecorator(); | ||
$actual = $urlDecorator->decorateUrlWith($url, $arguments); | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function dataProvider() | ||
{ | ||
return [ | ||
['https://example.com/$1', [1], 'https://example.com/1'], | ||
['https://example.com/$1', ['foo'], 'https://example.com/foo'], | ||
['https://example.com/$1/$2', [1, 'foo'], 'https://example.com/1/foo'] | ||
]; | ||
} | ||
|
||
public function testDecorateUrlWithReturnsUrlWhenArgumentsIsEmptyArray() | ||
{ | ||
$urlDecorator = new UrlDecorator(); | ||
$result = $urlDecorator->decorateUrlWith('https://example.com', []); | ||
|
||
$this->assertEquals('https://example.com', $result); | ||
} | ||
|
||
public function testDecorateUrlWithReturnsUrlWhenArgumentsAreNull() | ||
{ | ||
$urlDecorator = new UrlDecorator(); | ||
$result = $urlDecorator->decorateUrlWith('https://example.com', null); | ||
|
||
$this->assertEquals('https://example.com', $result); | ||
} | ||
|
||
// Test that arguments that are not strings or numbers are not included in the URL. | ||
public function testDecorateUrlWithDoesNotIncludeArgumentsThatAreNotStringsOrNumbers() | ||
{ | ||
$urlDecorator = new UrlDecorator(); | ||
$result = $urlDecorator->decorateUrlWith('https://example.com/$1/$2', [1, 'foo', ['bar']]); | ||
|
||
$this->assertEquals('https://example.com/1/foo', $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace WebhooksManager\UrlDecorator; | ||
|
||
class UrlDecorator implements UrlDecoratorInterface | ||
{ | ||
/** | ||
* Decorates the given URL with the given arguments. | ||
* | ||
* @param string $url The URL to be decorated. | ||
* Should be formatted with sprintf specifiers. https://www.php.net/manual/en/function.sprintf.php | ||
* @param mixed $arguments The arguments to be included in the URL. | ||
* @return string The decorated URL. | ||
*/ | ||
public function decorateUrlWith(string $url, ?array $arguments): string | ||
{ | ||
if (empty($arguments)) { | ||
return $url; | ||
} | ||
|
||
foreach ($arguments as $key => $value) { | ||
if (!is_string($value) && !is_numeric($value)) { | ||
continue; | ||
} | ||
|
||
$url = str_replace('$' . $key + 1, urlencode($value), $url); | ||
} | ||
|
||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace WebhooksManager\UrlDecorator; | ||
|
||
interface UrlDecoratorInterface | ||
{ | ||
/** | ||
* Decorates the given URL with the given arguments. | ||
* | ||
* @param string $url The URL to be decorated. | ||
* @param mixed $arguments The arguments to be included in the URL. | ||
* @return string The decorated URL. | ||
*/ | ||
public function decorateUrlWith(string $url, ?array $arguments): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters