Skip to content

Commit ba19ec6

Browse files
author
roadiz-ci
committed
Merge branch hotfix/2.7.38
1 parent 6f1c4a4 commit ba19ec6

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
},
1919
"require-dev": {
2020
"phpstan/phpstan": "^2.1.36",
21-
"phpstan/phpdoc-parser": "<2"
21+
"phpstan/phpdoc-parser": "<2",
22+
"phpunit/phpunit": "^9.6",
23+
"symfony/phpunit-bridge": "^7.0"
2224
},
2325
"autoload": {
2426
"psr-4": {
2527
"RZ\\Roadiz\\Random\\": "src/"
2628
}
2729
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"RZ\\Roadiz\\Random\\Tests\\": "tests/"
33+
}
34+
},
2835
"extra": {
2936
"branch-alias": {
3037
"dev-main": "2.7.x-dev",

tests/TokenGeneratorTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RZ\Roadiz\Random\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Log\NullLogger;
9+
use RZ\Roadiz\Random\TokenGenerator;
10+
11+
final class TokenGeneratorTest extends TestCase
12+
{
13+
private function createTokenGenerator(): TokenGenerator
14+
{
15+
return new TokenGenerator(new NullLogger());
16+
}
17+
18+
/**
19+
* generateToken() base64-encodes 32 raw bytes (44 chars with padding) then
20+
* strips the trailing "=" padding, always leaving 43 chars. A regression that
21+
* shortens the byte count (or the base64 step) would change this length.
22+
*/
23+
public function testGenerateTokenLength(): void
24+
{
25+
$token = $this->createTokenGenerator()->generateToken();
26+
27+
self::assertSame(43, \strlen($token));
28+
}
29+
30+
public function testGenerateTokenCharset(): void
31+
{
32+
$token = $this->createTokenGenerator()->generateToken();
33+
34+
self::assertMatchesRegularExpression('/^[A-Za-z0-9_-]+$/', $token);
35+
}
36+
37+
public function testGenerateTokenIsUnique(): void
38+
{
39+
$generator = $this->createTokenGenerator();
40+
$tokens = [];
41+
for ($i = 0; $i < 1000; ++$i) {
42+
$tokens[] = $generator->generateToken();
43+
}
44+
45+
self::assertCount(1000, array_unique($tokens));
46+
}
47+
48+
/**
49+
* getRandomNumber() must return raw openssl_random_pseudo_bytes() output (not a
50+
* time/uniqid-derived string): its length always matches the requested byte count,
51+
* and RandomGenerator throws instead of falling back if OpenSSL reports non-strong randomness.
52+
*/
53+
public function testGetRandomNumberReturnsRequestedByteCount(): void
54+
{
55+
$randomNumber = $this->createTokenGenerator()->getRandomNumber();
56+
57+
self::assertSame(32, \strlen($randomNumber));
58+
}
59+
}

0 commit comments

Comments
 (0)