Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure every generated code is unique #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/urlshortener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

return [

// Multiplier for pseudo-random codes
'multiplier' => 6824219,

// The characters used to generate an unique URL
'characterset' => env('URLSHORTENER_CHARACTERSET', "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"),

Expand Down
30 changes: 24 additions & 6 deletions src/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,42 @@ function ($model) {
);
}

public static function generateCode($url)
public static function encode($num, $multiplier, $alphabet, $length_min)
{
if ($num >= pow(strlen($alphabet), $length_min)) {
$length_min = ceil(log($num, strlen($alphabet)));
}

$result = '';

$num = $num * $multiplier % pow(strlen($alphabet), $length_min);
do {
$result = $alphabet[$num % strlen($alphabet)] . $result;
$num = intval($num / strlen($alphabet));
} while ($num);

$result = str_pad($result, $length_min, $alphabet[0], STR_PAD_LEFT);

return $result;
}

public static function generateCode($url)
{
$code = "";

$characters = \str_split(config("urlshortener.characterset"));
$characters = config("urlshortener.characterset");
$length = config("urlshortener.length_min");
$multiplier = config("urlshortener.multiplier");

for ($i = 0; $i < $length; $i++) {
$code .= $characters[\random_int(0, \count($characters) - 1)];
}
$number = URL::count() + 1;

$code = self::encode($number, $multiplier, $characters, $length);

return $code;
}

public function __toString()
{

return (string) route('urlshortener.redirect', ['code' => $this->code]);
}
}
17 changes: 13 additions & 4 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
<?php

namespace ArieTimmerman\Laravel\OAuth2\Tests;
namespace ArieTimmerman\Laravel\URLShortener\Tests;

use Orchestra\Testbench\TestCase;
use ArieTimmerman\Laravel\URLShortener\URL;

class BasicTest extends TestCase
{
private static $lengthMin = 4;
private static $characterset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';

protected function setUp(): void
{
parent::setUp();

// $this->runDatabaseMigrations();
$this->artisan('migrate', ['--database' => 'testbench'])->run();
}

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app ['config']->set('urlshortener.characterset', self::$characterset);
$app ['config']->set('urlshortener.length_min', self::$lengthMin);
}

public function testUrlCode()
{
for ($i = 0;$i<10000;$i++) {
for ($i = 0; $i<10000; $i++) {
$code = URL::generateCode("http://www.example.com");

//Check if the length is correct
Expand Down
7 changes: 4 additions & 3 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

namespace ArieTimmerman\Laravel\OAuth2\Tests;
namespace ArieTimmerman\Laravel\URLShortener\Tests;

use Orchestra\Testbench\TestCase;
use ArieTimmerman\Laravel\URLShortener\URL;
use ArieTimmerman\Laravel\URLShortener\URLShortener;
use Illuminate\Support\Facades\Route;

Expand All @@ -12,7 +10,10 @@ class RouteTest extends TestCase

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
$app ['config']->set('urlshortener.route_resource_enabled', false);
$app ['config']->set('urlshortener.url_prefix_code', '');

}

public function testRoutesRegistered()
Expand Down
41 changes: 41 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace ArieTimmerman\Laravel\URLShortener\Tests;

use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class TestCase extends OrchestraTestCase
{
use RefreshDatabase;
use DatabaseMigrations;

protected function setUp(): void
{
parent::setUp();

$this->runDatabaseMigrations();
}

protected function getEnvironmentSetUp($app)
{
$app ['config']->set('app.url', 'http://localhost');

$app['config']->set('app.key', 'base64:1234mRasdLA123F0JiF02Og3bLXbk5qPE8H3+vX2O5M=');

$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

protected function getPackageProviders($app)
{
return [
\ArieTimmerman\Laravel\URLShortener\ServiceProvider::class,
];
}
}
30 changes: 30 additions & 0 deletions tests/UniqueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ArieTimmerman\Laravel\URLShortener\Tests;

use ArieTimmerman\Laravel\URLShortener\URLShortener;

class UniqueTest extends TestCase
{
private static $lengthMin = 2;
private static $characterset = 'AB';

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app ['config']->set('urlshortener.characterset', self::$characterset);
$app ['config']->set('urlshortener.length_min', self::$lengthMin);
}

public function testUrlCode()
{
$all = [];
for ($i = 0; $i<12; $i++) {
$code = (string)URLShortener::shorten("http://www.example.com/" . $i);
$all[] = $code;
}

$this->assertEquals(12, count(array_unique($all)));
}
}