source: trunk/tests/VersionTest.php @ 137

Last change on this file since 137 was 137, checked in by scdev, 18 years ago

Q - Renamed SessionCache? to Cache, RecordVersion? to Version, and RecordLock? to Lock

File size: 6.0 KB
Line 
1<?php
2/**
3 * PHPUnit test case for Version
4 *
5 * The method skeletons below need to be filled in with
6 * real data so that the tests will run correctly. Replace
7 * all EXPECTED_VAL and PARAM strings with real data.
8 *
9 * Created with PHPUnit_Skeleton on 2005-08-09
10 */
11require_once 'PHPUnit.php';
12class VersionTest extends PHPUnit_TestCase {
13
14    var $Version;
15    var $Auth_SQL;
16
17    function VersionTest($name)
18    {
19        $this->PHPUnit_TestCase($name);
20    }
21
22    function setUp()
23    {   
24        require dirname(__FILE__) . '/_config.inc.php';
25        require_once '../lib/Version.inc.php';
26
27        require_once '../lib/Auth_SQL.inc.php';
28        $this->Auth_SQL =& new Auth_SQL('testauth');
29        $this->Auth_SQL->setParam(array(
30            'db_table'          => 'test_user_tbl',
31            'db_primary_key'    => 'user_id',
32            'db_login_table'    => 'test_login_tbl',
33            'login_url'         => '/login.php',
34            'blocking'          => true
35        ));
36
37        // Use fresh user table.
38        $this->Auth_SQL->initDB(true);
39
40        // Insert test data.
41        $db =& DB::getInstance();
42        $db->query("
43            INSERT INTO test_user_tbl (
44                username,
45                userpass,
46                first_name,
47                last_name,
48                email,
49                user_type
50            ) VALUES (
51                'testuser',
52                md5('testpass'),
53                'John',
54                'Doe',
55                'john@example.com',
56                'admin'
57            )
58        ");
59
60        $this->Auth_SQL->login('testuser', 'testpass');
61
62        $this->Version =& Version::getInstance($this->Auth_SQL);
63        $this->Version->setParam(array('db_table' => 'test_version_tbl'));
64
65        // Use fresh version table.
66        $this->Version->initDB(true);
67    }
68
69    function tearDown()
70    {
71        $db =& DB::getInstance();
72   
73        unset($this->Version);
74        unset($this->Auth_SQL);
75        $db->query("DROP TABLE IF EXISTS test_user_tbl");
76        $db->query("DROP TABLE IF EXISTS test_login_tbl");
77        $db->query("DROP TABLE IF EXISTS test_version_tbl");
78    }
79
80    function test_getinstance()
81    {
82        $result = Version::getinstance($this->Auth_SQL);
83        $this->assertTrue($result && is_a($result, 'Version'));
84    }
85
86    function test_setparam()
87    {
88        $this->Version->setparam(array('var'=>'val'));
89    }
90
91    function test_getparam()
92    {
93        $this->Version->getparam('var');
94    }
95
96    function test_create()
97    {
98        $result = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
99        $this->assertTrue($result);
100    }
101
102    function test_restore()
103    {
104        $current = $this->Version->getCurrent('test_user_tbl', 'user_id', '1');
105        $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
106        $versioned = $this->Version->getData($version_id);
107        $result = $this->Version->restore($version_id);
108        $restored = $this->Version->getCurrent('test_user_tbl', 'user_id', '1');
109        $this->assertTrue(is_array($result), 'Restore did not return array.');
110        $this->assertTrue($current === $versioned && $versioned === $restored, 'Restored version does not match original.');
111    }
112
113    function test_deleteold()
114    {
115        // Creat 3 test versions.
116        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user');
117        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user');
118        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user');
119
120        $a = $this->Version->getList('test_user_tbl', 'user_id', '1');
121
122        $this->Version->deleteold('test_user_tbl', 'user_id', '1');
123        $b = $this->Version->getList('test_user_tbl', 'user_id', '1');
124
125        $this->Version->setparam(array('max_qty'=>2));
126        $this->Version->setparam(array('min_qty'=>1));
127        $this->Version->setparam(array('min_days'=>0));
128        $this->Version->deleteold('test_user_tbl', 'user_id', '1');
129        $c = $this->Version->getList('test_user_tbl', 'user_id', '1');
130
131        $this->Version->setparam(array('max_qty'=>0));
132        $this->Version->setparam(array('min_qty'=>0));
133        $this->Version->setparam(array('min_days'=>0));
134        $this->Version->deleteold('test_user_tbl', 'user_id', '1');
135        $d = $this->Version->getList('test_user_tbl', 'user_id', '1');
136
137        $this->assertTrue(sizeof($a) == 3, 'A is wrong');
138        $this->assertTrue(sizeof($b) == 3, 'B is wrong');
139        $this->assertTrue(sizeof($c) == 1, 'C is wrong');
140        $this->assertTrue(sizeof($d) == 0, 'D is wrong');
141    }
142
143    function test_getlist()
144    {
145        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user');
146        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user');
147        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user');
148        $result = $this->Version->getList('test_user_tbl', 'user_id', '1');
149        $this->assertTrue(is_array($result) && !empty($result));
150    }
151
152    function test_getverson()
153    {
154        $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
155        $result = $this->Version->getverson($version_id);
156        $this->assertTrue(is_array($result) && !empty($result));
157    }
158
159    function test_getdata()
160    {
161        $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
162        $result = $this->Version->getdata($version_id);
163        $this->assertTrue(is_array($result) && !empty($result));
164    }
165
166    function test_getcurrent()
167    {
168        $result = $this->Version->getCurrent('test_user_tbl', 'user_id', '1');
169        $this->assertTrue(is_array($result) && !empty($result));
170    }
171
172}
173// Running the test.
174$suite = new PHPUnit_TestSuite('VersionTest');
175$result = PHPUnit::run($suite);
176echo $result->toString();
177?>
Note: See TracBrowser for help on using the repository browser.