source: tags/2.1.5/tests/VersionTest.php @ 377

Last change on this file since 377 was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 6.9 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2010 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
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 *
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 *
30 * Created with PHPUnit_Skeleton on 2005-08-09
31 */
32require_once 'PHPUnit.php';
33class VersionTest extends PHPUnit_TestCase {
34
35    var $Version;
36    var $Auth_SQL;
37
38    function VersionTest($name)
39    {
40        $this->PHPUnit_TestCase($name);
41    }
42
43    function setUp()
44    {   
45        require dirname(__FILE__) . '/_config.inc.php';
46        require_once '../lib/Version.inc.php';
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);
60
61        // Insert test data.
62        $db =& DB::getInstance();
63        $db->query("
64            INSERT INTO test_user_tbl (
65                username,
66                userpass,
67                first_name,
68                last_name,
69                email
70            ) VALUES (
71                'testuser',
72                md5('testpass'),
73                'John',
74                'Doe',
75                'john@example.com'
76            )
77        ");
78
79        $this->Auth_SQL->login('testuser', 'testpass');
80
81        $this->Version =& Version::getInstance($this->Auth_SQL);
82        $this->Version->setParam(array('db_table' => 'test_version_tbl'));
83
84        // Use fresh version table.
85        $this->Version->initDB(true);
86    }
87
88    function tearDown()
89    {
90        $db =& DB::getInstance();
91   
92        unset($this->Version);
93        unset($this->Auth_SQL);
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");
97    }
98
99    function test_getinstance()
100    {
101        $result = Version::getinstance($this->Auth_SQL);
102        $this->assertTrue($result && is_a($result, 'Version'));
103    }
104
105    function test_setparam()
106    {
107        $this->Version->setparam(array('var'=>'val'));
108    }
109
110    function test_getparam()
111    {
112        $this->Version->getparam('var');
113    }
114
115    function test_create()
116    {
117        $result = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
118        $this->assertTrue($result);
119    }
120
121    function test_restore()
122    {
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');
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.
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');
138
139        $a = $this->Version->getList('test_user_tbl', 'user_id', '1');
140
141        $this->Version->deleteold('test_user_tbl', 'user_id', '1');
142        $b = $this->Version->getList('test_user_tbl', 'user_id', '1');
143
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');
149
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');
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    {
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');
168        $this->assertTrue(is_array($result) && !empty($result));
169    }
170
171    function test_getverson()
172    {
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);
175        $this->assertTrue(is_array($result) && !empty($result));
176    }
177
178    function test_getdata()
179    {
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);
182        $this->assertTrue(is_array($result) && !empty($result));
183    }
184
185    function test_getcurrent()
186    {
187        $result = $this->Version->getCurrent('test_user_tbl', 'user_id', '1');
188        $this->assertTrue(is_array($result) && !empty($result));
189    }
190
191}
192// Running the test.
193$suite = new PHPUnit_TestSuite('VersionTest');
194$result = PHPUnit::run($suite);
195echo $result->toString();
196?>
Note: See TracBrowser for help on using the repository browser.