Skip to content

Commit 756a788

Browse files
Added Tests to mirror .NET port
1 parent aceaa86 commit 756a788

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/BaseTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
require_once __DIR__.'/../vendor/autoload.php';
4+
require_once __DIR__.'/../src/ulid.php';
5+
6+
use lewiscowles\core\Ulid;
7+
8+
use PHPUnit\Framework\TestCase;
9+
10+
class BaseTest extends TestCase
11+
{
12+
13+
const TIME = 1469918176385;
14+
15+
public function setup()
16+
{
17+
$this->ulid = new Ulid();
18+
}
19+
20+
public function testRandIsBetween1and0()
21+
{
22+
$rand = $this->invokeMethod($this->ulid, 'getRand');
23+
$this->assertTrue( $rand > 0 && $rand < 1 );
24+
}
25+
26+
public function testEncodeTimeShouldReturnExpectedEncodedResult()
27+
{
28+
$hash = $this->invokeMethod($this->ulid, 'encodeTime', [self::TIME, 10]);
29+
$this->assertEquals("01ARYZ6S41", $hash);
30+
}
31+
32+
public function testEncodeTimeShouldChangeLengthProperly()
33+
{
34+
$hash = $this->invokeMethod($this->ulid, 'encodeTime', [self::TIME, 12]);
35+
$this->assertEquals("0001ARYZ6S41", $hash);
36+
}
37+
38+
public function testEncodeTimeShouldTruncateTimeIfNotLongEnough()
39+
{
40+
$hash = $this->invokeMethod($this->ulid, 'encodeTime', [self::TIME, 8]);
41+
$this->assertEquals("ARYZ6S41", $hash);
42+
}
43+
44+
public function testEncodeRandomShouldReturnCorrectLength()
45+
{
46+
$length = 12;
47+
$hash = $this->invokeMethod($this->ulid, 'encodeRandom', [$length]);
48+
$this->assertEquals($length, strlen($hash));
49+
}
50+
51+
public function testNewUlidShouldReturnCorrectLength()
52+
{
53+
$hash = $this->ulid->get();
54+
$this->assertEquals(26, strlen($hash));
55+
}
56+
57+
protected function invokeMethod(&$object, $methodName, array $parameters = array())
58+
{
59+
$reflection = new \ReflectionClass(get_class($object));
60+
$method = $reflection->getMethod($methodName);
61+
$method->setAccessible(true);
62+
return $method->invokeArgs($object, $parameters);
63+
}
64+
}

0 commit comments

Comments
 (0)