FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_common_cache.php
Go to the documentation of this file.
1 <?php
2 /*
3  Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
24 require_once(dirname(__FILE__) . '/../common-cache.php');
25 require_once(dirname(__FILE__) . '/../common-db.php');
26 
30 class test_common_cached extends \PHPUnit\Framework\TestCase
31 {
32  public $PG_CONN;
33  public $upload_pk = 0;
34  public $uploadtree_pk = 0;
35  public $UserCacheStat = 0;
36 
40  protected function setUp()
41  {
42  global $PG_CONN;
43  $sysconfig = dirname(__FILE__).'/sysconfigDirTest';
44  if (!is_callable('pg_connect')) {
45  $this->markTestSkipped("php-psql not found");
46  }
47  $PG_CONN = DBconnect($sysconfig);
48  }
49 
54  {
55  global $PG_CONN;
56  global $upload_pk;
57  global $uploadtree_pk;
58 
61  $sql = "INSERT INTO upload (upload_filename,upload_mode,upload_ts) VALUES ('cache_test',40,now());";
62  $result = pg_query($PG_CONN, $sql);
63  DBCheckResult($result, $sql, __FILE__, __LINE__);
64  pg_free_result($result);
65 
67  $sql = "SELECT upload_pk from upload where upload_filename = 'cache_test';";
68  $result = pg_query($PG_CONN, $sql);
69  DBCheckResult($result, $sql, __FILE__, __LINE__);
70  $row = pg_fetch_assoc($result);
71  $upload_pk= $row['upload_pk'];
72  pg_free_result($result);
73 
75  $sql= "INSERT INTO uploadtree (upload_fk) VALUES($upload_pk)";
76  $result = pg_query($PG_CONN, $sql);
77  DBCheckResult($result, $sql, __FILE__, __LINE__);
78  pg_free_result($result);
79 
81  $sql = "SELECT uploadtree_pk from uploadtree where upload_fk = $upload_pk;";
82  $result = pg_query($PG_CONN, $sql);
83  DBCheckResult($result, $sql, __FILE__, __LINE__);
84  $row = pg_fetch_assoc($result);
85  $uploadtree_pk= $row['uploadtree_pk'];
86  pg_free_result($result);
87  }
88 
93  {
94  print "Start unit test for common-cache.php\n";
95  print "test function ReportCachePut()\n";
96  global $PG_CONN;
97  global $upload_pk;
98  global $uploadtree_pk;
99 
101 
102  $CacheKey = "?mod=nomoslicense&upload=$upload_pk&item=$uploadtree_pk&show=detail";
103  $CacheValue = "no data";
104  ReportCachePut($CacheKey, $CacheValue);
106  $sql = "SELECT report_cache_value from report_cache where report_cache_uploadfk = $upload_pk;";
107  $result = pg_query($PG_CONN, $sql);
108  DBCheckResult($result, $sql, __FILE__, __LINE__);
109  $row = pg_fetch_assoc($result);
110  $value = $row['report_cache_value'];
111  pg_free_result($result);
112  $this->assertEquals($CacheValue, $value);
113  $this->resetEnv4ReportCachePut();
114  }
115 
120  {
121  print "test function ReportCachePut()\n";
122  global $PG_CONN;
123  global $upload_pk;
124  global $uploadtree_pk;
125 
127 
128  $CacheKey = "?mod=nomoslicense&item=$uploadtree_pk&show=detail";
129  $CacheValue = "no data";
130  ReportCachePut($CacheKey, $CacheValue);
132  $sql = "SELECT report_cache_value from report_cache where report_cache_uploadfk = $upload_pk;";
133  $result = pg_query($PG_CONN, $sql);
134  DBCheckResult($result, $sql, __FILE__, __LINE__);
135  $row = pg_fetch_assoc($result);
136  $value = $row['report_cache_value'];
137  pg_free_result($result);
138  $this->assertEquals($CacheValue, $value);
139  $this->resetEnv4ReportCachePut();
140  }
141 
146  {
147  print "test function ReportCacheGet()\n";
148  global $PG_CONN;
149  global $upload_pk;
150  global $uploadtree_pk;
151  global $UserCacheStat;
152 
154 
156  $CacheKey = "?mod=nomoslicense&upload=$upload_pk&item=$uploadtree_pk&show=detail";
157  $CacheValue = "no data";
158  ReportCachePut($CacheKey, $CacheValue);
160  $UserCacheStat = 2;
161  $value = ReportCacheGet($CacheKey);
162  $this->assertEquals($CacheValue, $value);
163  $this->resetEnv4ReportCachePut();
164  print "unit test for common-cache.php end\n";
165  }
166 
171  {
172  global $PG_CONN;
173  global $upload_pk;
176  $sql = "DELETE from report_cache where report_cache_uploadfk = $upload_pk;";
177  $result = pg_query($PG_CONN, $sql);
178  DBCheckResult($result, $sql, __FILE__, __LINE__);
179  pg_free_result($result);
181  $sql= "DELETE from uploadtree where upload_fk = $upload_pk;";
182  $result = pg_query($PG_CONN, $sql);
183  DBCheckResult($result, $sql, __FILE__, __LINE__);
184  pg_free_result($result);
186  $sql = "DELETE from upload where upload_pk = $upload_pk;";
187  $result = pg_query($PG_CONN, $sql);
188  DBCheckResult($result, $sql, __FILE__, __LINE__);
189  pg_free_result($result);
190  }
191 
195  protected function tearDown()
196  {
197  global $PG_CONN;
199  if (is_callable('pg_close')) {
200  pg_close($PG_CONN);
201  }
202  }
203 }
testReportCachePut_upload_id_null()
test for ReportCachePut upload id is not in $CacheKey
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
Definition: common-db.php:44
ReportCachePut($CacheKey, $CacheValue)
This function is used to write a record to the report cache. If the record already exists...
setUp()
initialization
preparation4ReportCachePut()
preparation for testing ReportCachePut
testReportCachePut_upload_id_not_null()
test for ReportCachePut upload id is in $CacheKey
resetEnv4ReportCachePut()
reset enviroment after testing ReportCachePut
tearDown()
clean the env
ReportCacheGet($CacheKey)
This function is used by Output() to see if the requested report is in the report cache...
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
testReportCacheGet()
test for ReportCacheGet