mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-10 21:51:44 +00:00
91 - Integrating Hashids
This commit is contained in:
18
app/HashidsTicketCodeGenerator.php
Normal file
18
app/HashidsTicketCodeGenerator.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
class HashidsTicketCodeGenerator implements TicketCodeGenerator
|
||||||
|
{
|
||||||
|
private $hashids;
|
||||||
|
|
||||||
|
public function __construct($salt)
|
||||||
|
{
|
||||||
|
$this->hashids = new \Hashids\Hashids($salt, 6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateFor($ticket)
|
||||||
|
{
|
||||||
|
return $this->hashids->encode($ticket->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,5 +4,5 @@ namespace App;
|
|||||||
|
|
||||||
interface TicketCodeGenerator
|
interface TicketCodeGenerator
|
||||||
{
|
{
|
||||||
public function generate();
|
public function generateFor($ticket);
|
||||||
}
|
}
|
||||||
|
|||||||
64
tests/unit/HashidsTicketCodeGeneratorTest.php
Normal file
64
tests/unit/HashidsTicketCodeGeneratorTest.php
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Ticket;
|
||||||
|
use App\HashidsTicketCodeGenerator;
|
||||||
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
|
||||||
|
class HashidsTicketCodeGeneratorTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @test */
|
||||||
|
function ticket_codes_are_at_least_6_characters_long()
|
||||||
|
{
|
||||||
|
$ticketCodeGenerator = new HashidsTicketCodeGenerator('testsalt1');
|
||||||
|
|
||||||
|
$code = $ticketCodeGenerator->generateFor(new Ticket(['id' => 1]));
|
||||||
|
|
||||||
|
$this->assertTrue(strlen($code) >= 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function ticket_codes_can_only_contain_uppercase_letters()
|
||||||
|
{
|
||||||
|
$ticketCodeGenerator = new HashidsTicketCodeGenerator('testsalt1');
|
||||||
|
|
||||||
|
$code = $ticketCodeGenerator->generateFor(new Ticket(['id' => 1]));
|
||||||
|
|
||||||
|
$this->assertRegExp('/^[A-Z]+$/', $code);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function ticket_codes_for_the_same_ticket_id_are_the_same()
|
||||||
|
{
|
||||||
|
$ticketCodeGenerator = new HashidsTicketCodeGenerator('testsalt1');
|
||||||
|
|
||||||
|
$code1 = $ticketCodeGenerator->generateFor(new Ticket(['id' => 1]));
|
||||||
|
$code2 = $ticketCodeGenerator->generateFor(new Ticket(['id' => 1]));
|
||||||
|
|
||||||
|
$this->assertEquals($code1, $code2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function ticket_codes_for_different_ticket_ids_are_different()
|
||||||
|
{
|
||||||
|
$ticketCodeGenerator = new HashidsTicketCodeGenerator('testsalt1');
|
||||||
|
|
||||||
|
$code1 = $ticketCodeGenerator->generateFor(new Ticket(['id' => 1]));
|
||||||
|
$code2 = $ticketCodeGenerator->generateFor(new Ticket(['id' => 2]));
|
||||||
|
|
||||||
|
$this->assertNotEquals($code1, $code2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function ticket_codes_generated_with_different_salts_are_different()
|
||||||
|
{
|
||||||
|
$ticketCodeGenerator1 = new HashidsTicketCodeGenerator('testsalt1');
|
||||||
|
$ticketCodeGenerator2 = new HashidsTicketCodeGenerator('testsalt2');
|
||||||
|
|
||||||
|
$code1 = $ticketCodeGenerator1->generateFor(new Ticket(['id' => 1]));
|
||||||
|
$code2 = $ticketCodeGenerator2->generateFor(new Ticket(['id' => 1]));
|
||||||
|
|
||||||
|
$this->assertNotEquals($code1, $code2);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user