SMMP/spec/build/api/core/CheckerSpec.php

303 lines
7.6 KiB
PHP
Raw Normal View History

<?php
use \api\core\Checker;
describe('api', function(){
describe('core', function(){
describe('Checker', function(){
/* (1) Unknown type
---------------------------------------------------------*/
context('* unknown type', function(){
it('do not pass unknown type with any value', function(){
expect(Checker::run('unknown_type', 'someString' ))->toBeFalsy();
expect(Checker::run('unknown_type', 123 ))->toBeFalsy();
expect(Checker::run('unknown_type', [1, 2] ))->toBeFalsy();
expect(Checker::run('unknown_type', true ))->toBeFalsy();
expect(Checker::run('unknown_type', false ))->toBeFalsy();
expect(Checker::run('unknown_type', new StdClass() ))->toBeFalsy();
expect(Checker::run('unknown_type', null ))->toBeFalsy();
});
});
/* (2) Mixed
---------------------------------------------------------*/
context('* mixed', function(){
it('pass type="mixed" with any type', function(){
expect(Checker::run('mixed', 'someString' ))->toBeTruthy();
expect(Checker::run('mixed', 123 ))->toBeTruthy();
expect(Checker::run('mixed', [1, 2] ))->toBeTruthy();
expect(Checker::run('mixed', true ))->toBeTruthy();
expect(Checker::run('mixed', false ))->toBeTruthy();
expect(Checker::run('mixed', new StdClass() ))->toBeTruthy();
});
it('do not pass type="mixed" with null', function(){
expect(Checker::run('mixed', null))->toBeFalsy();
});
});
/* (3) id
---------------------------------------------------------*/
context('* id', function(){
it('pass type="id" whith any integer number (string or int)', function(){
expect(Checker::run('id', '123' ))->toBeTruthy();
expect(Checker::run('id', 123 ))->toBeTruthy();
});
it('do not pass type="id" whith decimal value (string or int)', function(){
expect(Checker::run('id', '1.23' ))->toBeFalsy();
expect(Checker::run('id', 1.23 ))->toBeFalsy();
});
it('pass type="id" whith boundaries', function(){
expect(Checker::run('id', 0 ))->toBeTruthy();
expect(Checker::run('id', 2147483647 ))->toBeTruthy();
});
it('do not pass type="id" whith not match out-of-boundaries', function(){
expect(Checker::run('id', 0-1 ))->toBeFalsy();
expect(Checker::run('id', 2147483647+1 ))->toBeFalsy();
});
});
/* (4) text
---------------------------------------------------------*/
context('* text', function(){
it('pass type="text" with any string', function(){
expect(Checker::run('text', '123' ))->toBeTruthy();
expect(Checker::run('text', '123'.'123' ))->toBeTruthy();
});
it('pass type="text" with empty string', function(){
$empty_str = '';
expect($empty_str)->toHaveLength(0);
expect(Checker::run('text', $empty_str))->toBeTruthy();
});
it('do not pass type="text" when not string', function(){
expect(Checker::run('text', [] ))->toBeFalsy();
expect(Checker::run('text', 1 ))->toBeFalsy();
expect(Checker::run('text', 1.23 ))->toBeFalsy();
expect(Checker::run('text', true ))->toBeFalsy();
expect(Checker::run('text', false ))->toBeFalsy();
expect(Checker::run('text', new StdClass() ))->toBeFalsy();
});
});
/* (5) Rfid
---------------------------------------------------------*/
context('* hash', function(){
given('sizes', function(){ return [
40 => 'sha1',
64 => 'sha256',
128 => 'sha512'
];
});
it('do not pass when -40/64/128 char hash', function(){
foreach($this->sizes as $size=>$sha){
$hash = hash($sha, 'someClearText');
$hash = substr($hash, 1); // remove 1 char
expect($hash)->toHaveLength($size-1);
expect(Checker::run('hash', $hash))->toBeFalsy();
}
});
it('pass when 40/64/128 char hash', function(){
foreach($this->sizes as $size=>$sha){
$hash = hash($sha, 'someClearText');
expect($hash)->toHaveLength($size);
expect(Checker::run('hash', $hash))->toBeTruthy();
}
});
it('do not pass when +40/64/128 char hash', function(){
foreach($this->sizes as $size=>$sha){
$hash = hash($sha, 'someClearText');
$hash = $hash.'a'; // add character
expect($hash)->toHaveLength($size+1);
expect(Checker::run('hash', $hash))->toBeFalsy();
}
});
it('do not pass when incorrect character', function(){
$hash = hash('sha256', 'someClearText');
$hash[1] = 'g';
expect($hash)->not->toMatch('/^[a-f0-9]+$/');
expect(Checker::run('hash', $hash))->toBeFalsy();
});
});
/* (6) mail
---------------------------------------------------------*/
context('* mail', function(){
it('do not pass type="mail" with out-of-boundaries elements match', function(){
expect(Checker::run('mail', '@b.cd'))->toBeFalsy();
expect(Checker::run('mail', 'a@.cd'))->toBeFalsy();
expect(Checker::run('mail', 'a@b.d'))->toBeFalsy();
expect(Checker::run('mail', 'a@b.cdefg'))->toBeFalsy();
});
it('pass type="mail" with max length', function(){
$mail = str_repeat('a', 45).'@b.cd';
expect($mail)->toHaveLength(50);
expect(Checker::run('mail', $mail))->toBeTruthy();
});
it('do not pass type="mail" with more than max length', function(){
$mail = str_repeat('a', 46).'@b.cd';
expect($mail)->toHaveLength(51);
expect(Checker::run('mail', $mail))->toBeFalsy();
});
it('pass type="mail" with minimum match', function(){
expect(Checker::run('mail', 'a@b.cd'))->toBeTruthy();
expect(Checker::run('mail', 'a@b.cdef'))->toBeTruthy();
});
});
/* (7) alphanumeric
---------------------------------------------------------*/
context('* alphanumeric', function(){
it('pass when : letters/numbers/./-', function(){
expect(Checker::run('alphanumeric', 'abcz0129-.'))->toBeTruthy();
});
it('pass when unicode', function(){
expect(Checker::run('alphanumeric', 'çèééàôò'))->toBeTruthy();
});
it('do not pass when not alphanumeric (space/,)', function(){
expect(Checker::run('alphanumeric', 'abcz0129-. '))->toBeFalsy();
expect(Checker::run('alphanumeric', 'abcz0129-.,'))->toBeFalsy();
});
it('do not pass when empty', function(){
$str = '';
expect($str)->toBeEmpty();
expect(Checker::run('alphanumeric', $str))->toBeFalsy();
});
});
/* (8) letters
---------------------------------------------------------*/
context('* letters', function(){
it('pass when letters (letters/space/-)', function(){
expect(Checker::run('letters', 'abcz- - -a'))->toBeTruthy();
});
it('do not pass when unicode', function(){
expect(Checker::run('letters', 'çèééàôò'))->toBeFalsy();
});
it('do not pass when not letters (./,/numbers)', function(){
expect(Checker::run('letters', 'abcz- - -a0'))->toBeFalsy();
expect(Checker::run('letters', 'abcz- - -a9'))->toBeFalsy();
expect(Checker::run('letters', 'abcz- - -a,'))->toBeFalsy();
expect(Checker::run('letters', 'abcz- - -a.'))->toBeFalsy();
});
it('do not pass when empty', function(){
$str = '';
expect($str)->toBeEmpty();
expect(Checker::run('letters', $str))->toBeFalsy();
});
});
});
});
});