[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[376] | 5 | * Copyright 2001-2010 Strangecode, LLC |
---|
[362] | 6 | * |
---|
| 7 | * This file is part of The Strangecode Codebase. |
---|
[42] | 8 | * |
---|
[362] | 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
| 13 | * |
---|
| 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * PHPUnit test case for codebase/lib/Version.inc.php |
---|
| 25 | * |
---|
[42] | 26 | * The method skeletons below need to be filled in with |
---|
| 27 | * real data so that the tests will run correctly. Replace |
---|
| 28 | * all EXPECTED_VAL and PARAM strings with real data. |
---|
| 29 | * |
---|
[1] | 30 | * Created with PHPUnit_Skeleton on 2005-08-09 |
---|
| 31 | */ |
---|
| 32 | require_once 'PHPUnit.php'; |
---|
[137] | 33 | class VersionTest extends PHPUnit_TestCase { |
---|
[1] | 34 | |
---|
[137] | 35 | var $Version; |
---|
[1] | 36 | var $Auth_SQL; |
---|
| 37 | |
---|
[137] | 38 | function VersionTest($name) |
---|
[1] | 39 | { |
---|
| 40 | $this->PHPUnit_TestCase($name); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | function setUp() |
---|
[136] | 44 | { |
---|
[1] | 45 | require dirname(__FILE__) . '/_config.inc.php'; |
---|
[137] | 46 | require_once '../lib/Version.inc.php'; |
---|
[1] | 47 | |
---|
| 48 | require_once '../lib/Auth_SQL.inc.php'; |
---|
| 49 | $this->Auth_SQL =& new Auth_SQL('testauth'); |
---|
| 50 | $this->Auth_SQL->setParam(array( |
---|
| 51 | 'db_table' => 'test_user_tbl', |
---|
| 52 | 'db_primary_key' => 'user_id', |
---|
| 53 | 'db_login_table' => 'test_login_tbl', |
---|
| 54 | 'login_url' => '/login.php', |
---|
| 55 | 'blocking' => true |
---|
| 56 | )); |
---|
| 57 | |
---|
| 58 | // Use fresh user table. |
---|
| 59 | $this->Auth_SQL->initDB(true); |
---|
[42] | 60 | |
---|
[1] | 61 | // Insert test data. |
---|
[136] | 62 | $db =& DB::getInstance(); |
---|
| 63 | $db->query(" |
---|
[1] | 64 | INSERT INTO test_user_tbl ( |
---|
| 65 | username, |
---|
| 66 | userpass, |
---|
| 67 | first_name, |
---|
| 68 | last_name, |
---|
[357] | 69 | email |
---|
[1] | 70 | ) VALUES ( |
---|
| 71 | 'testuser', |
---|
| 72 | md5('testpass'), |
---|
| 73 | 'John', |
---|
| 74 | 'Doe', |
---|
[357] | 75 | 'john@example.com' |
---|
[1] | 76 | ) |
---|
| 77 | "); |
---|
[42] | 78 | |
---|
[1] | 79 | $this->Auth_SQL->login('testuser', 'testpass'); |
---|
[42] | 80 | |
---|
[137] | 81 | $this->Version =& Version::getInstance($this->Auth_SQL); |
---|
| 82 | $this->Version->setParam(array('db_table' => 'test_version_tbl')); |
---|
[1] | 83 | |
---|
| 84 | // Use fresh version table. |
---|
[137] | 85 | $this->Version->initDB(true); |
---|
[1] | 86 | } |
---|
| 87 | |
---|
| 88 | function tearDown() |
---|
| 89 | { |
---|
[136] | 90 | $db =& DB::getInstance(); |
---|
| 91 | |
---|
[137] | 92 | unset($this->Version); |
---|
[1] | 93 | unset($this->Auth_SQL); |
---|
[136] | 94 | $db->query("DROP TABLE IF EXISTS test_user_tbl"); |
---|
| 95 | $db->query("DROP TABLE IF EXISTS test_login_tbl"); |
---|
| 96 | $db->query("DROP TABLE IF EXISTS test_version_tbl"); |
---|
[1] | 97 | } |
---|
| 98 | |
---|
| 99 | function test_getinstance() |
---|
| 100 | { |
---|
[137] | 101 | $result = Version::getinstance($this->Auth_SQL); |
---|
| 102 | $this->assertTrue($result && is_a($result, 'Version')); |
---|
[1] | 103 | } |
---|
| 104 | |
---|
| 105 | function test_setparam() |
---|
| 106 | { |
---|
[137] | 107 | $this->Version->setparam(array('var'=>'val')); |
---|
[1] | 108 | } |
---|
| 109 | |
---|
| 110 | function test_getparam() |
---|
| 111 | { |
---|
[137] | 112 | $this->Version->getparam('var'); |
---|
[1] | 113 | } |
---|
| 114 | |
---|
| 115 | function test_create() |
---|
| 116 | { |
---|
[137] | 117 | $result = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); |
---|
[1] | 118 | $this->assertTrue($result); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | function test_restore() |
---|
| 122 | { |
---|
[137] | 123 | $current = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); |
---|
| 124 | $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); |
---|
| 125 | $versioned = $this->Version->getData($version_id); |
---|
| 126 | $result = $this->Version->restore($version_id); |
---|
| 127 | $restored = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); |
---|
[1] | 128 | $this->assertTrue(is_array($result), 'Restore did not return array.'); |
---|
| 129 | $this->assertTrue($current === $versioned && $versioned === $restored, 'Restored version does not match original.'); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | function test_deleteold() |
---|
| 133 | { |
---|
| 134 | // Creat 3 test versions. |
---|
[137] | 135 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user'); |
---|
| 136 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user'); |
---|
| 137 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user'); |
---|
[1] | 138 | |
---|
[137] | 139 | $a = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
[1] | 140 | |
---|
[137] | 141 | $this->Version->deleteold('test_user_tbl', 'user_id', '1'); |
---|
| 142 | $b = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
[1] | 143 | |
---|
[137] | 144 | $this->Version->setparam(array('max_qty'=>2)); |
---|
| 145 | $this->Version->setparam(array('min_qty'=>1)); |
---|
| 146 | $this->Version->setparam(array('min_days'=>0)); |
---|
| 147 | $this->Version->deleteold('test_user_tbl', 'user_id', '1'); |
---|
| 148 | $c = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
[1] | 149 | |
---|
[137] | 150 | $this->Version->setparam(array('max_qty'=>0)); |
---|
| 151 | $this->Version->setparam(array('min_qty'=>0)); |
---|
| 152 | $this->Version->setparam(array('min_days'=>0)); |
---|
| 153 | $this->Version->deleteold('test_user_tbl', 'user_id', '1'); |
---|
| 154 | $d = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
[1] | 155 | |
---|
| 156 | $this->assertTrue(sizeof($a) == 3, 'A is wrong'); |
---|
| 157 | $this->assertTrue(sizeof($b) == 3, 'B is wrong'); |
---|
| 158 | $this->assertTrue(sizeof($c) == 1, 'C is wrong'); |
---|
| 159 | $this->assertTrue(sizeof($d) == 0, 'D is wrong'); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | function test_getlist() |
---|
| 163 | { |
---|
[137] | 164 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user'); |
---|
| 165 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user'); |
---|
| 166 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user'); |
---|
| 167 | $result = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
[1] | 168 | $this->assertTrue(is_array($result) && !empty($result)); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | function test_getverson() |
---|
| 172 | { |
---|
[137] | 173 | $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); |
---|
| 174 | $result = $this->Version->getverson($version_id); |
---|
[1] | 175 | $this->assertTrue(is_array($result) && !empty($result)); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | function test_getdata() |
---|
| 179 | { |
---|
[137] | 180 | $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); |
---|
| 181 | $result = $this->Version->getdata($version_id); |
---|
[1] | 182 | $this->assertTrue(is_array($result) && !empty($result)); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | function test_getcurrent() |
---|
| 186 | { |
---|
[137] | 187 | $result = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); |
---|
[1] | 188 | $this->assertTrue(is_array($result) && !empty($result)); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | } |
---|
| 192 | // Running the test. |
---|
[137] | 193 | $suite = new PHPUnit_TestSuite('VersionTest'); |
---|
[1] | 194 | $result = PHPUnit::run($suite); |
---|
| 195 | echo $result->toString(); |
---|
| 196 | ?> |
---|