source: tags/2.1.5/tests/LockTest.php

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

Releasing trunk as stable version 2.1.5

File size: 6.5 KB
RevLine 
[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/Lock.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 */
32require_once 'PHPUnit.php';
[137]33class LockTest extends PHPUnit_TestCase {
[1]34
[137]35    var $Lock;
[1]36    var $Auth_SQL;
37
[137]38    function LockTest($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/Lock.inc.php';
[1]47        require_once '../lib/Auth_SQL.inc.php';
48
[136]49        $this->Auth_SQL =& new Auth_SQL('test');
[1]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.
[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',
[136]72                '" . $this->Auth_SQL->encryptPassword('testpass') . "',
[1]73                'John',
74                'Doe',
[357]75                'john@example.com'
[1]76            )
77        ");
78        $this->Auth_SQL->login('testuser', 'testpass');
79
[136]80        if (!$this->Auth_SQL->isLoggedIn()) {
81            trigger_error("User login failed...tests canceled.", E_USER_ERROR);
82        }
83
[137]84        $this->Lock =& Lock::getInstance($this->Auth_SQL);
85        $this->Lock->setParam(array('db_table' => 'test_lock_tbl'));
[1]86
87        // Use fresh lock table.
[137]88        $this->Lock->initDB(true);
[1]89    }
90
91    function tearDown()
92    {
[136]93        $db =& DB::getInstance();
94   
[137]95        unset($this->Lock);
[1]96        unset($this->Auth_SQL);
[136]97        $db->query("DROP TABLE IF EXISTS test_user_tbl");
98        $db->query("DROP TABLE IF EXISTS test_login_tbl");
99        $db->query("DROP TABLE IF EXISTS test_lock_tbl");
[1]100    }
101
102    function test_getinstance()
103    {
[137]104        $result = Lock::getinstance($this->Auth_SQL);
105        $this->assertTrue($result && is_a($result, 'Lock'));
[1]106    }
107
108    function test_setparam()
109    {
[137]110        $this->Lock->setparam(array('var'=>'val'));
[1]111    }
112
113    function test_getparam()
114    {
[137]115        $this->Lock->getparam('var');
[1]116    }
117
118    function test_select()
119    {
[137]120        $this->Lock->set('test_user_tbl', 'user_id', '1');
121        $result = $this->Lock->select('test_user_tbl', 'user_id', '1');
[1]122        $this->assertTrue($result);
123    }
124
125    function test_islocked()
126    {
[137]127        $this->Lock->select('test_user_tbl', 'user_id', '1');
[1]128
[137]129        $this->Lock->set('test_user_tbl', 'user_id', '1');
130        $result = $this->Lock->islocked();
[1]131        $this->assertTrue($result, 'Lock was not set.');
132
[137]133        $this->Lock->remove();
134        $this->Lock->select('test_user_tbl', 'user_id', '1');
135        $result = $this->Lock->islocked();
[1]136        $this->assertFalse($result, 'Lock was not removed.');
137    }
138
139    function test_ismine()
140    {
[137]141        $this->Lock->set('test_user_tbl', 'user_id', '1');
142        $this->Lock->select('test_user_tbl', 'user_id', '1');
143        $result = $this->Lock->ismine();
[1]144        $this->assertTrue($result);
145    }
146
147    function test_set()
148    {
[137]149        $this->Lock->select('test_user_tbl', 'user_id', '1');
150        $this->Lock->remove();
151        $this->Lock->set('test_user_tbl', 'user_id', '1');
152        $this->Lock->select('test_user_tbl', 'user_id', '1');
153        $result = $this->Lock->islocked();
[1]154        $this->assertTrue($result);
155    }
156
157    function test_remove()
158    {
[137]159        $this->Lock->select('test_user_tbl', 'user_id', '1');
160        $this->Lock->set('test_user_tbl', 'user_id', '1');
161        $this->Lock->remove();
162        $this->Lock->select('test_user_tbl', 'user_id', '1');
163        $result = $this->Lock->islocked();
[1]164        $this->assertFalse($result);
165    }
166
167    function test_removeall()
168    {
[137]169        $this->Lock->select('test_user_tbl', 'user_id', '1');
170        $this->Lock->set('test_user_tbl', 'user_id', '1');
171        $this->Lock->removeall();
172        $this->Lock->select('test_user_tbl', 'user_id', '1');
173        $result = $this->Lock->islocked();
[1]174        $this->assertFalse($result);
175    }
176
177    function test__auto_timeout()
178    {
[137]179        $this->Lock->_auto_timeout();
[1]180    }
181
182    function test_getid()
183    {
[137]184        $this->Lock->select('test_user_tbl', 'user_id', '1');
185        $this->Lock->set('test_user_tbl', 'user_id', '1');
186        $result = $this->Lock->getid();
[1]187        $this->assertTrue('' != $result);
188        $this->assertTrue(is_numeric($result));
189    }
190
191    function test_gettitle()
192    {
[137]193        $this->Lock->select('test_user_tbl', 'user_id', '1');
194        $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock');
195        $result = $this->Lock->gettitle();
[1]196        $this->assertEquals('userlock', $result);
197    }
198
199    function test_geteditor()
200    {
[137]201        $this->Lock->select('test_user_tbl', 'user_id', '1');
202        $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock');
203        $result = $this->Lock->geteditor();
[1]204        $this->assertEquals('testuser', $result);
205    }
206
207    function test_getsecondselapsed()
208    {
[137]209        $result = $this->Lock->getsecondselapsed();
[1]210        $this->assertType('integer', $result);
211    }
212
213}
214// Running the test.
[137]215$suite = new PHPUnit_TestSuite('LockTest');
[1]216$result = PHPUnit::run($suite);
217echo $result->toString();
218?>
Note: See TracBrowser for help on using the repository browser.