/inc/main.php
PHP | 2355 lines | 2163 code | 187 blank | 5 comment | 39 complexity | 951744b0fb36fa26b71c04b85b345ece MD5 | raw file
Possible License(s): GPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- class Main extends F3instance {
- function hotlink() {
- $this->set('HOTLINK','/error');
- file_put_contents('f3hotlink.tmp',md5('f3hotlink'));
- $this->clear('ROUTES');
- $this->route('GET /',
- function() {
- echo 'This should never be executed';
- },0,5000,FALSE
- );
- $this->route('GET /error',array($this,'ehandler'));
- $this->mock('GET /');
- $_SERVER['HTTP_REFERER']='http://www.yahoo.com/search/';
- $this->run();
- }
- function ehandler() {
- $this->set('title','Error Handling');
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $this->expect(
- file_exists('f3hotlink.tmp') &&
- file_get_contents('f3hotlink.tmp')==md5('f3hotlink'),
- 'Hotlink test succeeded',
- 'Hotlink test failed - you shouldn\'t reload this page'
- );
- @unlink('f3hotlink.tmp');
- $this->set('QUIET',TRUE);
- $this->status(69);
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===500,
- $this->get('ERROR.text'),
- 'No error detected: '.var_export($this->get('ERROR'),TRUE)
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->set('a*x',123);
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===500,
- $this->get('ERROR.text'),
- 'No error detected: '.var_export($this->get('ERROR'),TRUE)
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $static='Akismet|AtomRSS|Auth|Data|Geo|Google|Graphics|ICU|Net|'.
- 'Template|Twitter|UTF|Web|XML|Yahoo';
- foreach (explode('|',$static) as $class) {
- $this->set('QUIET',TRUE);
- new $class;
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===500,
- $this->get('ERROR.text'),
- $class.' class instantiated'
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- }
- $dynamic='DB|FileDB|Log|M2|Zip';
- foreach (array_merge(explode('|',$static),explode('|',$dynamic))
- as $class) {
- $this->set('QUIET',TRUE);
- $method=$this->hash(mt_rand(0,getrandmax()));
- $class::$method();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===500,
- $this->get('ERROR.text'),
- 'No error detected: '.var_export($this->get('ERROR'),TRUE)
- );
- if (in_array($class,explode('|',$dynamic))) {
- try {
- $z=new $class('abc');
- $z->$method();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===500,
- $this->get('ERROR.text'),
- 'No error detected: '.var_export($this->get('ERROR'),TRUE)
- );
- // Remove file created by FileDB|Log class (side-effect)
- @rmdir('abc');
- @unlink('abc');
- }
- catch(exception $x) {
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===500,
- $this->get('ERROR.text'),
- 'No error detected: '.var_export($this->get('ERROR'),TRUE)
- );
- }
- }
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- }
- echo $this->render('basic/results.htm');
- }
- function globals() {
- $this->set('title','Globals');
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- foreach (explode('|',F3::PHP_Globals) as $php) {
- $this->set($php.'.x',3.14);
- $this->set($php.'.y',0.15);
- $this->expect(
- $this->get($php)==$GLOBALS['_'.$php],
- $php.' matches $_'.$php,
- $php.' does not match $_'.$php.': '.
- var_export($this->get($php),TRUE).' != '.
- var_export($GLOBALS['_'.$php],TRUE)
- );
- }
- $this->set('POST.xyz',567);
- $this->expect(
- $_POST['xyz']===567 &&
- $this->get('POST.xyz')===567 && $this->get('POST["xyz"]')===567,
- 'F3-mirrored PHP variable also alters underlying variable',
- 'Underlying variable unchanged: '.
- var_export($_POST['xyz'],TRUE)
- );
- $this->set('POST["xyz"]',567);
- $this->expect(
- $_POST['xyz']===567 &&
- $this->get('POST.xyz')===567 && $this->get('POST["xyz"]')===567,
- '$this->set() variant also alters underlying variable',
- 'Underlying variable unchanged: '.
- var_export($_POST['xyz'],TRUE)
- );
- $this->set('POST.abc.def',999);
- $this->expect(
- $_POST['abc']['def']===999 &&
- $this->get('POST.abc.def')===999 &&
- $this->get('POST["abc"]["def"]')===999,
- 'Multi-level array variable mirrored properly',
- 'Variable mirroring issue: '.
- var_export($_POST['abc']['def'],TRUE)
- );
- $_POST['xyz']=789;
- $this->expect(
- $this->get('POST.xyz')===789 && $this->get('POST["xyz"]')===789,
- 'Changing a PHP global also alters F3 equivalent',
- 'No change in F3-mirrored PHP variable: '.
- var_export($this->get('POST.xyz'),TRUE)
- );
- $_POST['xyz']=234;
- $this->expect(
- $this->get('POST.xyz')===234 && $this->get('POST["xyz"]')===234,
- 'PHP global in sync with F3 equivalent',
- 'PHP global not in sync with F3 equivalent: '.
- var_export($this->get('POST.xyz'),TRUE)
- );
- $this->clear('POST');
- $this->expect(
- !isset($_POST) && !$this->exists('POST'),
- 'Clearing F3 variable also clears PHP global',
- 'PHP global not cleared: '.
- var_export($this->get('POST'),TRUE)
- );
- $this->expect(
- strlen(session_id()),
- 'Session auto-started',
- 'Session was not auto-started: '.var_export($_SESSION,TRUE)
- );
- $this->clear('SESSION.x');
- $this->expect(
- !$this->exists('SESSION.x') && !isset($_SESSION['x']),
- 'Session variable cleared',
- 'Session variable not cleared: '.var_export($_SESSION,TRUE)
- );
- $this->clear('SESSION');
- $this->expect(
- !session_id(),
- 'Session destroyed',
- 'Session was not destroyed: '.var_export($_SESSION,TRUE)
- );
- echo $this->render('basic/results.htm');
- }
- function f3vars() {
- $this->set('title','F3 Variables');
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $base=$this->get('BASE');
- $this->set('BASE','abc/123');
- $this->expect(
- $this->get('BASE')==$base,
- 'Altering a read-only framework variable has no effect',
- 'Value of read-only framework variable changed: '.$this->get('BASE')
- );
- $this->expect(
- $this->get('title')=='F3 Variables',
- 'String assigned to userland variable',
- 'Incorrect value/data type: '.
- var_export($this->get('title'),TRUE).'/'.
- gettype($this->get('title'))
- );
- $this->expect(
- $this->get('title')=='F3 Variables' && is_string($this->get('title')),
- 'String value preserved',
- 'Incorrect value/data type: '.
- var_export($this->get('title'),TRUE).'/'.
- gettype($this->get('title'))
- );
- $this->expect(
- $this->get('title')==F3::get('title') && is_string(F3::get('title')),
- 'f3::get() and $this->get() return the same value',
- 'f3::get() and $this->get() behave differently! '.
- var_export($this->get('title'),TRUE).'/'.
- var_export(F3::get('title'),TRUE)
- );
- $this->set('i',123);
- $this->expect(
- $this->get('i')===123,
- 'Integer assigned',
- 'Incorrect value/data type: '.
- var_export($this->get('i'),TRUE).'/'.gettype($this->get('i'))
- );
- $this->expect(
- $this->get('i')===123,
- 'Integer value preserved',
- 'Incorrect value/data type: '.
- var_export($this->get('i'),TRUE).'/'.gettype($this->get('i'))
- );
- $this->set('f',345.6);
- $this->expect(
- $this->get('f')===345.6,
- 'Float assigned',
- 'Incorrect value/data type: '.
- var_export($this->get('f'),TRUE).'/'.gettype($this->get('f'))
- );
- $this->expect(
- $this->get('f')===345.6,
- 'Float value preserved',
- 'Incorrect value/data type: '.
- var_export($this->get('f'),TRUE).'/'.gettype($this->get('f'))
- );
- $this->set('e',1.23e-4);
- $this->expect(
- $this->get('e')===1.23e-4,
- 'Negative exponential float assigned',
- 'Incorrect value/data type: '.
- var_export($this->get('e'),TRUE).'/'.gettype($this->get('e'))
- );
- $this->set('e',1.23e+4);
- $this->expect(
- $this->get('e')===1.23e+4,
- 'Positive exponential float value preserved',
- 'Incorrect value/data type: '.
- var_export($this->get('e'),TRUE).'/'.gettype($this->get('e'))
- );
- $this->set('e',1.23e4);
- $this->expect(
- $this->get('e')===1.23e4,
- 'Unsigned exponential float value preserved',
- 'Incorrect value/data type: '.
- var_export($this->get('e'),TRUE).'/'.gettype($this->get('e'))
- );
- $this->set('b',TRUE);
- $this->expect(
- $this->get('b')===TRUE,
- 'Boolean value preserved',
- 'Incorrect value/data type: '.
- var_export($this->get('b'),TRUE).'/'.gettype($this->get('b'))
- );
- $this->set('a',array(1,'inner',3.5));
- $this->expect(
- is_array($this->get('a')) && $this->get('a')==array(1,'inner',3.5),
- 'Array preserved',
- 'Incorrect value/data type: '.
- var_export($this->get('a'),TRUE).'/'.gettype($this->get('a'))
- );
- $this->push('a','after');
- $this->expect(
- $this->get('a')==array(1,'inner',3.5,'after'),
- 'Array push() works',
- 'Array push() failed'
- );
- $this->expect(
- $this->pop('a')=='after' && $this->get('a')==array(1,'inner',3.5),
- 'Array pop() works',
- 'Array pop() failed'
- );
- $this->unshift('a','before');
- $this->expect(
- $this->get('a')==array('before',1,'inner',3.5),
- 'Array unshift() works',
- 'Array unshift() failed'
- );
- $this->expect(
- $this->shift('a')=='before' && $this->get('a')==array(1,'inner',3.5),
- 'Array shift() works',
- 'Array shift() failed'
- );
- $this->flip('a');
- $this->expect(
- $this->get('a')==array(1=>0,'inner'=>1,'3.5'=>2),
- 'Array flip() works',
- 'Array flip() failed: '.var_export($this->get('a'),TRUE)
- );
- $this->expect(
- is_null($this->get('hello')),
- 'Non-existent variable returns NULL',
- 'Non-existent variable failure: '.var_export($this->get('hello'),TRUE)
- );
- $this->set('obj',new Obj);
- $this->expect(
- $this->get('obj')==new Obj && is_object($this->get('obj')),
- 'Object preserved',
- 'Incorrect value/data type: '.
- var_export($this->get('obj'),TRUE).'/'.gettype($this->get('obj'))
- );
- $this->expect(
- $this->exists('i'),
- 'Existence confirmed',
- 'Variable does not exist: '.var_export($this->get('i'),TRUE)
- );
- $this->clear('i');
- $this->expect(
- !$this->exists('i'),
- 'Clear confirmed',
- 'Variable not cleared: '.var_export($this->exists('i'),TRUE)
- );
- $this->set('v[0]',123);
- $this->expect(
- $this->exists('v'),
- 'Array instantiated when element is assigned a value',
- 'Variable does not exist: '.var_export($this->get('v'),TRUE)
- );
- $this->expect(
- $this->get('v')==array(123),
- 'Array constructed properly',
- 'Array not constructed properly: '.var_export($this->get('v'),TRUE)
- );
- $this->set('v.1',456);
- $this->expect(
- $this->get('v')==array(123,456),
- 'Value assigned using dot-notation',
- 'Value not assigned: '.var_export($this->get('v'),TRUE)
- );
- $this->clear('v[1]');
- $this->expect(
- $this->get('v')==array(123),
- 'Element cleared using regular notation',
- 'Value not cleared: '.var_export($this->get('v'),TRUE)
- );
- $this->set('v.2',789);
- $this->clear('v.2');
- $this->expect(
- $this->get('v')==array(123),
- 'Element cleared using dot-notation',
- 'Value not cleared: '.var_export($this->get('v'),TRUE)
- );
- $this->clear('v');
- $this->expect(
- !$this->exists('v'),
- 'Clear confirmed',
- 'Array not cleared: '.var_export($this->get('v'),TRUE)
- );
- $this->set('a',369);
- $this->set('b','a');
- $this->set('{{@b}}',246);
- $this->expect(
- $this->get('a')===246,
- 'Variable variable assigned',
- 'Variable variable assignment error: '.var_export($this->get('a'),TRUE)
- );
- $this->set('a',357);
- $this->expect(
- $this->get('{{@b}}')===357,
- 'Variable variable retrieved',
- 'Variable variable retrieval error: '.var_export($this->get('{{@b}}'),TRUE)
- );
- $this->set('QUIET',TRUE);
- $this->expect(
- is_null($this->get('{{@b.1}}')) && !is_null($this->get('ERROR')),
- 'Incorrect variable variable usage',
- 'Variable variable usage error: '.var_export($this->get('{{@b.1}}'),TRUE)
- );
- $this->set('QUIET',FALSE);
- $this->set('a',array());
- $x=&$this->ref('a');
- $x[]=123;
- $x[]=234;
- $x[]=345;
- $this->set('QUIET',TRUE);
- $this->expect(
- $this->get('a')==array(123,234,345) && !is_null($this->get('ERROR')),
- 'Variable references handled properly',
- 'Variable references incorrect: '.var_export($this->get('a'),TRUE)
- );
- $this->set('QUIET',FALSE);
- $this->set('a',1);
- $this->set('b',2);
- $this->set('c',array('{{@a}}',array('{{@b}}')));
- $this->expect(
- $this->get('c')==array(1,array(2)),
- 'Deeply-nested tokens in framework array variable replaced',
- 'Deeply-nested tokens not replaced: '.var_export($this->get('c'),TRUE)
- );
- $this->set('str','hello');
- $this->concat('str',' world');
- $this->expect(
- $this->get('str')=='hello world',
- 'String concatenation works',
- 'String concatenation failed'
- );
- echo $this->render('basic/results.htm');
- }
- function matrix() {
- $this->set('title','Matrix');
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $z=array(
- array('id'=>123,'name'=>'paul','sales'=>0.35),
- array('id'=>456,'name'=>'ringo','sales'=>0.13),
- array('id'=>345,'name'=>'george','sales'=>0.57),
- array('id'=>234,'name'=>'john','sales'=>0.79)
- );
- Matrix::sort($z,'name');
- $this->expect(
- array_values($z)==array(
- array('id'=>345,'name'=>'george','sales'=>0.57),
- array('id'=>234,'name'=>'john','sales'=>0.79),
- array('id'=>123,'name'=>'paul','sales'=>0.35),
- array('id'=>456,'name'=>'ringo','sales'=>0.13),
- ),
- 'Sorting a multi-dimensional array by string column works properly',
- 'Incorrect array sort algorithm: '.var_export($z,TRUE)
- );
- Matrix::sort($z,'id');
- $this->expect(
- array_values($z)==array(
- array('id'=>123,'name'=>'paul','sales'=>0.35),
- array('id'=>234,'name'=>'john','sales'=>0.79),
- array('id'=>345,'name'=>'george','sales'=>0.57),
- array('id'=>456,'name'=>'ringo','sales'=>0.13)
- ),
- 'Sorting a multi-dimensional array by integer column works properly',
- 'Incorrect array sort algorithm: '.var_export($z,TRUE)
- );
- Matrix::sort($z,'sales');
- $this->expect(
- array_values($z)==array(
- array('id'=>456,'name'=>'ringo','sales'=>0.13),
- array('id'=>123,'name'=>'paul','sales'=>0.35),
- array('id'=>345,'name'=>'george','sales'=>0.57),
- array('id'=>234,'name'=>'john','sales'=>0.79)
- ),
- 'Sorting a multi-dimensional array by float column works properly',
- 'Incorrect array sort algorithm: '.var_export($z,TRUE)
- );
- echo $this->render('basic/results.htm');
- }
- function configure() {
- $this->set('title','Configuration');
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $this->clear('ROUTES');
- include 'inc/config.inc.php';
- $this->config('inc/config.ini');
- $this->expect(
- $this->get('num')==123,
- 'Integer variable found',
- 'Missing integer variable'
- );
- $this->expect(
- $this->get('str')=='abc',
- 'String variable found',
- 'Missing string variable'
- );
- $this->expect(
- $this->get('hash')==array('x'=>1,'y'=>2,'z'=>3),
- 'Hash variable found',
- 'Missing hash variable'
- );
- $this->expect(
- $this->get('list')==array(7,8,9),
- 'List variable found',
- 'Missing list variable'
- );
- $this->expect(
- $this->get('mix')==array("this",123.45,FALSE),
- 'Mixed array variable found',
- 'Missing mixed array variable'
- );
- $this->set('QUIET',TRUE);
- $this->mock('GET /');
- $this->run();
- $this->expect(
- is_null($this->get('ERROR')),
- $this->get('SERVER.REQUEST_METHOD').' '.$this->get('PARAMS.0').' exists',
- 'Routing/configuration error: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->mock('GET /404');
- $this->run();
- $this->expect(
- is_null($this->get('ERROR')),
- $this->get('SERVER.REQUEST_METHOD').' '.$this->get('PARAMS.0').' exists',
- 'Routing/configuration error: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->mock('GET /inside/multi');
- $this->run();
- $this->expect(
- is_null($this->get('ERROR')),
- $this->get('SERVER.REQUEST_METHOD').' '.$this->get('PARAMS.0').' exists',
- 'Routing/configuration error: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->mock('GET /noroute');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')),
- $this->get('ERROR.text'),
- 'Routing/configuration error: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->mock('GET /map');
- $this->run();
- $this->expect(
- is_null($this->get('ERROR')),
- $this->get('SERVER.REQUEST_METHOD').' '.$this->get('PARAMS.0').' exists',
- 'Routing/configuration error: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->mock('POST /map');
- $this->run();
- $this->expect(
- is_null($this->get('ERROR')),
- $this->get('SERVER.REQUEST_METHOD').' '.$this->get('PARAMS.0').' exists',
- 'Routing/configuration error: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->mock('DELETE /map');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===405,
- 'DELETE /map triggered an HTTP 405',
- 'Routing/configuration error: '.$this->get('ERROR.code')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->mock('PUT /map');
- $this->run();
- $this->expect(
- is_null($this->get('ERROR')),
- $this->get('SERVER.REQUEST_METHOD').' '.$this->get('PARAMS.0').' exists',
- 'Routing/configuration error: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- echo $this->render('basic/results.htm');
- }
- function redirect() {
- file_put_contents('f3routing.tmp',md5('f3routing'));
- $this->reroute('/routing');
- }
- function routing() {
- $this->set('title','Routing');
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $this->expect(
- file_exists('f3routing.tmp') &&
- file_get_contents('f3routing.tmp')==md5('f3routing'),
- 'Rerouting succeeded',
- 'Rerouting did not work as expected - you shouldn\'t reload this page'
- );
- @unlink('f3routing.tmp');
- $this->set('QUIET',TRUE);
- $this->clear('ROUTES');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===500,
- 'HTTP 500 expected - '.$this->get('ERROR.text'),
- 'No error detected: '.var_export($this->get('ERROR'),TRUE)
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->route('GRAB /','test');
- $this->mock('GET /');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===405,
- $this->get('ERROR.text'),
- 'No HTTP 405 triggered: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->clear('ROUTES');
- $this->set('IMPORTS','inc/');
- file_put_contents('inc/temp.php',
- '<?php '.
- 'F3::set(\'temp\',\'inside\');'
- );
- $this->route('GET /','temp.php');
- $this->mock('GET /');
- $this->run();
- $this->expect(
- $this->get('temp')=='inside',
- 'Import file loaded',
- 'Import file failure'
- );
- @unlink('inc/temp.php');
- $this->clear('ROUTES');
- $this->set('QUIET',TRUE);
- $this->route('GET /',
- function() {}
- );
- $this->mock('GET /test/noroute');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===404,
- 'HTTP 404 expected - non-existent route',
- 'No HTTP 404 triggered'
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->clear('ROUTES');
- $this->set('QUIET',TRUE);
- $this->route('POST /','nonexistent');
- $this->mock('POST /');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===404,
- 'HTTP 404 expected - non-existent function',
- 'No HTTP 404 triggered: '.$this->get('ERROR.text')
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->set('QUIET',TRUE);
- $this->error(404);
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===404,
- 'Programmatically-triggered HTTP 404',
- 'No HTTP 404 triggered'
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $anon=TRUE;
- $this->set('anon',TRUE);
- $this->mock('GET /solo');
- $self=$this;
- $this->route('GET /solo',
- function() use($anon) {
- $anon=FALSE;
- F3::set('anon',FALSE);
- }
- );
- $this->run();
- $this->expect(
- $anon && $this->get('anon')===FALSE,
- 'Routed to anonymous function',
- 'Issue with routing to anonymous function'
- );
- $this->clear('ROUTES');
- $this->clear('ERROR');
- function dummy1() {
- F3::set('routed',1);
- F3::set('x','i');
- }
- function dummy2() {
- F3::set('routed',2);
- F3::set('y','am');
- }
- function dummy3() {
- F3::set('routed',3);
- F3::set('z','fine');
- }
- function dummy4() {
- F3::set('routed',4);
- }
- $this->route('GET /a','dummy1');
- $this->route('GET /a/b/c','dummy2');
- $this->route('GET|POST /a-b/c','dummy3');
- $this->route('POST /a/b','dummy4');
- $this->set('QUIET',TRUE);
- $this->mock('POST /a');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')),
- 'No handler for mock route - triggered error',
- 'Route handling issue'
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->route('GET /hoohah','dummy1|dummy2|dummy3');
- $this->mock('GET /hoohah');
- $this->run();
- $this->expect(
- $this->get('x')=='i' && $this->get('y')=='am' && $this->get('z')=='fine',
- 'Route handler containing chained functions executed',
- 'Problem with chained functions'
- );
- $this->set('routed',0);
- $this->mock('GET /a');
- $this->run();
- $this->expect(
- $this->get('routed')===1,
- 'Non slash-terminated URI routed properly',
- 'Slash-terminated URI routing issue'
- );
- $this->set('routed',0);
- $this->mock('GET /a/b/c');
- $this->run();
- $this->expect(
- $this->get('routed')===2,
- 'Non slash-terminated URI (deep nesting) routed properly',
- 'Slash-terminated URI (deep nesting) routing issue'
- );
- $this->set('routed',0);
- $this->mock('GET /a-b/c');
- $this->run();
- $this->expect(
- $this->get('routed')===3,
- 'Slash-terminated URI (deep nesting) routed properly',
- 'Slash-terminated URI (deep nesting) routing issue'
- );
- $this->set('routed',0);
- $this->mock('GET /a-b/c?x=557&y=355');
- $this->run();
- $this->expect(
- $this->get('routed')===3,
- 'URI (with special characters and GET variables) routed properly',
- 'URI (with special characters and GET variables) routing issue'
- );
- $this->expect(
- $this->get('GET')==array('x'=>'557','y'=>'355'),
- 'GET variables passed to framework-mirrored PHP variable',
- 'Issue with GET variables in URI: '.var_export($this->get('GET'),TRUE)
- );
- $this->set('routed',0);
- $this->mock('POST /a-b/c?x=557&y=355');
- $this->run();
- $this->expect(
- $this->get('routed')===3,
- 'Route with combined GET|POST executed correctly',
- 'Handling of route with combined GET|POST is faulty'
- );
- $this->set('routed',0);
- $this->mock('DELETE /a-b/c?x=557&y=355');
- $this->set('QUIET',TRUE);
- $this->run();
- $this->set('QUIET',FALSE);
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')==405,
- 'No matching route for DELETE request method',
- 'DELETE request method handled incorrectly'
- );
- $this->set('routed',0);
- $this->mock('POST /a/b?x=224&y=466');
- $this->run();
- $this->expect(
- $this->get('routed')===4,
- 'POST route handler called',
- 'Routing issue with POST method'
- );
- $this->set('routed',0);
- $this->mock('POST /a/b?x=224&y=466');
- $this->run();
- $this->expect(
- $this->get('POST')===array('x'=>'224','y'=>'466'),
- 'POST variables passed to framework-mirrored PHP variable',
- 'Issue with POST variables in URI: '.var_export($this->get('POST'),TRUE)
- );
- $this->clear('ROUTES');
- $this->clear('ERROR');
- $this->route('GET /a','dummy1');
- $this->route('GET /a/@token','dummy2');
- $this->route('GET /old-adage/a/@token1/@token2/@token3','dummy3');
- $this->set('routed',0);
- $this->mock('GET /a/x');
- $this->run();
- $this->expect(
- $this->get('routed')===2,
- 'Framework treats root URI and tokenized URI differently',
- 'Routing problem with tokens'
- );
- $this->set('QUIET',TRUE);
- $this->set('routed',0);
- $this->mock('GET /a/rose/is/a/rose');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===404,
- 'Expected a 404 error - no route handler for specified URI',
- 'HTTP 404 not triggered'
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->mock('GET /old-adage/a/bird/in/hand');
- $this->run();
- $this->expect(
- $this->get('routed')===3 &&
- $this->get('PARAMS.token1')==='bird' &&
- $this->get('PARAMS.token2')==='in' &&
- $this->get('PARAMS.token3')==='hand',
- 'URI tokens handled properly',
- 'Incorrect handling of URI tokens'
- );
- $this->mock('GET /old-adage/a/fool-and/his-money-are/soon-parted');
- $this->run();
- $this->expect(
- $this->get('routed')===3 &&
- $this->get('PARAMS.token1')==='fool-and' &&
- $this->get('PARAMS.token2')==='his-money-are' &&
- $this->get('PARAMS.token3')==='soon-parted',
- 'URI tokens distributed correctly',
- 'Incorrect distribution of URI tokens'
- );
- $this->mock('GET /old-adage/a/fool and/his money are/soon parted');
- $this->run();
- $this->expect(
- $this->get('PARAMS.token1')==='fool and' &&
- $this->get('PARAMS.token2')==='his money are' &&
- $this->get('PARAMS.token3')==='soon parted',
- 'URL-encoded data (containing spaces) in route handled properly',
- 'Issue with URL-encoded data containing spaces'
- );
- $this->mock('GET /%6f%6c%64-adage/a/fool-and/his-money-are/soon-parted');
- $this->run();
- $this->expect(
- $this->get('PARAMS.token1')==='fool-and' &&
- $this->get('PARAMS.token2')==='his-money-are' &&
- $this->get('PARAMS.token3')==='soon-parted',
- 'Raw URL-encoded data in request URI handled properly',
- 'Issue with raw URL-encoded data'
- );
- $this->mock('GET /a?hello=%40%77%6f%72%6c%64');
- $this->run();
- $this->expect(
- $this->get('GET.hello')==='@world',
- 'Raw URL-encoded data in GET variable',
- 'Issue with URL-encoded data in GET variable'
- );
- $this->clear('ROUTES');
- @mkdir('inc/temp',0755);
- file_put_contents('inc/temp/ext.php',
- '<?php '.
- 'class Ext {'.
- 'function myfunc() {'.
- 'F3::set(\'routed\',5);'.
- '}'.
- '}'
- );
- $this->route('GET /ext','Ext->myfunc');
- $this->mock('GET /ext');
- $this->run();
- $this->expect(
- $this->get('routed')===5,
- 'Routed to autoload class',
- 'Routing to autoload class failed'
- );
- @unlink('inc/temp/ext.php');
- @mkdir('inc/temp/ns',0755);
- file_put_contents('inc/temp/ns/deep.php',
- '<?php '.
- 'namespace ns; '.
- 'class Deep {'.
- 'public function innerfunc() {'.
- '\\F3::set(\'routed\',6);'.
- '}'.
- '}'
- );
- @mkdir('inc/temp/ns/ns2',0755);
- file_put_contents('inc/temp/ns/ns2/deeper.php',
- '<?php '.
- 'namespace ns\\ns2; '.
- 'class Deeper {'.
- 'function innermostfunc() {'.
- '\\F3::set(\'routed\',7);'.
- '}'.
- '}'
- );
- $this->route('GET /deep','ns\Deep->innerfunc');
- $this->mock('GET /deep');
- $this->run();
- $this->expect(
- $this->get('routed')===6,
- 'Autoloaded level-1 namespaced class',
- 'Routing to level-1 namespaced class failed'
- );
- $this->route('GET /deeper','ns\ns2\Deeper->innermostfunc');
- $this->mock('GET /deeper');
- $this->run();
- $this->expect(
- $this->get('routed')===7,
- 'Autoloaded level-2 namespaced class',
- 'Routing to level-2 namespaced autoload class failed'
- );
- dummy3();
- $this->expect(
- $this->get('routed')===3,
- 'Direct call to route handler',
- 'Unable to call route handler directly'
- );
- @unlink('inc/temp/ns/ns2/deeper.php');
- rmdir('inc/temp/ns/ns2');
- @unlink('inc/temp/ns/deep.php');
- rmdir('inc/temp/ns');
- rmdir('inc/temp');
- $this->clear('ROUTES');
- $min=1000; // 1 second minimum execution time
- $time=microtime(TRUE);
- $this->route('GET /throttle',
- function() {
- echo 'done!';
- },
- 0,1000
- );
- $this->set('QUIET',TRUE);
- $this->mock('GET /throttle');
- $this->run();
- $this->set('QUIET',FALSE);
- $elapsed=microtime(TRUE)-$time;
- $this->expect(
- $this->get('RESPONSE')=='done!' && $elapsed*1000>=$min,
- 'Throttle working properly: done in '.sprintf('%1.3f',$elapsed).' secs',
- 'Throttle malfunctioning: completed in '.sprintf('%1.3f',$elapsed).' secs'
- );
- echo $this->render('basic/results.htm');
- }
- function ecache() {
- $this->set('title','Cache Engine');
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $this->set('CACHE',TRUE);
- $this->expect(
- $this->get('CACHE'),
- 'Cache back-end detected: \''.$this->get('CACHE').'\'',
- 'Cache disabled'
- );
- $this->set('x',123,TRUE);
- $this->expect(
- $this->cached('x'),
- 'Framework variable cached',
- 'Variable not cached: '.var_export($this->cached('x'),TRUE)
- );
- $this->expect(
- $this->get('x'),
- 'Value retrieved from cache',
- 'Caching issue: '.var_export($this->get('x'),TRUE)
- );
- $this->clear('x');
- $this->expect(
- is_bool($this->cached('x')),
- 'Variable removed from cache',
- 'Caching issue: '.var_export($this->cached('x'),TRUE)
- );
- $this->clear('ROUTES');
- $ttl=3;
- $this->route('GET /caching',
- function() {
- echo 'here';
- },
- $ttl
- );
- $start=time();
- $i=0;
- while (TRUE) {
- $this->set('QUIET',TRUE);
- $this->mock('GET /caching');
- sleep(1);
- $this->run();
- $cached=Cache::cached('url.'.$this->hash('GET /caching'));
- if (is_bool($cached))
- break;
- $this->set('QUIET',FALSE);
- if (!isset($saved))
- $saved=$cached;
- if ($saved!=$cached)
- break;
- $time=time();
- $this->expect(TRUE,'Cache age @'.date('G:i:s',$time).': '.
- ($time-$cached).' secs');
- $i++;
- if ($i==$ttl)
- break;
- }
- $this->expect(
- $i==$ttl,
- 'Cache refreshed',
- 'Cache TTL has expired'
- );
- echo $this->render('basic/results.htm');
- }
- function validator() {
- $this->set('title','User Input');
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $this->route('POST /form',
- function() {
- F3::input('field1','nonexistent');
- }
- );
- $this->set('QUIET',TRUE);
- $this->mock('POST /form');
- $this->run();
- $this->expect(
- !is_null($this->get('ERROR')) && $this->get('ERROR.code')===500,
- 'HTTP 500 expected - form field handler is invalid',
- 'No HTTP 500 triggered'
- );
- $this->set('QUIET',FALSE);
- $this->clear('ERROR');
- $this->route('POST /form',
- function() {
- F3::input('field',
- function($value) {
- F3::expect(
- $value=='alert(\'hello\');',
- 'HTML tags removed (attempt to insert Javascript)',
- 'HTML tags were not removed: '.$value
- );
- }
- );
- }
- );
- $this->mock('POST /form',array('field'=>'<script>alert(\'hello\');</script>'));
- $this->run();
- $this->clear('ROUTES');
- $this->expect(
- $_POST['field']=='alert(\'hello\');' &&
- $_POST['field']=='alert(\'hello\');',
- 'Framework sanitizes underlying $_POST and $_POST variables',
- 'Framework didn\'t sanitize $_POST/$_POST: '.$_POST['field']
- );
- $this->set('POST',array('field'=>'<p><b>hello</b> world</p>'));
- $this->input('field',
- function($value) {
- F3::expect(
- $value=='<p>hello world</p>',
- 'HTML tags allowed but not converted to HTML entities'.
- '<br/>Note: application is responsible for '.
- 'HTML decoding',
- 'HTML tags not converted/blocked by framework: '.$value
- );
- },
- 'p'
- );
- $this->set('POST',array('field'=>'Adam & Eve'));
- $this->input('field',
- function($value) {
- F3::expect(
- $value=='Adam & Eve',
- 'Ampersand preserved',
- 'Ampersand converted to HTML entity!'
- );
- }
- );
- $this->set('POST',array('field'=>'©'));
- $this->input('field',
- function($value) {
- F3::expect(
- $value=='©',
- 'No duplicate encoding of HTML entity: '.$value,
- 'Double-encoding of HTML entity: '.$value
- );
- }
- );
- $this->set('POST',array('field'=>'hello "world"'));
- $this->input('field',
- function($value) {
- F3::expect(
- $value=='hello "world"',
- 'Double-quotes preserved: '.$value,
- 'Double-quotes not handled properly: '.$value
- );
- }
- );
- $this->expect(
- Data::validEmail('!def!xyz%abc@example.com'),
- 'Valid e-mail address: !def!xyz%abc@example.com',
- 'Framework flagged !def!xyz%abc@example.com invalid!'
- );
- $this->expect(
- Data::validEmail('"Abc@def"@example.com'),
- 'Valid e-mail address: "Abc@def"@example.com',
- 'Framework flagged "Abc@def"@example.com invalid!'
- );
- $this->expect(
- !Data::validEmail('"Abc@def"@example.com',TRUE),
- 'Invalid e-mail address: "Abc@def"@example.com (MX record verified)',
- 'Framework flagged "Abc@def"@example.com valid!'
- );
- $this->expect(
- !Data::validEmail('Abc@def@example.com'),
- 'Invalid e-mail address: Abc@def@example.com',
- 'Framework flagged Abc@def@example.com valid!'
- );
- $this->expect(
- Data::validEmail('a@b.com'),
- 'Valid e-mail address: a@b.com (MX record not verified)',
- 'Framework flagged a@b.com invalid!'
- );
- $this->expect(
- !Data::validEmail('a@b.com',TRUE),
- 'Invalid e-mail address: a@b.com (MX record verified)',
- 'Framework flagged a@b.com valid!'
- );
- $this->expect(
- Data::validURL('http://www.google.com'),
- 'Valid URL: http://www.google.com',
- 'Framework flagged http://www.google.com invalid!'
- );
- $this->expect(
- Data::validURL('http://www.yahoo.com/'),
- 'Valid URL: http://www.yahoo.com/',
- 'Framework flagged http://www.yahoo.com/ invalid!'
- );
- $this->expect(
- Data::validURL(
- 'http://www.google.com/search?ie=UTF-8&oe=UTF-8&sourceid=navclient'),
- 'Valid URL: '.
- 'http://www.google.com/search?ie=UTF-8&oe=UTF-8&sourceid=navclient',
- 'Framework flagged '.
- 'http://www.google.com/search?ie=UTF-8&oe=UTF-8&sourceid=navclient '.
- 'invalid!'
- );
- $this->expect(
- Data::validURL('http://www.yahoo.com?http%3A%2F%2Fwww.yahoo.com'),
- 'Valid URL: http://www.yahoo.com?http%3A%2F%2Fwww.yahoo.com',
- 'Framework flagged '.
- 'http://www.yahoo.com?http%3A%2F%2Fwww.yahoo.com invalid!'
- );
- echo $this->render('basic/results.htm');
- }
- function renderer() {
- $this->set('title','Output Rendering');
- $this->set('CACHE',TRUE);
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $out=$this->render('basic/layout.htm');
- $this->expect(
- $out==="",
- 'Subtemplate not defined - none inserted',
- 'Subtemplate insertion issue: '.$out
- );
- $this->set('sub','sub1.htm');
- $this->set('test','<i>italics</i>');
- $out=$this->render('basic/layout.htm');
- $this->expect(
- $out=="<i>italics</i>",
- 'HTML Special characters retained: '.$out,
- 'Problem with HTML insertion: '.$out
- );
- $this->set('sub','sub1.htm');
- $this->set('test','©');
- $out=$this->render('basic/layout.htm');
- $this->expect(
- $out=="©",
- 'HTML entity inserted: '.$this->render('basic/layout.htm'),
- 'Problem with HTML insertion: '.$out
- );
- $this->set('sub','sub1.htm');
- $this->set('test',chr(0));
- $out=$this->render('basic/layout.htm');
- $this->expect(
- $out==chr(0),
- 'Control characters allowed: '.$this->render('basic/layout.htm'),
- 'Control characters removed: '.$out
- );
- $this->set('sub','sub1.htm');
- $this->set('test','??? ???? ????? ?????? ??? ?? ???? ??.');
- $out=$this->render('basic/layout.htm');
- $this->expect(
- $this->render('basic/layout.htm')=="??? ???? ????? ?????? ??? ?? ???? ??.",
- 'UTF-8 character set rendered correctly: '.$out,
- 'UTF-8 issue: '.$out
- );
- $this->set('sub','sub1.htm');
- $this->set('test','I am here.');
- $out=$this->render('basic/layout.htm');
- $this->expect(
- $this->render('basic/layout.htm')=="I am here.",
- 'HTML entities preserved: '.$out,
- 'HTML entities converted: '.$out
- );
- $this->set('sub','sub2.htm');
- $this->set('src','/test/image');
- $this->set('alt',htmlspecialchars('this is "the" life'));
- $out=$this->render('basic/layout.htm');
- $this->expect(
- $out==
- '<img src="/test/image" alt="this is "the" life"/>',
- 'Double-quotes inside HTML attributes converted to XML entities',
- 'Problem with double-quotes inside HTML attributes: '.$out
- );
- $this->set('sub','sub3.htm');
- $out=$this->render('basic/layout.htm');
- $this->clear('div');
- $this->expect(
- $out=='',
- 'Undefined array renders empty output',
- 'Output not empty: '.$out
- );
- $this->set('sub','sub3.htm');
- $out=$this->render('basic/layout.htm');
- $this->set('div',NULL);
- $this->expect(
- $out=='',
- 'NULL used as group attribute renders empty output',
- 'Output not empty: '.$out
- );
- $this->set('sub','sub3.htm');
- $out=$this->render('basic/layout.htm');
- $this->set('div',array());
- $this->expect(
- $out=='',
- 'Empty array used as group attribute renders empty output',
- 'Output not empty: '.$out
- );
- $this->set('sub','sub3.htm');
- $this->set('div',
- array(
- 'coffee'=>array('arabica','barako','liberica','kopiluwak'),
- 'tea'=>array('darjeeling','pekoe','samovar')
- )
- );
- $out=$this->render('basic/layout.htm');
- $this->expect(
- preg_match(
- '#'.
- '<div>\s+'.
- '<p><span><b>coffee</b></span></p>\s+'.
- '<p>\s+'.
- '<span>arabica</span>\s+'.
- '<span>barako</span>\s+'.
- '<span>liberica</span>\s+'.
- '<span>kopiluwak</span>\s+'.
- '</p>\s+'.
- '</div>\s+'.
- '<div>\s+'.
- '<p><span><b>tea</b></span></p>\s+'.
- '<p>\s+'.
- '<span>darjeeling</span>\s+'.
- '<span>pekoe</span>\s+'.
- '<span>samovar</span>\s+'.
- '</p>\s+'.
- '</div>'.
- '#s',
- $out
- ),
- 'Subtemplate inserted; nested repeat directives rendered correctly',
- 'Template rendering issue: '.var_export($out,TRUE)
- );
- $this->set('sub','sub4.htm');
- $out=$this->render('basic/layout.htm');
- $this->expect(
- preg_match(
- '#'.
- '<script type="text/javascript">\s*'.
- 'function hello\(\) {\s*'.
- 'alert\(\'Javascript works\'\);\s*'.
- '}\s*'.
- '</script>\s*'.
- '<script type="text/javascript">alert\(unescape\("%3Cscript src=\'" \+ gaJsHost \+ "google-analytics\.com/ga\.js\' type=\'text/javascript\'%3E%3C/script%3E"\)\);</script>\s'.
- '#s',
- $out
- ),
- 'Javascript preserved',
- 'Javascript mangled: '.htmlentities($out)
- );
- $this->set('sub','sub5.htm');
- $this->set('cond1',FALSE);
- $this->set('cond3',FALSE);
- $out=trim($this->render('basic/layout.htm'));
- $this->expect(
- $out=='c1:F,c3:F',
- 'Conditional directives evaluated correctly: FALSE, FALSE',
- 'Incorrect evaluation of conditional directives: '.$out
- );
- $this->set('cond1',FALSE);
- $this->set('cond3',TRUE);
- $out=trim($this->render('basic/layout.htm'));
- $this->expect(
- $out=='c1:F,c3:T',
- 'Conditional directives evaluated correctly: FALSE, TRUE',
- 'Incorrect evaluation of conditional directives: '.$out
- );
- $this->set('cond1',TRUE);
- $this->set('cond2',FALSE);
- $out=trim($this->render('basic/layout.htm'));
- $this->expect(
- $out=='c1:T,c2:F',
- 'Conditional directives evaluated correctly: TRUE, FALSE',
- 'Incorrect evaluation of conditional directives: '.$out
- );
- $this->set('cond1',TRUE);
- $this->set('cond2',TRUE);
- $out=trim($this->render('basic/layout.htm'));
- $this->expect(
- $out=='c1:T,c2:T',
- 'Conditional directives evaluated correctly: TRUE, TRUE',
- 'Incorrect evaluation of conditional directives: '.$out
- );
- $pi=3.141592654;
- $money=63950.25;
- $this->set('sub','sub6.htm');
- $this->set('LANGUAGE','en');
- $out=$this->render('basic/layout.htm');
- // PHP 5.3.2 inserts a line feed at end of translation
- $this->expect(
- $out==
- "<h3>I love Fat-Free!</h3>\n".
- "<p>Today is ".ICU::format('{0,date}',array(time()))."</p>\n".
- "<p>The quick brown fox jumps over the lazy dog.</p>\n".
- "<p>".ICU::format('{0,number}',array($pi))."</p>\n".
- "<p>".ICU::format('{0,number,currency}',array($money))."</p>",
- 'English locale (i18n)',
- 'English locale mangled: '.$out
- );
- $this->set('sub','sub6.htm');
- $this->set('LANGUAGE','fr-FR');
- $out=$this->render('basic/layout.htm');
- // PHP 5.3.2 inserts a line feed at end of translation
- $this->expect(
- $out==
- "<h3>J'aime Fat-Free!</h3>\n".
- "<p>Aujourd'hui, c'est ".ICU::format('{0,date}',array(time()))."</p>\n".
- "<p>Les naïfs ægithales hâtifs pondant à Noël où il gèle sont sûrs d'être déçus et de voir leurs drôles d'œufs abîmés.</p>\n".
- "<p>".ICU::format('{0,number}',array($pi))."</p>\n".
- "<p>".ICU::format('{0,number,currency}',array($money))."</p>",
- 'Translated properly to French',
- 'French translation mangled: '.$out
- );
- $this->set('sub','sub6.htm');
- $this->set('LANGUAGE','es-AR');
- $out=$this->render('basic/layout.htm');
- // PHP 5.3.2 inserts a line feed at end of translation
- $this->expect(
- $out==
- "<h3>Me encanta Fat-Free!</h3>\n".
- "<p>Hoy es ".ICU::format('{0,date}',array(time()))."</p>\n".
- "<p>El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío, añoraba a su querido cachorro.</p>\n".
- "<p>".ICU::format('{0,number}',array($pi))."</p>\n".
- "<p>".ICU::format('{0,number,currency}',array($money))."</p>",
- 'Translated properly to Spanish',
- 'Spanish translation mangled: '.$out
- );
- $this->set('sub','sub6.htm');
- $this->set('LANGUAGE','de-DE');
- $out=$this->render('basic/layout.htm');
- // PHP 5.3.2 inserts a line feed at end of translation
- $this->expect(
- $out==
- "<h3>Ich liebe Fat-Free!</h3>\n".
- "<p>Heute ist ".ICU::format('{0,date}',array(time()))."</p>\n".
- "<p>Im finsteren Jagdschloß am offenen Felsquellwasser patzte der affig-flatterhafte kauzig-höfliche Bäcker über seinem versifften kniffligen Xylophon.</p>\n".
- "<p>".ICU::format('{0,number}',array($pi))."</p>\n".
- "<p>".ICU::format('{0,number,currency}',array($money))."</p>",
- 'Translated properly to German',
- 'German translation mangled: '.$out
- );
- $this->set('LANGUAGE','en');
- $this->set('benchmark',
- array_fill(1,100,
- array(
- 'a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,
- 'f'=>6,'g'=>7,'h'=>8,'i'=>9,'j'=>10
- )
- )
- );
- $time=microtime(TRUE);
- $this->render('basic/benchmark.htm');
- $elapsed=round(microtime(TRUE)-$time,3);
- $this->expect(
- $elapsed<0.05,
- 'Template containing '.(count($this->get('benchmark'))*10).
- '+ HTML elements/calculations rendered in '.$elapsed.' seconds',
- 'Template rendering too slow on this server: '.$elapsed.' seconds'
- );
- echo $this->render('basic/results.htm');
- }
- function template() {
- $this->set('title','Template Engine');
- $this->set('CACHE',TRUE);
- $this->expect(
- is_null($this->get('ERROR')),
- 'No errors expected at this point',
- 'ERROR variable is set: '.$this->get('ERROR.text')
- );
- $this->set('a',123);
- $this->set('b','{{@a}}');
- $this->expect(
- Template::resolve('{{@b}}')=='123',
- 'Template token substituted; string value returned',
- 'Token substition failed: '.Template::resolve('{{@b}}')
- );
- $this->expect(
- Template::resolve('{{@a}}')=='123',
- 'Template engine confirms substitution',
- 'Template engine failed: '.Template::resolve('{{@a}}')
- );
- $this->set('a',345);
- $this->expect(
- Template::resolve('{{@a}}')=='345',
- 'Template engine confirms replacement',
- 'Template engine failed: '.Template::resolve('{{@a}}')
- );
- $this->expect(
- Template::resolve('{{@a+1}}')=='346',
- 'Mixed expression correct',
- 'Mixed expression failed: '.Template::resolve('{{@a+1}}')
- );
- $this->expect(
- Template::resolve('{{@a + 1}}')=='346',
- 'Mixed expression (with whitespaces) correct',
- 'Mixed expression (with whitespaces) failed: '.Template::resolve('{{@a + 1}}')
- );
- $this->set('x','{{123}}');
- $this->expect(
- Template::resolve('{{@x}}')=='123',
- 'Integer constant in template expression correct',
- 'Template expression is wrong: '.Template::resolve('{{@x}}')
- );
- $this->set('i','hello');
- $this->set('j','there');
- $this->expect(
- Template::resolve('{{@i.@j}}')=='hellothere',
- 'String concatenation works',
- 'String concatenation problem: '.Template::resolve('{{@i.@j}}')
- );
- $this->expect(
- Template::resolve('{{@i. @j}}')=='hellothere',
- 'String concatenation (with whitespaces) works',
- 'String concatenation (with whitespaces) problem: '.
- Template::resolve('{{@i. @j}}')
- );
- $this->expect(
- Template::resolve('{{@i .@j}}')=='hellothere',
- 'Variation in string concatenation (with whitespaces) works',
- 'Variation in string concatenation (with whitespaces) problem: '.
- Template::resolve('{{@i .@j}}')
- );
- $this->expect(
- Template::resolve('{{ @i . @j }}')=='hellothere',
- 'Liberal amounts of whitespaces produces the correct result',
- 'Liberal amounts of whitespaces produces strange result: '.
- Template::resolve('{{ @i . @j }}')
- );
- $this->set('x','{{345+5}}');
- $this->expect(
- Template::resolve('{{@x}}')=='350',
- 'Arithmetic expression in template expression evaluated',
- 'Arithmetic expression is wrong: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{1+0.23e-4}}');
- $this->expect(
- Template::resolve('{{@x}}')=='1.000023',
- 'Negative exponential float in template expression correct',
- 'Negative exponential float is wrong: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{1+0.23e+4}}');
- $this->expect(
- Template::resolve('{{@x}}')=='2301',
- 'Positive exponential float in template expression correct',
- 'Positive exponential float is wrong: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{1+0.23e4}}');
- $this->expect(
- Template::resolve('{{@x}}')=='2301',
- 'Unsigned exponential float in template expression correct',
- 'Unsigned exponential float is wrong: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{456+7.5}}');
- $this->expect(
- Template::resolve('{{@x}}')=='463.5',
- 'Integer + float in template expression correct',
- 'Integer + float is wrong: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{(1+2)*3}}');
- $this->expect(
- Template::resolve('{{@x}}')=='9',
- 'Parenthesized arithmetic expression evaluated',
- 'Parenthesized expression is wrong: '.Template::resolve('{{@x}}')
- );
- $this->expect(
- Template::resolve('{{(@a+1)*2}}')=='692',
- 'Variable + arithmetic expression evaluated',
- 'Variable + arithmetic expression is wrong: '.
- Template::resolve('{{(@a+1)*2}}')
- );
- $this->set('x','{{(intval(1+2.25))*3}}');
- $this->expect(
- Template::resolve('{{@x}}')=='9',
- 'Allowed function and nested parentheses evaluated',
- 'Allowed function/parentheses failed: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{(round(234.567,1)+(-1)+1)*2}}');
- $this->expect(
- Template::resolve('{{@x}}')=='469.2',
- 'Function with multiple arguments evaluated',
- 'Function with multiple arguments failed: '.Template::resolve('{{@x}}')
- );
- $this->set('x',NULL);
- $this->expect(
- Template::resolve('{{@x}}')=='',
- 'NULL converted to empty string',
- 'NULL not converted to empty string: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{array()}}');
- $this->expect(
- Template::resolve('{{@x}}')=='',
- 'Empty array converted to empty string',
- 'Array conversion failed: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{array(1,2,3)}}');
- $this->expect(
- Template::resolve('{{@x}}')=='Array',
- 'Array converted to string \'Array\'',
- 'Array conversion failed: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{NULL}}');
- $this->expect(
- Template::resolve('{{@x}}')=='',
- 'NULL value evaluated',
- 'Incorrect NULL evaluation: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{null}}');
- $this->expect(
- Template::resolve('{{@x}}')=='',
- 'NULL value evaluated (case-insensitive)',
- 'Incorrect NULL evaluation: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{TRUE}}');
- $this->expect(
- Template::resolve('{{@x}}')=='1',
- 'Boolean TRUE expression evaluated',
- 'Incorrect boolean evaluation: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{FALSE}}');
- $this->expect(
- Template::resolve('{{@x}}')=='',
- 'Boolean FALSE expression converted to empty string',
- 'Incorrect boolean evaluation: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{0}}');
- $this->expect(
- Template::resolve('{{@x}}')=='0',
- 'Zero remains as-is',
- 'Incorrect evaluation of integer zero: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{a@b.com}}');
- $this->expect(
- Template::resolve('{{@x}}')=='\'a@b.com\'',
- 'E-mail address preserved',
- 'Incorrect interpretation of e-mail address: '.Template::resolve('{{@x}}')
- );
- $this->set('x','{{new CustomObj}}');
- $this->expect(
- Template::resolve('{{@x}}')=='\'new CustomObj\'',
- 'Object instantiation using template engine prohibited',
- 'Object instantiation issue: '.Template::resolve('{{@x}}')
- );
- $this->set('func',
- function($x) {
- return 123;
- }
- );
- $this->expect(
- Template::resolve('{{@func("hello")}}')==123,
- 'Variable containing anonymous function interpreted correctly',
- 'Template misunderstood variable containing anonymous function: '.
- Template::reso…
Large files files are truncated, but you can click here to view the full file