source: trunk/tests/VersionTest.php @ 169

Last change on this file since 169 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
RevLine 
[1]1<?php
2/**
[137]3 * PHPUnit test case for Version
[42]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 *
[1]9 * Created with PHPUnit_Skeleton on 2005-08-09
10 */
11require_once 'PHPUnit.php';
[137]12class VersionTest extends PHPUnit_TestCase {
[1]13
[137]14    var $Version;
[1]15    var $Auth_SQL;
16
[137]17    function VersionTest($name)
[1]18    {
19        $this->PHPUnit_TestCase($name);
20    }
21
22    function setUp()
[136]23    {   
[1]24        require dirname(__FILE__) . '/_config.inc.php';
[137]25        require_once '../lib/Version.inc.php';
[1]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);
[42]39
[1]40        // Insert test data.
[136]41        $db =& DB::getInstance();
42        $db->query("
[1]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        ");
[42]59
[1]60        $this->Auth_SQL->login('testuser', 'testpass');
[42]61
[137]62        $this->Version =& Version::getInstance($this->Auth_SQL);
63        $this->Version->setParam(array('db_table' => 'test_version_tbl'));
[1]64
65        // Use fresh version table.
[137]66        $this->Version->initDB(true);
[1]67    }
68
69    function tearDown()
70    {
[136]71        $db =& DB::getInstance();
72   
[137]73        unset($this->Version);
[1]74        unset($this->Auth_SQL);
[136]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");
[1]78    }
79
80    function test_getinstance()
81    {
[137]82        $result = Version::getinstance($this->Auth_SQL);
83        $this->assertTrue($result && is_a($result, 'Version'));
[1]84    }
85
86    function test_setparam()
87    {
[137]88        $this->Version->setparam(array('var'=>'val'));
[1]89    }
90
91    function test_getparam()
92    {
[137]93        $this->Version->getparam('var');
[1]94    }
95
96    function test_create()
97    {
[137]98        $result = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
[1]99        $this->assertTrue($result);
100    }
101
102    function test_restore()
103    {
[137]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');
[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.
[137]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');
[1]119
[137]120        $a = $this->Version->getList('test_user_tbl', 'user_id', '1');
[1]121
[137]122        $this->Version->deleteold('test_user_tbl', 'user_id', '1');
123        $b = $this->Version->getList('test_user_tbl', 'user_id', '1');
[1]124
[137]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');
[1]130
[137]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');
[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    {
[137]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');
[1]149        $this->assertTrue(is_array($result) && !empty($result));
150    }
151
152    function test_getverson()
153    {
[137]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);
[1]156        $this->assertTrue(is_array($result) && !empty($result));
157    }
158
159    function test_getdata()
160    {
[137]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);
[1]163        $this->assertTrue(is_array($result) && !empty($result));
164    }
165
166    function test_getcurrent()
167    {
[137]168        $result = $this->Version->getCurrent('test_user_tbl', 'user_id', '1');
[1]169        $this->assertTrue(is_array($result) && !empty($result));
170    }
171
172}
173// Running the test.
[137]174$suite = new PHPUnit_TestSuite('VersionTest');
[1]175$result = PHPUnit::run($suite);
176echo $result->toString();
177?>
Note: See TracBrowser for help on using the repository browser.