Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

integrate testsuite into Agile Toolkit

  • Loading branch information...
commit 9d431258675f58b51d45c3421a0d8017c588afd3 1 parent e9e276f
@romaninsh romaninsh authored
View
3  README.md
@@ -1,8 +1,9 @@
-[![Build Status](https://travis-ci.org/atk4/atk4.png?branch=master)](https://travis-ci.org/atk4/atk4)
Agile Toolkit - Web UI Toolkit
====
Agile Toolkit is a Web UI framework and collection of usable widgets. It allows you to develop rich web applications by writing only PHP code. Agile Toolkit is inspired by Desktop GUI Toolkits and is a fully-object oriented development environment.
+[![Build Status](https://travis-ci.org/atk4/atk4.png?branch=master)](https://travis-ci.org/atk4/atk4)
+
Overview
----
Agile Toolkit has introduced three new principles in web development:
View
2  lib/AbstractView.php
@@ -144,7 +144,7 @@ function getHTML($destroy = true, $execute_js = true)
$this->removeHook('output', array($this, '_tsBuffer'));
$ret=$this->_tsBuffer;
$this->_tsBuffer='';
- if ($execute_js && $this->api->jquery) {
+ if ($execute_js && @$this->api->jquery) {
$this->api->jquery->getJS($this);
}
if ($destroy) {
View
4 lib/ApiWeb.php
@@ -212,7 +212,9 @@ function initializeSession($create=true){
session creation and placing cookies is to enhance user privacy.
Call to memorize() / recall() will automatically create session */
- if($this->_is_session_initialized)return;
+
+
+ if($this->_is_session_initialized || session_id())return;
// Change settings if defined in settings file
$params=session_get_cookie_params();
View
6 lib/Page/Tester.php
@@ -58,9 +58,9 @@ function init(){
$ff=$f->addField('Text','responses');
$this->responses=
' public $proper_responses=array(
- '.join(',
- ',$this->responses).'
- );';
+ '.join(',
+ ',$this->responses).'
+ );';
$ff->set($this->responses);
$ff->js('click')->select();
}
View
1  tests/.htaccess
@@ -1 +0,0 @@
-deny from all
View
5 tests/config-default.php
@@ -0,0 +1,5 @@
+<?php
+$config['atk']['base_path']='../';
+$config['url_postfix']='';
+$config['url_prefix']='?page=';
+$config['logger']['log_output']='full';
View
5 tests/index.php
@@ -0,0 +1,5 @@
+<?php
+include '../loader.php';
+$api=new TestApi('sample_project');
+$api->main();
+?>
View
72 tests/lib/Model/AgileTest.php
@@ -0,0 +1,72 @@
+<?php
+/**
+ * This model traverses pages of your project to look for test-cases.
+ *
+ * Test-files must implement a class descending from Page_Tester, refer
+ * to that class for more info.
+ */
+class Model_AgileTest extends Model {
+ public $dir='page';
+
+ function init(){
+ parent::init();
+
+ $this->addField('name');
+ $this->addField('total');
+ $this->addField('success');
+ $this->addField('fail');
+ $this->addField('exception');
+ $this->addField('speed');
+ $this->addField('memory');
+ $this->addField('result');
+
+ /**
+ * This model automatically sets its source by traversing
+ * and searching for suitable files
+ */
+ $p=$this->api->pathfinder->searchDir($this->dir);
+ sort($p);
+ $this->setSource('ArrayAssoc',$p);
+ $this->addHook('afterLoad',$this);
+
+ return $this;
+ }
+ function skipped(){
+ $this['result']='Skipped';
+ return $this;
+ }
+ function afterLoad(){
+ // Extend this method and return skipped() for the tests which
+ // you do not want to run
+ if (false) {
+ return $this->skipped();
+ }
+
+ $page='page_'.str_replace('/','_',str_replace('.php','',$this['name']));
+ try {
+ $p=$this->api->add($page,array('auto_test'=>false));
+
+ if(!$p instanceof Page_Tester){
+ $this['result']='Not Supported';
+ return;
+ }
+
+ if(!$p->proper_responses){
+ $this['result']='No proper responses';
+ return;
+ }
+
+ // This will execute the actual test
+ $res=$p->silentTest();
+ $this->set($res);
+ $this['speed']=round($this['speed'],3);
+ //list($this['total_tests'], $this['successful'], $this['time']) =
+ $this['result']=$this['success']==$this['total']?'OK':'FAIL';
+
+ $p->destroy();
+ }catch(Exception $e){
+ $this['result']='Exception: '.$e->getMessage();
+ return;
+ }
+ }
+}
View
20 tests/lib/TestApi.php
@@ -0,0 +1,20 @@
+<?php
+class TestApi extends ApiFrontend {
+ function init(){
+ parent::init();
+ $this->add('jUI');
+ $m=$this->add('Menu',null,'Menu');
+ $m->addMenuItem('index','Back');
+ }
+ function page_index($page){
+ $l=$this->add('Grid');
+ $l->setModel('AgileTest');
+ $l->addTotals();
+ $l->addHook('formatRow',function($l){
+ $n=$l->current_row['name'];
+ $n=str_replace('.php','',$n);
+ $n='<a href="'.$l->api->url($n).'">'.$n.'</a>';
+ $l->current_row_html['name']=$n;
+ });
+ }
+}
View
176 tests/page/core.php
@@ -0,0 +1,176 @@
+<?php
+
+class MyObject extends AbstractObject {
+ public $x=1;
+}
+class MyController extends AbstractObject {
+ public $x=1;
+}
+class Exception_Logic_Ouch extends Exception_Logic {
+}
+class TrackedObject extends AbstractObject {
+ public $auto_track_element=true;
+}
+
+class page_core extends Page_Tester {
+ public $proper_responses=array(
+ "Test_empty"=>'1',
+ "Test_clone"=>'1',
+ "Test_destroy"=>'1',
+ "Test_getElement"=>'1',
+ "Test_getElement1"=>'TrackedObject',
+ "Test_getElement2"=>'1',
+ "Test_hasElement2"=>'1',
+ "Test_uniqname"=>'1',
+ "Test_add"=>'1',
+ "Test_add2_controller"=>'',
+ "Test_session"=>'1',
+ "Test_session2"=>'1',
+ "Test_session3"=>'1',
+ "Test_exception"=>'1',
+ "Test_exception2"=>'1',
+ "Test_exception3"=>'1',
+ "Test_hook1"=>'1',
+ "Test_hook2"=>'1',
+ "Test_hook3"=>'1',
+ "Test_addMethod"=>'1',
+ "Test_addGlobalMethod"=>'1',
+ "Test_addon1"=>'helloworld_core',
+ "Test_addon2"=>'helloworld_core'
+ );
+ function prepare(){
+ return array($this->add('MyObject'));
+ }
+ function test_empty($t){
+ return $t->x;
+ }
+ function test_clone($t){
+ $t2=clone $t;
+ return $t2->x;
+ }
+ function test_destroy($t){
+ $s=serialize($t->elements);
+ $t->add('MyObject')->destroy();
+ $s2=serialize($t->elements);
+ return $s==$s2;
+ }
+ function test_getElement($t){
+ return !is_object($t->owner->getElement($t->short_name));
+ }
+ function test_getElement1($t){
+ $t=$this->add('TrackedObject');
+ return get_class($t->owner->getElement($t->short_name));
+ }
+ function test_getElement2($t){
+ try{
+ $t->owner->getElement('no such element');
+ return false;
+ }catch(Exception $e){
+ return true;
+ }
+ }
+ function test_hasElement2($t){
+ return !$t->owner->hasElement('no such element');
+ }
+ function test_uniqname($t){
+ $o1=$t->add('MyObject','c1');
+ $o2=$t->add('MyObject','c1');
+ return $o1->name!=$o2->name;
+ }
+ function test_add($t){
+ $o1=$t->add('MyObject','c1');
+ $o2=$t->add('MyObject','c1');
+ $o1->x++;
+ return $o1->x != $o2->x;
+ }
+ function test_add2_controller($t){
+ // same controller added twice uses same value
+ $o1=$t->add('MyController','c1');
+ $o2=$t->add('MyController','c1');
+ $o1->x++;
+ return $o1->x == $o2->x;
+ }
+
+ /*
+ *
+ * session is not working in CLI
+ function test_session($t){
+ $t->memorize('foo',true);
+ return $t->recall('foo');
+ }
+ function test_session2($t){
+ $t->learn('foo',true);
+ $t->learn('foo','123');
+ return $t->recall('foo');
+ }
+ function test_session3($t){
+ $t->learn('foo',1);
+ $t->forget('foo');
+ return $t->recall('foo',2)===2;
+ }
+ */
+
+ function test_exception($t){
+ try{
+ throw $t->exception('test');
+ }catch(Exception $e){
+ return true;
+ }
+ return false;
+ }
+ function test_exception2($t){
+ try{
+ throw $t->exception('test','Logic');
+ }catch(Exception_Logic $e){
+ return true;
+ }
+ return false;
+ }
+ function test_exception3($t){
+
+ try{
+ $t->default_exception='Exception_Logic';
+ throw $t->exception('test','_Ouch');
+ }catch(Exception_Logic_Ouch $e){
+ return true;
+ }
+ return false;
+ }
+
+
+ function myfunc($t,$br=false){
+ if($br)$t->breakHook(65);
+ return 42;
+ }
+ function myfunc2($x,$t){
+ return 44+$t->x;
+ }
+ function test_hook1($t){
+ $t->addHook('test',array($this,'myfunc'));
+ $r=$t->hook('test');
+ return $r[0]==42;
+ }
+ function test_hook2($t){
+ $t->addHook('test',array($this,'myfunc'));
+ $r=$t->hook('test',array(true));
+ return $r==65;
+ }
+ function test_hook3($t){
+ $t->addHook('test',array($this,'myfunc'));
+ $t->removeHook('test');
+ $r=$t->hook('test',array($t,true));
+ return !$r;
+ }
+
+ function test_addMethod($t){
+ $t->addMethod('testmethod',array($this,'myfunc'));
+ return $t->testmethod()==42;
+ }
+ function test_addGlobalMethod($t){
+ $t->api->addGlobalMethod('testmethod_gl',array($this,'myfunc2'));
+ $res=($x=$t->testmethod_gl())==45;
+ $t->api->removeGlobalMethod('testmethod');
+ return $res;
+ }
+}
+
View
60 tests/page/exceptions.php
@@ -0,0 +1,60 @@
+<?php
+class page_exceptions extends Page_Tester {
+ public $proper_responses=array(
+ "Test_exception1"=>'Proper Exception',
+ "Test_exception2"=>'Exception_ForUser',
+ "Test_exception3"=>'Exception_Logic_test',
+ "Test_moreinfo1"=>'',
+ "Test_htmloutput"=>'Exception_ForUser: Unable to access file (filename=testfile.txt)'
+ );
+ function prepare(){
+ return array();
+ }
+
+ function test_exception1(){
+ try{
+ throw $this->exception('Proper Exception');
+ }catch(Exception $e){
+ return $e->getMessage();
+ }
+ }
+ function test_exception2(){
+ $c=$this->add('Controller');
+ $c->default_exception='Exception_ForUser';
+
+ try{
+ throw $c->exception('Proper Exception');
+ }catch(Exception $e){
+ return get_class($e);
+ }
+ }
+ function test_exception3(){
+ $c=$this->add('Controller');
+ $c->default_exception='Exception_Logic';
+
+ try{
+ throw $c->exception('Proper Exception','_test');
+ }catch(Exception $e){
+ return get_class($e);
+ }
+ }
+ function test_moreinfo1(){
+ try{
+ throw $this->exception('Proper Exception','Exception_ForUser')
+ ->addMoreInfo('foo','bar');
+ }catch(Exception $e){
+ // return $e->getHTML();
+ }
+ }
+ function test_htmloutput(){
+ try {
+ throw $this->exception('Unable to access file',null,'13')
+ ->addMoreInfo('filename','testfile.txt');
+ } catch (Exception $e){
+ $result=$e->getText();
+ $result=preg_replace('/ in .*$/','',$result);
+ return $result;
+ }
+ }
+}
+class Exception_Logic_test extends Exception_Logic {}
View
132 tests/page/model3.php
@@ -0,0 +1,132 @@
+<?php
+
+class page_model3 extends Page_Tester {
+ public $proper_responses=array(
+ "Test_create"=>'[]',
+ "Test_source"=>'{"controller_data_array":[]}',
+ "Test_field"=>'{"controller_data_array":[]}',
+ "Test_save"=>'{"controller_data_array":{"1":{"name":"John","id":1}}}',
+ "Test_save2"=>'{"controller_data_array":{"1":{"name":"John","id":1},"2":{"name":"Susan","id":2}}}',
+ "Test_savelater"=>'{"controller_data_array":{"1":{"name":"John","id":1}}}',
+ "Test_savelater2"=>'{"controller_data_array":{"1":{"name":"John","id":1},"2":{"name":"Susan","id":2}}}',
+ "Test_save3"=>'{"name":"Susan","id":2}',
+ "Test_load"=>'{"name":"John","id":1}',
+ "Test_loadby"=>'{"name":"John","id":1}'
+ );
+ function prepare(){
+ return array();
+ }
+ function test_create(){
+ $m=$this->add('Model_Simple');
+ return json_encode($m->_table);
+ }
+ function test_source(){
+ $m=$this->add('Model_Simple');
+ $m->setSource('Array',array());
+ return json_encode($m->_table);
+ }
+ function test_field(){
+ $m=$this->add('Model_Simple');
+ $m->setSource('Array',array());
+ $m->addField('name');
+ return json_encode($m->_table);
+ }
+ function test_save(){
+ $m=$this->add('Model_Simple');
+ $m->setSource('Array',array());
+ $m->addField('name');
+ $m->set('name','John');
+ $m->save();
+
+ return json_encode($m->_table);
+ }
+ function test_save2(){
+ $m=$this->add('Model_Simple');
+ $m->setSource('Array',array());
+ $m->addField('name');
+ $m->set('name','John');
+ $m->save();
+ $m->unload();
+
+ $m->set('name','Susan');
+ $m->save();
+
+ return json_encode($m->_table);
+ }
+ function test_savelater(){
+ $m=$this->add('Model_Simple');
+ $m->setSource('Array',array());
+ $m->addField('name');
+ $m->set('name','John');
+ $m->save();
+ $m->unload();
+
+ $m->set('name','Susan');
+ $m->saveLater();
+
+ return json_encode($m->_table);
+ }
+ function test_savelater2(){
+ $m=$this->add('Model_Simple');
+
+ // TODO: test array connectivity
+
+ $m->setSource('Array',array());
+ $m->addField('name');
+ $m->set('name','John');
+ $m->save();
+ $m->unload();
+
+ $m->set('name','Susan');
+ $m->saveLater();
+ $m->unload();
+
+ return json_encode($m->_table);
+ }
+ function test_save3(){
+ $m=$this->add('Model_Simple');
+ $m->setSource('Array',array());
+ $m->addField('name');
+ $m->set('name','John');
+ $m->save();
+ $m->unload();
+
+ $m->set('name','Susan');
+ $m->save();
+
+ return json_encode($m->get());
+ }
+ function test_load(){
+ $m=$this->add('Model_Simple');
+ $m->setSource('Array',array());
+ $m->addField('name');
+ $m->set('name','John');
+ $m->save();
+ $m->unload();
+
+ $m->set('name','Susan');
+ $m->save();
+
+ $m->load(1);
+
+ return json_encode($m->get());
+ }
+ function test_loadby(){
+ $m=$this->add('Model_Simple');
+ $m->setSource('Array',array());
+ $m->addField('name');
+ $m->set('name','John');
+ $m->save();
+ $m->unload();
+
+ $m->set('name','Susan');
+ $m->save();
+
+ $m->loadBy('name','John');
+
+ return json_encode($m->get());
+ }
+}
+
+class Model_Simple extends Model {
+}
View
56 tests/page/modeldef.php
@@ -0,0 +1,56 @@
+<?php
+
+class page_modeldef extends Page_Tester {
+ public $db;
+ public $proper_responses=array(
+ "Test_addfield"=>'OK',
+ "Test_settergetter"=>'int',
+ "Test_person1"=>'John',
+ "Test_person2"=>''
+ );
+ function init(){
+ $this->db=$this->add('DB');
+ parent::init();
+ }
+ function prepare(){
+ return array($this->add('Model'));
+ }
+ function test_addfield($m){
+ $m->addField('hello');
+ return 'OK';
+ }
+ function test_settergetter($m){
+ $m->addField('name')->type('string');
+ $m->addField('age')->type('int')->calculated(true);
+
+ return $m->getElement('age')->type();
+ }
+
+ function makePerson($m){
+
+ $m->addField('name')->type('string');
+ $m->addField('age')->type('int')->calculated(true);
+ $m->addMethod('calculate_age',function(){
+ return "test123";
+ });
+ }
+
+
+ function test_person1($m){
+ $this->makePerson($m);
+
+ $m->set('name','John');
+ return $m->get('name');
+ }
+ function test_person2($m){
+ $this->makePerson($m);
+
+ try{
+ $m->set('nosuchfield','John');
+ }catch(Exception_Logic $e){
+ return 'No such field: GOOD!';
+ }
+ return $m->get('name');
+ }
+}
+
View
66 tests/page/normalize.php
@@ -0,0 +1,66 @@
+<?php
+
+class page_normalize extends Page_Tester {
+ public $proper_responses=array(
+ "Test_empty"=>'',
+ "Test_normal"=>'Lazy_dog_over_the_fox',
+ "Test_before_after"=>'Hey_there',
+ "Test_padder1"=>'Hey-there-Bring-me-my-sandwich',
+ "Test_padder2"=>'Hey.there.Bring.me.my.sandwich',
+ "Test_padder3"=>'Hey\\there\\Bring\\me\\my\\sandwich',
+ "Test_padder4"=>'Hey^there^Bring^me^my^sandwich',
+ "Test_c_basic"=>'Model_Hi',
+ "Test_c_basic2"=>'Model_Hi',
+ "Test_c_basic3"=>'Model_Data_Eater',
+ "Test_c_prefix"=>'hellokitty\\Model_Hi',
+ "Test_c_prefix2"=>'hellokitty\\Model_Hi',
+ "Test_c_prefix3"=>'hellokitty\\Model_Hi',
+ "Test_c_insidematch"=>'Model_View_Model_Test'
+ );
+ function prepare(){
+ return null;
+ }
+ function test_empty(){
+ return $this->api->normalizeName('');
+ }
+ function test_normal(){
+ return $this->api->normalizeName('Lazy dog )(*&)(& over the fox');
+ }
+ function test_before_after(){
+ return $this->api->normalizeName('>> Hey there!');
+ }
+ function test_padder1(){
+ return $this->api->normalizeName('>> Hey there! Bring me my sandwich!','-');
+ }
+ function test_padder2(){
+ return $this->api->normalizeName('>> Hey there! Bring me my sandwich!','.');
+ }
+ function test_padder3(){
+ return $this->api->normalizeName('>> Hey there! Bring me my sandwich!','\\');
+ }
+ function test_padder4(){
+ return $this->api->normalizeName('>> Hey there! Bring me my sandwich!','^_^');
+ }
+ function test_c_basic(){
+ return $this->api->normalizeClassName('Hi','Model');
+ }
+ function test_c_basic2(){
+ return $this->api->normalizeClassName('Model_Hi','Model');
+ }
+ function test_c_basic3(){
+ return $this->api->normalizeClassName('Model_Data_Eater','Model_Data');
+ }
+ function test_c_prefix(){
+ return $this->api->normalizeClassName('hellokitty/Model_Hi','Model');
+ }
+ function test_c_prefix2(){
+ return $this->api->normalizeClassName('hellokitty/Hi','Model');
+ }
+ function test_c_prefix3(){
+ return $this->api->normalizeClassName('hellokitty\\Hi','Model');
+ }
+ function test_c_insidematch(){
+ return $this->api->normalizeClassName('View_Model_Test','Model');
+ }
+}
+
View
65 tests/page/view.php
@@ -0,0 +1,65 @@
+<?php
+
+class MyInvisible extends AbstractView {
+ function recursiveRender(){
+ }
+}
+
+class page_view extends Page_Tester {
+ public $proper_responses=array(
+ "Test_empty"=>'<p>Hello world</p>',
+ "Test_frame"=>'
+<div id="myapi_frame" class="ui-widget-content ui-corner-all atk-box " style="">
+ <h2>FrameTitle</h2>
+ <p>Hello world</p>
+</div>
+',
+ "Test_lister"=>'Smith<br/>
+Blogs<br/>
+',
+ "Test_lister2"=>'Smith<br/>
+Blogs<br/>
+'
+ );
+ function init(){
+ $this->i=$this->add('MyInvisible');
+ parent::init();
+ $this->i->destroy();
+ }
+ function prepare($junk,$method){
+ return array($this->i->add('View',$method));
+ }
+ function test_empty($t){
+ $x=$t->add('HelloWorld');
+ return $x->getHTML();
+ }
+
+ /*
+ function test_frame($t){
+
+ $api=new ApiWeb('myapi');
+ $x=$api->add('Frame')->setTitle('FrameTitle');
+ $x->add('HelloWorld');
+ $x->getHTML();
+ }
+ */
+ function test_lister($t){
+ $s3 = array(
+ array('a'=>1,'b'=>'John','name'=>'Smith'),
+ array('a'=>2,'b'=>'Joe','name'=>'Blogs')
+ );
+ $l=$this->add('Lister');
+ $l->setSource($s3);
+ return $l->getHTML();
+ }
+ function test_lister2($t){
+ $s3 = array(
+ 'Smith','Blogs'
+ );
+ $l=$this->add('Lister');
+ $l->setSource($s3);
+ return $l->getHTML();
+ }
+
+}
+
View
19 tests/run_tests.php
@@ -0,0 +1,19 @@
+<?php
+require'../loader.php';
+
+$api=new ApiCLI();
+
+$tests = $api->add('Model_AgileTest');
+
+$result='OK';
+
+foreach($tests as $row){
+ foreach($row as $key=>$val){
+ if($key=='name')$val=str_pad($val,10);
+ echo "$key: $val\t";
+ }
+ if($row['result']!='OK')$result='FAIL';
+ echo "\n";
+}
+
+if($result!='OK')exit(1);
View
3  tests/run_tests.sh
@@ -1,3 +1,4 @@
#!/bin/bash
-echo "Tests passed OK"
+cd `pwd`
+php run_tests.php && echo "Tests passed OK" || echo "Some tests failed"
Please sign in to comment.
Something went wrong with that request. Please try again.