Skip to content

Commit

Permalink
Merge pull request #21 from PUGX/exception
Browse files Browse the repository at this point in the history
✨ add exception
  • Loading branch information
garak authored Oct 2, 2022
2 parents 80c54d9 + e32c7a8 commit 9c37066
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
"require": {
"php": "^7.1 || ^8.0",
"ext-json": "*",
"paragonie/random-lib": "^2.0",
"symfony/polyfill-mbstring": "^1.19"
},
Expand Down
16 changes: 10 additions & 6 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ final class Factory
*/
private static $factory;

/**
* @throws InvalidShortidException
*/
public function generate(int $length = null, string $alphabet = null, bool $readable = null): Shortid
{
$length = null === $length ? $this->length : $length;
$readable = null === $readable ? $this->readable : $readable;
$length = $length ?? $this->length;
$readable = $readable ?? $this->readable;
if (null === $alphabet && $readable) {
$alphabet = Generator::EASY_TO_READ;
$alphabet = \str_replace(\str_split(Generator::AMBIGUOUS_CHARS), '', $this->alphabet);
$alphabet .= \str_repeat('_', \strlen(Generator::AMBIGUOUS_CHARS) / 2);
$alphabet .= \str_repeat('-', \strlen(Generator::AMBIGUOUS_CHARS) / 2);
}
$alphabet = null === $alphabet ? $this->alphabet : $alphabet;
$id = self::getFactory()->getMediumStrengthGenerator()->generateString($length, $alphabet);
$id = self::getFactory()->getMediumStrengthGenerator()->generateString($length, $alphabet ?? $this->alphabet);

return new Shortid($id);
return new Shortid($id, $length, $alphabet);
}

public function setAlphabet(string $alphabet): void
Expand Down
7 changes: 7 additions & 0 deletions src/InvalidShortidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PUGX\Shortid;

final class InvalidShortidException extends \Exception
{
}
12 changes: 11 additions & 1 deletion src/Shortid.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ final class Shortid implements \JsonSerializable, \Serializable
*/
private static $factory;

public function __construct(string $id)
/**
* @throws InvalidShortidException
*/
public function __construct(string $id, int $length = null, string $alphabet = null)
{
if (!self::isValid($id, $length, $alphabet)) {
throw new InvalidShortidException(\sprintf('Invalid shortid %s (length %d alphabet %s', $id, $length, $alphabet));
}

$this->id = $id;
}

Expand All @@ -24,6 +31,9 @@ public function __toString(): string
return $this->id;
}

/**
* @throws InvalidShortidException
*/
public static function generate(int $length = null, string $alphabet = null, bool $readable = false): self
{
if (null === $length) {
Expand Down
12 changes: 6 additions & 6 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testGenerate(): void
{
$generated = $this->factory->generate();

$this->assertRegExp('/^[a-z0-9\_\-]{7,7}$/i', $generated->__toString());
self::assertRegExp('/^[a-z0-9\_\-]{7,7}$/i', $generated->__toString());
}

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ public function testSetAlphabet(string $alphabet): void

$newAlphabet = $this->factory->getAlphabet();

$this->assertSame($alphabet, $newAlphabet);
self::assertSame($alphabet, $newAlphabet);
}

/**
Expand All @@ -77,27 +77,27 @@ public function wrongAlphabetsProvider(): array
{
return [
'test' => ['test'],
'rand' => [\sha1((string) \random_int(0, \getrandmax()))],
'rand' => [\sha1((string) \random_int(0, \mt_getrandmax()))],
];
}

public function testGetFactory(): void
{
$factory = Factory::getFactory();

$this->assertInstanceOf(RandomLibFactory::class, $factory);
self::assertInstanceOf(RandomLibFactory::class, $factory);
}

public function testSetLength(): void
{
$this->factory->setLength(5);
$this->assertSame(5, $this->factory->getLength());
self::assertSame(5, $this->factory->getLength());
}

public function testCheckLength(): void
{
$null = $this->factory->checkLength(null, false);
$this->assertNull($null);
self::assertNull($null);
}

public function testSetWrongLengthType(): void
Expand Down
37 changes: 22 additions & 15 deletions tests/ShortidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use JsonSerializable;
use PHPUnit\Framework\TestCase;
use PUGX\Shortid\Factory;
use PUGX\Shortid\InvalidShortidException;
use PUGX\Shortid\Shortid;

final class ShortidTest extends TestCase
Expand All @@ -18,47 +19,47 @@ public function testGenerate(): void
{
$generated = Shortid::generate();

$this->assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
self::assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
}

public function testGenerateWithReadable(): void
{
$generated = Shortid::generate(null, null, true);

$this->assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
self::assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
}

public function testGenerateWithLength(): void
{
$generated = Shortid::generate(8);

$this->assertRegExp('/^[a-z0-9\_\-]{8}$/i', $generated->__toString());
self::assertRegExp('/^[a-z0-9\_\-]{8}$/i', $generated->__toString());
}

public function testGetFactory(): void
{
$factory = Shortid::getFactory();

$this->assertInstanceOf(Factory::class, $factory);
self::assertInstanceOf(Factory::class, $factory);
}

public function testSetFactory(): void
{
$factory = new Factory();
Shortid::setFactory($factory);

$this->assertSame($factory, Shortid::getFactory());
self::assertSame($factory, Shortid::getFactory());
}

public function testIsValid(): void
{
$this->assertTrue(Shortid::isValid('shortid'));
self::assertTrue(Shortid::isValid('shortid'));
}

public function testIsNotValid(): void
{
$this->assertFalse(Shortid::isValid('/(;#!'));
$this->assertFalse(Shortid::isValid('harmful string stuff'));
self::assertFalse(Shortid::isValid('/(;#!'));
self::assertFalse(Shortid::isValid('harmful string stuff'));
}

public function testIsValidWithRegexChar(): void
Expand All @@ -67,50 +68,56 @@ public function testIsValidWithRegexChar(): void
$factory->setAlphabet('hìjklmnòpqrstùvwxyzABCDEFGHIJKLMNOPQRSTUVWX.\+*?[^]$(){}=!<>|:-/');
Shortid::setFactory($factory);

$this->assertTrue(Shortid::isValid('slsh/]?'));
self::assertTrue(Shortid::isValid('slsh/]?'));
}

public function testJsonSerializable(): void
{
$generated = Shortid::generate();

$this->assertInstanceOf(JsonSerializable::class, $generated);
self::assertInstanceOf(JsonSerializable::class, $generated);
}

public function testJsonEncode(): void
{
$generated = Shortid::generate();

$this->assertSame('"'.$generated.'"', \json_encode($generated));
self::assertSame('"'.$generated.'"', \json_encode($generated));
}

public function testSerialize(): void
{
$shortid = new Shortid('shortid');

$this->assertSame('shortid', $shortid->serialize());
self::assertSame('shortid', $shortid->serialize());
}

public function testUnserialize(): void
{
$shortid = Shortid::generate();
$shortid->unserialize('shortid');

$this->assertSame('shortid', (string) $shortid);
self::assertSame('shortid', (string) $shortid);
}

public function testMagicSerialize(): void
{
$shortid = new Shortid('shortid');

$this->assertSame(['id' => 'shortid'], $shortid->__serialize());
self::assertSame(['id' => 'shortid'], $shortid->__serialize());
}

public function testMagicUnserialize(): void
{
$shortid = Shortid::generate();
$shortid->__unserialize(['id' => 'shortid']);

$this->assertSame('shortid', (string) $shortid);
self::assertSame('shortid', (string) $shortid);
}

public function testInvalidArgumentInConstructor(): void
{
$this->expectException(InvalidShortidException::class);
new Shortid('an_invalid_too_long_shortid');
}
}

0 comments on commit 9c37066

Please sign in to comment.