From c4e2c8c1a22d74e854a74fd55d8cf8099826f500 Mon Sep 17 00:00:00 2001 From: Slim Amamou Date: Thu, 7 Feb 2008 08:53:26 +0100 Subject: [PATCH] =?utf8?q?j'ai=20corrig=C3=A9=20un=20bug=20qui=20empechait?= =?utf8?q?=20le=20schema=20browser=20de=20fonctionner=20avec=20les=20noms?= =?utf8?q?=20de=20tables=20bizarres.=20tels=20que=20ceux=20de=20joomla.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bazdig/bazdig.css | 6 -- bazdig/bazdig.db | Bin 4096 -> 4096 bytes bazdig/console/index.php | 7 +- bazdig/db/set/index.php | 8 +- bazdig/history/index.php | 4 +- index.php | 3 - ini.php | 29 ------- lib/database.php | 9 +- lib/localresource.php | 24 +++--- lib/mark.php | 213 ++++++++++++++++++++++++++++++++++++++++++++++ lib/pagecopy.php | 80 +++++++++++++++++ lib/pageselector.php | 45 ++++++++++ lib/user.php | 28 ++++++ real.ini.php | 12 +++ test/MarkTest.php | 160 ++++++++++++++++++++++++++++++++++ test/PageCopyTest.php | 90 ++++++++++++++++++++ test/PageSelectorTest.php | 72 ++++++++++++++++ test/QueryTest.php | 80 +++++++++++++++++ 18 files changed, 801 insertions(+), 69 deletions(-) delete mode 100644 index.php delete mode 100644 ini.php create mode 100755 lib/mark.php create mode 100755 lib/pagecopy.php create mode 100644 lib/pageselector.php create mode 100755 lib/user.php create mode 100644 real.ini.php create mode 100644 test/MarkTest.php create mode 100644 test/PageCopyTest.php create mode 100644 test/PageSelectorTest.php create mode 100644 test/QueryTest.php diff --git a/bazdig/bazdig.css b/bazdig/bazdig.css index 3450343..6bc50ae 100644 --- a/bazdig/bazdig.css +++ b/bazdig/bazdig.css @@ -7,12 +7,6 @@ body { h3 { display: inline; } -#error { - background-color: yellow; - border: 2px solid red; - padding: 10px; - margin: 10px; -} #nav a { border: 1px outset silver ; padding: 5px 10px; diff --git a/bazdig/bazdig.db b/bazdig/bazdig.db index 1215223c0e6d4a2dae8eddc9184d78e81fd29c15..440128add8b674e989d4c8d911b3806222da3c52 100644 GIT binary patch delta 441 zcwUWAJ5Iwu5Vbu^V-gf%ml^FH%tmUxLD&&=oT@Ev-{Y7AEJMyZ(ps&Jvs_kaGj@qkYz-2 zKTs$|Q6dv9Vk%s>Z@Vt8^h_u9O);PA64%9Dg=f0Lc~nlTJ>R!m0lvgp zS>!E2Jx{tGajBRP77#z+k;kU~Rw)tli1Fo!Y{W5?$I+>9&$Vlgg{ diff --git a/bazdig/console/index.php b/bazdig/console/index.php index 7364af6..259d8be 100644 --- a/bazdig/console/index.php +++ b/bazdig/console/index.php @@ -18,12 +18,7 @@ -getparam('db')->file; - if (!is_writable($bazdig_db)) { - echo '
WARNING your history database is not writeable. chmod 777 '. $bazdig->file .' && chmod 666 '. $bazdig_db .'
'; - } -?> + diff --git a/bazdig/db/set/index.php b/bazdig/db/set/index.php index 0afb7d5..a14b50f 100644 --- a/bazdig/db/set/index.php +++ b/bazdig/db/set/index.php @@ -4,10 +4,10 @@ define('WARAQ_ROOT', '../../..'); require_once WARAQ_ROOT .'/'. 'ini.php'; - $_SESSION['db_type'] = $_GET['dbt']; - $_SESSION['db_name'] = $_GET['dbn']; - $_SESSION['db_host'] = $_GET['dbh']; - $_SESSION['db_user'] = $_GET['dbu']; + $_SESSION['db_type'] = $_GET['dbt']; + $_SESSION['db_name'] = $_GET['dbn']; + $_SESSION['db_host'] = $_GET['dbh']; + $_SESSION['db_user'] = $_GET['dbu']; $_SESSION['db_password'] = $_GET['dbp']; header('Location: '. $bazdig->get('/console')->url ); diff --git a/bazdig/history/index.php b/bazdig/history/index.php index e56de50..258b3bd 100644 --- a/bazdig/history/index.php +++ b/bazdig/history/index.php @@ -5,9 +5,9 @@ require "code.php"; - $dbFile = $bazdig->getparam("db")->file; + $dbFile = $bazdig->get("/bazdig.db"); $console = $bazdig->get("/console"); - SqlCode::set_db("sqlite:". $dbFile); + SqlCode::set_db("sqlite:". $dbFile->get_file()); if ($_GET['q']) { $queries = SqlCode::search($_GET['q']); diff --git a/index.php b/index.php deleted file mode 100644 index dfc90db..0000000 --- a/index.php +++ /dev/null @@ -1,3 +0,0 @@ -get(WARAQ_ROOT . '/bazdig'); - - $GLOBALS['bazdig'] = new WaraqService($bazdigService->url, $bazdigService->file); - $bazdig =& $GLOBALS['bazdig']; - $bazdig->setparam("db", $bazdig->get('bazdig.db')); - -function firstWord($string) -{ - $string = trim($string); - return str_trunkate($string, ' '); -} - -function str_trunkate($haystack, $needle) -{ - if (!$pos = strpos($haystack, $needle)) return $haystack; - return substr($haystack, 0, $pos); -} - diff --git a/lib/database.php b/lib/database.php index 87445da..ae8ca42 100755 --- a/lib/database.php +++ b/lib/database.php @@ -84,7 +84,7 @@ $createQuery = new SqlCode($row['sql']); $this->columns = $createQuery->extractColumns(); } else { - $query = "show columns from $table"; + $query = "show columns from `$table`"; $result = $db->query($query); foreach ($result as $column) { $this->columns []= new Column($column['Field'], $column['Type']); @@ -107,10 +107,3 @@ $this->type = $type; } } - - -function columnNames($row) -{ - return array_keys($row); -} - diff --git a/lib/localresource.php b/lib/localresource.php index 37b01bb..96751fc 100755 --- a/lib/localresource.php +++ b/lib/localresource.php @@ -6,20 +6,19 @@ function __construct($url, $file) { - $this->url = absolutize($url); - $this->file = absolutize($file); + $this->url = $url; + $this->file = $file; + } + + function LocalResource($url, $file) + { + $this->__construct($url, $file); } function get($path) { - $f = $this->file .'/'. $path; - if (strpos($this->url, '?')) { - $u = str_replace('?', "/$path?", $this->url ); - } else if (strpos($this->url, '#')) { - $u = str_replace('#', "/$path#", $this->url ); - } else { - $u = $this->url .'/'. $path; - } + $u = absolutize($this->url .'/'. $path); + $f = absolutize($this->file .'/'. $path); $r = new LocalResource($u, $f); return $r; } @@ -38,7 +37,10 @@ function absolutize($path) { $path = ereg_replace('/\./', '/', $path); - $path = ereg_replace('/[^/.]+/+\.\./', '/', $path); + while (ereg('/[^/.]+/\.\./', $path)) { + $path = ereg_replace('/[^/.]+/\.\./', '/', $path); + } return $path; } +?> diff --git a/lib/mark.php b/lib/mark.php new file mode 100755 index 0000000..e0a1641 --- /dev/null +++ b/lib/mark.php @@ -0,0 +1,213 @@ +creationDate = time(); + if ($id) { + $this->id = $id; + } + } + + static function get_table_name() + { + return "marks"; + } + + static function sql_select($wildcards = NULL) + { + $tableName = self::get_table_name(); + + $order = ' order by creationDate desc, pageUrl '; + if (array_key_exists('order', $wildcards)) { + $order = " order by ". $wildcards['order']; + unset($wildcards['order']); + } + + $limit = ''; + if (array_key_exists('limit', $wildcards)) { + $limit = " limit ". $wildcards['limit']; + unset($wildcards['limit']); + } + + if (empty($wildcards)) { + return "SELECT id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from $tableName $order $limit;"; + } + + $tuples = array(); + if (array_key_exists('pageUrl', $wildcards)) { + $tuples []= " pageUrl LIKE '". $wildcards['pageUrl'] ."%'"; + unset($wildcards['pageUrl']); + } + + foreach ($wildcards as $key => $value) { + $tuples []= "$key='$value'"; + } + + $conditions = implode(' and ', $tuples); + + $query = "SELECT id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from $tableName where $conditions $order $limit;"; + + return $query; + } + + function toHTMLanchor() + { + $anchor = "". $this->text .""; + return $anchor; + } + + function toSQLinsert() + { + $id = $this->id; + $creationDate = date('c', $this->creationDate); + $pageUrl = $this->pageUrl; + $text = $this->text; + $owner = $this->owner; + $startNodePath = $this->startNodePath; + $startOffset = $this->startOffset; + $endNodePath = $this->endNodePath; + $endOffset = $this->endOffset; + + $tableName = Mark::get_table_name(); + return "INSERT into $tableName (id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset) values ('$id', '$creationDate', '$pageUrl', '$text', '$owner', '$startNodePath', '$startOffset', '$endNodePath', '$endOffset');"; + } + + function toSQLselect() + { + return "SELECT creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from marks where id='". $this->id ."';"; + } + + static function set_db($db, $user = "", $password = "") + { + if ($db instanceof PDO) { + self::$db =& $db; + } else { + if (empty($user)) { + self::$db =& new PDO($db); + } else { + self::$db =& new PDO($db, $user, $password); + } + } + + return self::$db; + } + + static function count($wildcards = NULL, $db = NULL) + { + if ($db == NULL) { + $db = self::$db; + } + if (! $db instanceof PDO) { + throw new Exception("Not a Data Base"); + } + + $tableName = Mark::get_table_name(); + $query = "select count(*) from $tableName"; + if ($result = $db->query($query)) { + $result = $result->fetchAll(); + } else { + echo "Query error"; + print_r($db->errorInfo()); + throw new Exception("Query error"); + } + + return $result[0][0]; + } + + static function select($wildcards = NULL, $db = NULL) + { + if ($db == NULL) { + $db = self::$db; + } + if (! $db instanceof PDO) { + throw new Exception("Not a Data Base"); + } + $marks = array(); + $query = self::sql_select($wildcards); + if ($result = $db->query($query)) { + $result = $result->fetchAll(); + } else { + echo "Query error"; + print_r($db->errorInfo()); + throw new Exception("Query error"); + } + foreach ($result as $r) { + $mark = new Mark($r['id']); + $mark->creationDate = strtotime($r['creationDate']); + $mark->pageUrl = strip_http_get_params($r['pageUrl']); + $mark->text = $r['text']; + $mark->owner = $r['owner']; + $mark->startNodePath = json_decode($r['startNodePath']); + $mark->startOffset = $r['startOffset']; + $mark->endNodePath = json_decode($r['endNodePath']); + $mark->endOffset = $r['endOffset']; + array_push( $marks, $mark); + } + + return $marks; + } + + function save($db = NULL) + { + if ($db == NULL) { + $db = self::$db; + } + $query = $this->toSQLinsert(); + + $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $statement = $db->query($query); + + return $statement; + } + + function load($db = NULL) + { + if ($db == NULL) { + $db = self::$db; + } + if (is_string($db)) { + $db = new PDO($db, $user, $pass); + } + $query = $this->toSQLselect(); + if ($result = $db->query($query)) { + $result = $result->fetch(); + } else { + throw new Exception("Query error"); + } + + $this->creationDate = strtotime($result['creationDate']); + $this->pageUrl = $result['pageUrl']; + $this->text = $result['text']; + $this->owner = $result['owner']; + $this->startNodePath = $result['startNodePath']; + $this->startOffset = $result['startOffset']; + $this->endNodePath = $result['endNodePath']; + $this->endOffset = $result['endOffset']; + + return $this; + } + + function __toString() + { + return json_encode($this); + } + } + +function strip_http_get_params($url) +{ + if (!$urlBaseLength = strpos($url, '?')) return $url; + return substr($url, 0, $urlBaseLength); +} diff --git a/lib/pagecopy.php b/lib/pagecopy.php new file mode 100755 index 0000000..6a7d397 --- /dev/null +++ b/lib/pagecopy.php @@ -0,0 +1,80 @@ +date = date("c"); + $this->origin = $origin; + $c = $root->get($name); + $this->file = $c->get_file(); + $this->url = $c->get_url(); + } + + function update() + { + $ch = curl_init(); + $timeout = 5; // set to zero for no timeout + curl_setopt ($ch, CURLOPT_URL, $this->origin); + curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); + $file_contents = curl_exec($ch); + curl_close($ch); + + if ($content = $file_contents) { + + if (eregi("", $content)) { + $content = eregi_replace("", "". $this->startOfHEAD , $content); + } else { + $content = eregi_replace("^", "". $this->startOfHEAD , $content); + } + if (eregi("", $content)) { + $content = eregi_replace("", $this->endOfHEAD . "", $content); + } else { + $content = eregi_replace("^", $this->endOfHEAD . "", $content); + } + if (eregi("", $content)) { + $content = eregi_replace("", $this->endOfHTML . "", $content); + } else { + $content = eregi_replace("$", $this->endOfHTML . "", $content); + } + $copy = fopen($this->file, "w"); + if (! fwrite($copy, $content)) { + throw new Exception("Err: Ecriture"); + } + } + } + + function add_to_html($s) + { + $this->endOfHTML .= $s; + } + + function add_to_head($s) + { + $this->endOfHEAD .= $s; + } + + function add_to_head_start($s) + { + $this->startOfHEAD .= $s; + } + } + + function url2fileName($url) + { + $fileName = $url; + $forbidden = array('/',':','?','=','&','+','%'); + $fileName = ereg_replace('^http://', '', $fileName); //remove http:// + $fileName = ereg_replace('#.*$', '', $fileName); //remove fragment id + $fileName = str_replace($forbidden, '_', $fileName); + + return $fileName; + } diff --git a/lib/pageselector.php b/lib/pageselector.php new file mode 100644 index 0000000..1afd77b --- /dev/null +++ b/lib/pageselector.php @@ -0,0 +1,45 @@ +pageCount , "integer"); + settype($itemsCount, "integer"); + settype($itemsPerPage, "integer"); + + $this->baseUrl = $baseUrl; + $this->pageCount = $itemsCount / $itemsPerPage; + } + + function getLinks($page_number) + { + settype($page_number, "integer"); + settype($p, "integer"); + settype($first, "integer"); + settype($last, "integer"); + + $links = array(); + $first = $page_number - (MAX_SELECTABLE_PAGES / 2); + $first = $first > 0 ? $first : 1 ; + $last = $first + MAX_SELECTABLE_PAGES; + $last = $last < $this->pageCount ? $last : $this->pageCount; + + if ($page_number > 1) { + $links['previous'] = $this->baseUrl . ($page_number - 1); + } + for ($p=$first; $p <= $last; $p++) { + if ($p == $page_number) { + $links[$p] = '#'; + } else { + $links[$p] = $this->baseUrl . $p; + } + } + if ($page_number < $this->pageCount) { + $links['next'] = $this->baseUrl . ($page_number + 1); + } + + return $links; + } +} diff --git a/lib/user.php b/lib/user.php new file mode 100755 index 0000000..556bb7c --- /dev/null +++ b/lib/user.php @@ -0,0 +1,28 @@ +select("id")->from("users"); + $rightQuery->where(array('id' => $this->id, 'password' => $this->password)); + } + } diff --git a/real.ini.php b/real.ini.php new file mode 100644 index 0000000..31eb21d --- /dev/null +++ b/real.ini.php @@ -0,0 +1,12 @@ +setparam("db", "mysql:host=localhost;dbname=bazdig"); + $bazdig->setparam("db/user", "root"); + $bazdig->setparam("db/password", ""); +?> diff --git a/test/MarkTest.php b/test/MarkTest.php new file mode 100644 index 0000000..89c107b --- /dev/null +++ b/test/MarkTest.php @@ -0,0 +1,160 @@ +Mark =& new Mark("testid"); + $this->Mark2 =& new Mark("testid2"); + $this->Mark2->pageUrl = "http://localhost/test2"; + $this->Mark2->text = "test text2"; + $this->Mark3 =& new Mark("testid3"); + $this->Mark4 =& new Mark("testid4"); + $this->Mark4->pageUrl = "http://localhost/test?s=55555"; + $this->db =& new PDO("sqlite2:markkit-test.db"); + } + + function tearDown() + { + unset($this->Mark); + $tableName = Mark::get_table_name(); + $this->db->query("delete from $tableName where id='testid2'"); + } + + function testtoSQLinsert() + { + $result = $this->Mark->toSQLinsert(); + $expected = "/insert/i"; + $this->assertWantedPattern($expected, $result); + $expected = "/'testid'/"; + $this->assertWantedPattern($expected, $result); + $result = $this->Mark2->toSQLinsert(); + $expected = "/insert/i"; + $this->assertWantedPattern($expected, $result); + $expected = "/'testid2'/"; + $this->assertWantedPattern($expected, $result); + $expected = "/'http:\/\/localhost\/test2'/"; + $this->assertWantedPattern($expected, $result); + } + + function testtoSQLselect() + { + $result = $this->Mark->toSQLselect(); + $expected = "/select/i"; + $this->assertWantedPattern($expected, $result); + $expected = "/id='testid'/"; + $this->assertWantedPattern($expected, $result); + } + + function testsql_select() + { + $options = array('owner' => "testuser", 'pageUrl' => "http://localhost/dokuwiki/"); + $result = Mark::sql_select($options); + $expected = "/owner='testuser'/i"; + $this->assertWantedPattern($expected, $result); + $expected = "/pageUrl LIKE 'http:\/\/localhost\/dokuwiki\/%'/i"; + $this->assertWantedPattern($expected, $result); + } + + function testselect() + { + $this->Mark->creationDate = "1189807200"; + $this->Mark->pageUrl = "http://localhost/dokuwiki/"; + $this->Mark->text = "test text"; + $this->Mark->owner = "testuser"; + $this->Mark->startNodePath = array(1,2,3); + $this->Mark->startOffset = '0'; + $this->Mark->endNodePath = array(4,5,6); + $this->Mark->endOffset = '0'; + $this->Mark3->creationDate = "1189807200"; + $this->Mark3->pageUrl = "http://localhost/dokuwiki/x"; + $this->Mark3->text = "test text"; + $this->Mark3->owner = "testuser"; + $this->Mark3->startNodePath = array(1,2,3); + $this->Mark3->startOffset = '0'; + $this->Mark3->endNodePath = array(4,5,6); + $this->Mark3->endOffset = '0'; + $options = array('id' => 'testid'); + $result = Mark::select($options, $this->db); + $expected = array($this->Mark); + $this->assertEqual($expected, $result); + Mark::set_db($this->db); + $result = Mark::select($options); + $this->assertEqual($expected, $result); + Mark::set_db("sqlite2:markkit-test.db"); + $result = Mark::select($options); + $this->assertEqual($expected, $result); + $options = array('pageUrl' => "http://localhost/dokuwiki/"); + $result = Mark::select($options); + $expected = array($this->Mark, $this->Mark3); + $this->assertEqual($expected, $result); + } + + function testcount() + { + $result = $this->Mark->count(); + $expected = 3; + $this->assertEqual($expected, $result); + } + + function testsave() + { + $this->Mark2->save($this->db); + $result = $this->db->query("select * from marks where id='testid2'"); + $expected = '00000'; + $this->assertEqual($expected, $result->errorCode()); + $result = $result->fetch(); + $expected = 'http://localhost/test2'; + $this->assertEqual($expected, $result['pageUrl']); + $expected = 'test text2'; + $this->assertEqual($expected, $result['text']); + } + + function testload() + { + $result = $this->Mark->load($this->db); + $expected = "testuser"; + $this->assertEqual($expected, $result->owner); + } + + function testset_db() + { + global $waraq; + + $result = Mark::set_db("mysql:host=localhost;dbname=markkit", "root", ""); + $expected = "mysql"; + $this->assertEqual($expected, $result->getAttribute(PDO::ATTR_DRIVER_NAME)); + $result = Mark::set_db("sqlite2:markkit-test.db"); + $expected = "sqlite2"; + $this->assertEqual($expected, $result->getAttribute(PDO::ATTR_DRIVER_NAME)); + } + + function test_toHTMLanchor() + { + $result = $this->Mark2->toHTMLanchor(); + $expected = "test text2"; + $this->assertEqual($expected, $result); + } + + function test_strip_http_get_params() + { + $result = strip_http_get_params("http://localhost/test?s=55555"); + $expected = "http://localhost/test"; + $this->assertEqual($expected, $result); + } + +} +// Running the test. +$test =& new MarkTest; +$test->run(new HtmlReporter()); +?> diff --git a/test/PageCopyTest.php b/test/PageCopyTest.php new file mode 100644 index 0000000..c50a278 --- /dev/null +++ b/test/PageCopyTest.php @@ -0,0 +1,90 @@ +PHPUnit_TestCase($name); + } + + function setUp() + { + require_once '../lib/pagecopy.php'; + $url = "http://localhost/slim/markkit/test"; + $file = "/home/slim/markkit-glassjoe/markkit/test"; + $copydir = new LocalResource($url, $file); + $test_url_wf = "$url/test_page_well_formed.html"; + $test_url_ts = "$url/test_page_tag_soup.html"; + $this->PageCopy =& new PageCopy("testcopy.html",$test_url_wf, $copydir); + $this->PageCopyTS =& new PageCopy("testcopy_ts.html",$test_url_ts, $copydir); + } + + function tearDown() + { + unset($this->PageCopy); + unset($this->PageCopyTS); + unlink("testcopy.html"); + @unlink("testcopy_ts.html"); + } + + function testupdate() + { + $this->PageCopy->update(); + $this->assertTrue(is_file("testcopy.html")); + } + + function testadd_to_html() + { + $this->PageCopy->add_to_html("
"); + $this->PageCopy->update(); + $c = file_get_contents("testcopy.html"); + $this->assertTrue(ereg("id='testaddtohtml' />\s*", $c)); + $this->assertFalse(ereg("id='testaddtohtml'.*id='testaddtohtml'", $c)); + $this->PageCopyTS->add_to_html("
"); + $this->PageCopyTS->update(); + $c = file_get_contents("testcopy_ts.html"); + $this->assertTrue(ereg("id='testaddtohtml' />", $c)); + } + + function testadd_to_head() + { + $this->PageCopy->add_to_head("
"); + $this->PageCopy->update(); + $c = file_get_contents("testcopy.html"); + $this->assertTrue(ereg("id='testaddtohtml' />\s*", $c)); + $this->assertFalse(ereg("id='testaddtohtml'.*id='testaddtohtml'", $c)); + $this->PageCopyTS->add_to_head("
"); + $this->PageCopyTS->update(); + $c = file_get_contents("testcopy_ts.html"); + $this->assertTrue(ereg("id='testaddtohtml' />", $c)); + } + + function testadd_to_head_start() + { + $this->PageCopy->add_to_head_start("
"); + $this->PageCopy->update(); + $c = file_get_contents("testcopy.html"); + $this->assertTrue(ereg("\s*
", $c)); + $this->assertFalse(ereg("id='testaddtohtml'.*id='testaddtohtml'", $c)); + $this->PageCopyTS->add_to_head_start("
"); + $this->PageCopyTS->update(); + $c = file_get_contents("testcopy_ts.html"); + $this->assertTrue(ereg("
", $c)); + } +} +// Running the test. +$suite = new PHPUnit_TestSuite('PageCopyTest'); +$result = PHPUnit::run($suite); +echo $result->toString(); +?> diff --git a/test/PageSelectorTest.php b/test/PageSelectorTest.php new file mode 100644 index 0000000..9faaa7d --- /dev/null +++ b/test/PageSelectorTest.php @@ -0,0 +1,72 @@ +PageSelector =& new PageSelector("http://localhost/?p=", 2, 10); + $this->PageSelector2 =& new PageSelector("http://localhost/?p=", 2, 30); + } + + function test_getLinks() { + $result = $this->PageSelector->getLinks(4); + $expected = array('previous' => 'http://localhost/?p=3', + '1' => 'http://localhost/?p=1', + '2' => 'http://localhost/?p=2', + '3' => 'http://localhost/?p=3', + '4' => '#', + '5' => 'http://localhost/?p=5', + 'next' => 'http://localhost/?p=5'); + $this->assertEqual( $expected, $result ); + $result = $this->PageSelector2->getLinks(10); + $expected = array('previous' => 'http://localhost/?p=9', + '5' => 'http://localhost/?p=5', + '6' => 'http://localhost/?p=6', + '7' => 'http://localhost/?p=7', + '8' => 'http://localhost/?p=8', + '9' => 'http://localhost/?p=9', + '10' => '#', + '11' => 'http://localhost/?p=11', + '12' => 'http://localhost/?p=12', + '13' => 'http://localhost/?p=13', + '14' => 'http://localhost/?p=14', + '15' => 'http://localhost/?p=15', + 'next' => 'http://localhost/?p=11'); + $this->assertEqual( $expected, $result ); + $result = $this->PageSelector2->getLinks(3); + $expected = array('previous' => 'http://localhost/?p=2', + '1' => 'http://localhost/?p=1', + '2' => 'http://localhost/?p=2', + '3' => '#', + '4' => 'http://localhost/?p=4', + '5' => 'http://localhost/?p=5', + '6' => 'http://localhost/?p=6', + '7' => 'http://localhost/?p=7', + '8' => 'http://localhost/?p=8', + '9' => 'http://localhost/?p=9', + '10' => 'http://localhost/?p=10', + '11' => 'http://localhost/?p=11', + 'next' => 'http://localhost/?p=4'); + $this->assertEqual( $expected, $result ); + $result = $this->PageSelector2->getLinks(14); + $expected = array('previous' => 'http://localhost/?p=13', + '5' => 'http://localhost/?p=5', + '6' => 'http://localhost/?p=6', + '7' => 'http://localhost/?p=7', + '8' => 'http://localhost/?p=8', + '9' => 'http://localhost/?p=9', + '10' => 'http://localhost/?p=10', + '11' => 'http://localhost/?p=11', + '12' => 'http://localhost/?p=12', + '13' => 'http://localhost/?p=13', + '14' => '#', + '15' => 'http://localhost/?p=15', + 'next' => 'http://localhost/?p=15'); + $this->assertEqual( $expected, $result ); + } +} +// Running the test. +$test = &new PageSelectorTest; +$test->run(new HtmlReporter()); diff --git a/test/QueryTest.php b/test/QueryTest.php new file mode 100644 index 0000000..c9533ca --- /dev/null +++ b/test/QueryTest.php @@ -0,0 +1,80 @@ +PHPUnit_TestCase($name); + } + + function setUp() + { + require_once '../lib/database.php'; + $this->Query =& new Query(); + $this->DataBase =& new DataBase("sqlite://home/slim/waraq/test/database.db"); + } + + function tearDown() + { + unset($this->Query); + } + + function testdb() + { + $result = $this->Query->db(PARAM); + $expected = EXPECTED_VAL; + $this->assertEquals($expected, $result); + } + + function testinsert() + { + $addItemQuery = new Query; + //$addItemQuery->insert("item")->set("p1='test'")->then("p2='test'"); + + $addItemQuery->string(); + + + + $p1 = $getItemQuery->db(); + $this->assertEquals($expected, $result); + + } + + function testselect() + { + $result = $this->Query->select("p1")->from("item")->where("p2='test'")->string(); + $expected = "select p1 from item where p2='test'"; + $this->assertEquals($expected, $result); + } + + function testfrom() + { + $result = $this->Query->from(PARAM); + $expected = EXPECTED_VAL; + $this->assertEquals($expected, $result); + } + + function testwhere() + { + $result = $this->Query->where(PARAM); + $expected = EXPECTED_VAL; + $this->assertEquals($expected, $result); + } + +} +// Running the test. +$suite = new PHPUnit_TestSuite('QueryTest'); +$result = PHPUnit::run($suite); +echo $result->toString(); +?> -- 2.11.4.GIT