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

testRandomByte() public method

Tests the random bytes string generation
Since: 2015-08-17
Author: Serghei Iakovlev ([email protected])
public testRandomByte ( )
    public function testRandomByte()
    {
        $this->specify("bytes does not generate a valid string", function () {
            $lens = [3, 6, 19, 222, 100, 9090909, 12312312];
            $random = new Random();
            $isValid = function ($bytes) {
                return preg_match('#^[^\\x00-\\xFF]+$#', $bytes) === 0;
            };
            foreach ($lens as $len) {
                $actual = $random->bytes($len);
                expect(strlen($actual))->equals($len);
                expect($isValid($actual))->true();
            }
            $actual = $random->bytes();
            expect(strlen($actual))->equals(16);
            expect($isValid($actual))->true();
        });
    }