Skip to content

Commit

Permalink
Merge branch 'release/12.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
astehlik committed May 12, 2023
2 parents f54c286 + 740bf8b commit 39b7a55
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
22 changes: 15 additions & 7 deletions Classes/UrlKeyGenerator/Base62UrlKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
use Tx\Tinyurls\Utils\GeneralUtilityWrapper;

/**
* Contains utilities for creating tiny url keys and url hashes.
* Generates a key for a tinyurl using a configured dictionary.
*
* The dictionary is used as a base for encoding an integer (the UID of the tinyurl recordd)
* into a string.
*
* Opionally, a minimum length for the generated key can be configured. When the key generated from
* the dictionary is shorter than the configured minimum length, a random string is appended to the
* original key, separated by a dash.
*/
class Base62UrlKeyGenerator implements UrlKeyGenerator
{
Expand Down Expand Up @@ -70,20 +77,21 @@ public function generateTinyurlKeyForUid(int $uid): string
* Thanks to http://jeremygibbs.com/2012/01/16/how-to-make-a-url-shortener
*
* @param int $base10Integer The integer that will converted
* @param string $base62Dictionary the dictionary for generating the base62 integer
* @param string $baseXDictionary the dictionary for generating the baseX integer
*
* @return string A base62 encoded integer using a custom dictionary
*/
protected function convertIntToBase62(int $base10Integer, string $base62Dictionary): string
protected function convertIntToBase62(int $base10Integer, string $baseXDictionary): string
{
$base62Integer = '';
$base = 62;
$baseXInteger = '';
$base = mb_strlen($baseXDictionary);

do {
$base62Integer = $base62Dictionary[$base10Integer % $base] . $base62Integer;
$dictionaryOffset = $base10Integer % $base;
$baseXInteger = mb_substr($baseXDictionary, $dictionaryOffset, 1) . $baseXInteger;
$base10Integer = floor($base10Integer / $base);
} while ($base10Integer > 0);

return $base62Integer;
return $baseXInteger;
}
}
2 changes: 1 addition & 1 deletion Documentation/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ project = URL shortener / TinyURLs
# ... (recommended) version, displayed next to title (desktop) and in <meta name="book-version"
# .................................................................................

release = 12.0.0
release = 12.1.0

# .................................................................................
# ... (recommended) displayed in footer
Expand Down
45 changes: 27 additions & 18 deletions Tests/Unit/UrlKeyGenerator/Base62UrlKeyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,11 @@
*/
class Base62UrlKeyGeneratorTest extends TestCase
{
/**
* @var Base62UrlKeyGenerator
*/
protected $base62UrlKeyGenerator;
protected Base62UrlKeyGenerator $base62UrlKeyGenerator;

/**
* @var ExtensionConfiguration|MockObject
*/
protected $extensionConfigurationMock;
protected ExtensionConfiguration|MockObject $extensionConfigurationMock;

/**
* @var GeneralUtilityWrapper|MockObject
*/
protected $generalUtilityMock;
protected GeneralUtilityWrapper|MockObject $generalUtilityMock;

protected function setUp(): void
{
Expand All @@ -52,7 +43,7 @@ protected function setUp(): void
);
}

public function testGenerateTinyurlKeyForUidEncodesIntegerIfNoMinimalLengthIsConfigured(): void
public function testGenerateTinyurlKeyForTinyUrlCreatesExpectedKey(): void
{
$this->extensionConfigurationMock->method('getBase62Dictionary')
->willReturn('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
Expand All @@ -63,6 +54,17 @@ public function testGenerateTinyurlKeyForUidEncodesIntegerIfNoMinimalLengthIsCon
self::assertSame('ud', $key);
}

public function testGenerateTinyurlKeyForUidEncodesIntegerIfNoMinimalLengthIsConfigured(): void
{
$this->extensionConfigurationMock->method('getBase62Dictionary')
->willReturn('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');

$tinyUrl = TinyUrl::createNew();
$tinyUrl->persistPostProcessInsert(1243);
$key = $this->base62UrlKeyGenerator->generateTinyurlKeyForUid(1243);
self::assertSame('ud', $key);
}

public function testGenerateTinyurlKeyForUidFillsUpKeyUpToConfiguredMinimalLength(): void
{
$this->extensionConfigurationMock->method('getBase62Dictionary')
Expand All @@ -75,9 +77,7 @@ public function testGenerateTinyurlKeyForUidFillsUpKeyUpToConfiguredMinimalLengt
->with(2)
->willReturn('ag');

$tinyUrl = TinyUrl::createNew();
$tinyUrl->persistPostProcessInsert(1243);
$key = $this->base62UrlKeyGenerator->generateTinyurlKeyForTinyUrl($tinyUrl);
$key = $this->base62UrlKeyGenerator->generateTinyurlKeyForUid(1243);
self::assertSame('ud-ag', $key);
}

Expand All @@ -96,9 +96,18 @@ public function testGenerateTinyurlKeyForUidFillsUpKeyWithConfiguredMinimalRando
->with(2)
->willReturn('ag');

$key = $this->base62UrlKeyGenerator->generateTinyurlKeyForUid(1243);
self::assertSame('ud-ag', $key);
}

public function testGenerateTinyurlKeyForUidWorksWithShorterDictionary(): void
{
$this->extensionConfigurationMock->method('getBase62Dictionary')
->willReturn('abcä');

$tinyUrl = TinyUrl::createNew();
$tinyUrl->persistPostProcessInsert(1243);
$key = $this->base62UrlKeyGenerator->generateTinyurlKeyForTinyUrl($tinyUrl);
self::assertSame('ud-ag', $key);
$key = $this->base62UrlKeyGenerator->generateTinyurlKeyForUid(1243);
self::assertSame('baäbcä', $key);
}
}
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'author' => 'Alexander Stehlik',
'author_email' => '[email protected]',
'author_company' => '',
'version' => '12.0.0',
'version' => '12.1.0',
'constraints' => [
'depends' => [
'php' => '8.1.0-8.2.99',
Expand Down

0 comments on commit 39b7a55

Please sign in to comment.