PHP Classes

File: tests/ShortIdTest.php

Recommend this page to a friend!
  Packages of A. B. M. Mahmudul Hasan   UID (UUID, ULID, Snowflake, Sonyflake, Nano, CUID, KSUID, XID, TBSL)   tests/ShortIdTest.php   Download  
File: tests/ShortIdTest.php
Role: Example script
Content type: text/plain
Description: Example script
Class: UID (UUID, ULID, Snowflake, Sonyflake, Nano, CUID, KSUID, XID, TBSL)
Generate, parse, validate unique identifiers
Author: By
Last change:
Date: 8 days ago
Size: 1,424 bytes
 

Contents

Class file image Download
<?php

use Infocyph\UID\Contracts\IdAlgorithmInterface;
use
Infocyph\UID\CUID2;
use
Infocyph\UID\NanoID;

test('CUID2', function () {
   
$string = CUID2::generate();
   
expect($string)
        ->
toBeString()
        ->
not()->toBeEmpty()
        ->
toHaveLength(24)
        ->
toMatch('/^[0-9a-z]+$/');
});

test('CUID2 custom length', function () {
   
$string = CUID2::generate(32);
   
expect($string)->toHaveLength(32)->toMatch('/^[0-9a-z]+$/');
});

test('nanoId', function () {
   
$string = NanoID::generate();
   
expect($string)->toBeString()->not()->toBeEmpty()->toHaveLength(21);
});

test('global helper functions for NanoID and CUID2', function () {
   
expect(nanoid(10))->toHaveLength(10)
        ->
and(cuid2(24))->toHaveLength(24);
});

test('NanoID and CUID2 validation and parse', function () {
   
$nano = NanoID::generate(12);
   
$cuid = CUID2::generate(24);

   
$nanoParsed = NanoID::parse($nano, 12);
   
$cuidParsed = CUID2::parse($cuid);

   
expect(NanoID::isValid($nano, 12))->toBeTrue()
        ->
and($nanoParsed['isValid'])->toBeTrue()
        ->
and($nanoParsed['length'])->toBe(12)
        ->
and($nanoParsed['alphabet'])->toBe('base64url')
        ->
and(CUID2::isValid($cuid))->toBeTrue()
        ->
and($cuidParsed['isValid'])->toBeTrue()
        ->
and($cuidParsed['length'])->toBe(24)
        ->
and(nanoid_is_valid($nano, 12))->toBeTrue()
        ->
and(cuid2_is_valid($cuid))->toBeTrue();
});