*/ class TokenGeneratorTest extends TestCase { use TokenGenerator; /** * Test the token generation */ public function testGenerate() { $token = $this->generate(40); $this->assertEquals(40, strlen($token), 'The token has the expected length'); $this->assertRegExp('/^[0-9a-f]{40}$/', $token, 'The token is correctly formatted'); $token = $this->generate(60); $this->assertEquals(60, strlen($token), 'The token has the expected length'); $this->assertRegExp('/^[0-9a-f]{60}$/', $token, 'The token is correctly formatted'); } /** * Micro collisions tests */ public function testCollide() { $tokens = []; for ($i = 0; $i < 10000; $i++) { $tokens[] = $this->generate(); } //if 2 values are identical the distribution size will be lower than the number of tokens $distribution = array_count_values($tokens); $this->assertCount(count($tokens), $distribution, 'The tokens are uniques'); } }