Phalcon\Test\Unit\Security\RandomTest::testRandomHex PHP Method

testRandomHex() public method

Tests the random hex generation
Since: 2015-08-17
Author: Serghei Iakovlev ([email protected])
public testRandomHex ( )
    public function testRandomHex()
    {
        $this->specify("hex does not generate a valid string", function () {
            $lens = [3, 6, 130, 19, 710, 943087];
            $random = new Random();
            $checkSize = function ($hex, $len) {
                return strlen($hex) == $len * 2;
            };
            $isValid = function ($hex) {
                return preg_match("#^[^0-9a-f]+\$#i", $hex) === 0;
            };
            foreach ($lens as $len) {
                $actual = $random->hex($len);
                expect($checkSize($actual, $len))->true();
                expect($isValid($actual))->true();
            }
            $actual = $random->hex();
            expect($checkSize($actual, 16))->true();
            expect($isValid($actual))->true();
        });
    }