-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from acelaya-forks/feature/extra-path-mode
Add config option for REDIRECT_EXTRA_PATH_MODE
- Loading branch information
Showing
6 changed files
with
129 additions
and
2 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
45 changes: 45 additions & 0 deletions
45
src/Config/Option/UrlShortener/ExtraPathModeConfigOption.php
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\Installer\Config\Option\UrlShortener; | ||
|
||
use Shlinkio\Shlink\Installer\Config\Option\BaseConfigOption; | ||
use Symfony\Component\Console\Style\StyleInterface; | ||
|
||
class ExtraPathModeConfigOption extends BaseConfigOption | ||
{ | ||
public const MODES = [ | ||
'default' => 'Match strictly', | ||
'append' => 'Append extra path', | ||
'ignore' => 'Discard extra path', | ||
]; | ||
|
||
public function getEnvVar(): string | ||
{ | ||
return 'REDIRECT_EXTRA_PATH_MODE'; | ||
} | ||
|
||
public function ask(StyleInterface $io, array $currentOptions): string | ||
{ | ||
return $io->choice( | ||
question: <<<QUESTION | ||
Do you want Shlink to redirect short URLs as soon as the first segment of the path matches a short code? | ||
append: | ||
* {shortDomain}/{shortCode}/[...extraPath] -> {longUrl}/[...extraPath] | ||
* https://s.test/abc123 -> https://www.example.com | ||
* https://s.test/abc123/shlinkio -> https://www.example.com/shlinkio | ||
ignore: | ||
* {shortDomain}/{shortCode}/[...extraPath] -> {longUrl} | ||
* https://s.test/abc123 -> https://www.example.com | ||
* https://s.test/abc123/shlinkio -> https://www.example.com | ||
QUESTION, | ||
choices: self::MODES, | ||
default: 'default', | ||
); | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
test/Config/Option/UrlShortener/ExtraPathModeConfigOptionTest.php
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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\Installer\Config\Option\UrlShortener; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use Shlinkio\Shlink\Installer\Config\Option\UrlShortener\ExtraPathModeConfigOption; | ||
use Symfony\Component\Console\Style\StyleInterface; | ||
|
||
class ExtraPathModeConfigOptionTest extends TestCase | ||
{ | ||
private ExtraPathModeConfigOption $configOption; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->configOption = new ExtraPathModeConfigOption(); | ||
} | ||
|
||
#[Test] | ||
public function returnsExpectedEnvVar(): void | ||
{ | ||
self::assertEquals('REDIRECT_EXTRA_PATH_MODE', $this->configOption->getEnvVar()); | ||
} | ||
|
||
#[Test, DataProvider('provideChoices')] | ||
public function expectedQuestionIsAsked(string $choice): void | ||
{ | ||
$io = $this->createMock(StyleInterface::class); | ||
$io->expects($this->once())->method('choice')->with( | ||
<<<QUESTION | ||
Do you want Shlink to redirect short URLs as soon as the first segment of the path matches a short code? | ||
append: | ||
* {shortDomain}/{shortCode}/[...extraPath] -> {longUrl}/[...extraPath] | ||
* https://s.test/abc123 -> https://www.example.com | ||
* https://s.test/abc123/shlinkio -> https://www.example.com/shlinkio | ||
ignore: | ||
* {shortDomain}/{shortCode}/[...extraPath] -> {longUrl} | ||
* https://s.test/abc123 -> https://www.example.com | ||
* https://s.test/abc123/shlinkio -> https://www.example.com | ||
QUESTION, | ||
ExtraPathModeConfigOption::MODES, | ||
'default', | ||
)->willReturn($choice); | ||
|
||
$answer = $this->configOption->ask($io, []); | ||
|
||
self::assertEquals($choice, $answer); | ||
} | ||
|
||
public static function provideChoices(): iterable | ||
{ | ||
foreach (ExtraPathModeConfigOption::MODES as $mode => $_) { | ||
yield $mode => [$mode]; | ||
} | ||
} | ||
} |