FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_common_cache.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
14 
15 require_once(dirname(__FILE__, 2) . '/common-cache.php');
16 require_once(dirname(__FILE__, 2) . '/common-db.php');
17 
21 class test_common_cached extends \PHPUnit\Framework\TestCase
22 {
24  private $testDb;
25 
27  private $dbManager;
28 
29  public $upload_pk = 0;
30  public $uploadtree_pk = 0;
31  public $UserCacheStat = 0;
32 
36  protected function setUp() : void
37  {
38  $this->testDb = new TestPgDb("fosslibtest");
39  $tables = array('upload', 'uploadtree', 'report_cache');
40  $this->testDb->createPlainTables($tables);
41  $sequences = array('upload_upload_pk_seq', 'uploadtree_uploadtree_pk_seq', 'report_cache_report_cache_pk_seq');
42  $this->testDb->createSequences($sequences);
43  $this->testDb->createConstraints(['upload_pkey_idx', 'ufile_rel_pkey', 'report_cache_pkey', 'report_cache_report_cache_key_key']);
44  $this->testDb->alterTables($tables);
45 
46  global $upload_pk;
47  global $uploadtree_pk;
48  global $UserCacheStat;
49 
50  $this->dbManager = $this->testDb->getDbManager();
51  }
52 
57  {
58  global $upload_pk;
59  global $uploadtree_pk;
60 
63  $sql = "INSERT INTO upload (upload_filename,upload_mode,upload_ts) VALUES ('cache_test',40,now());";
64  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "insert.upload");
65 
67  $sql = "SELECT upload_pk from upload where upload_filename = 'cache_test';";
68  $row = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "upload.select");
69  $upload_pk= $row['upload_pk'];
70 
72  $sql= "INSERT INTO uploadtree (upload_fk) VALUES($upload_pk)";
73  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "insert.uploadtree");
74 
76  $sql = "SELECT uploadtree_pk from uploadtree where upload_fk = $upload_pk;";
77  $row = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "uploadtree.select");
78  $uploadtree_pk= $row['uploadtree_pk'];
79  }
80 
85  {
86  print "Start unit test for common-cache.php\n";
87  print "test function ReportCachePut()\n";
88  global $upload_pk;
89  global $uploadtree_pk;
90 
92 
93  $CacheKey = "?mod=nomoslicense&upload=$upload_pk&item=$uploadtree_pk&show=detail";
94  $CacheValue = "no data";
95  ReportCachePut($CacheKey, $CacheValue);
97  $sql = "SELECT report_cache_value from report_cache where report_cache_uploadfk = $upload_pk;";
98  $row = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "select.report_cache");
99  $value = $row['report_cache_value'];
100  $this->assertEquals($CacheValue, $value);
101  $this->resetEnv4ReportCachePut();
102  }
103 
108  {
109  print "test function ReportCachePut()\n";
110  global $upload_pk;
111  global $uploadtree_pk;
112 
114 
115  $CacheKey = "?mod=nomoslicense&item=$uploadtree_pk&show=detail";
116  $CacheValue = "no data";
117  ReportCachePut($CacheKey, $CacheValue);
119  $sql = "SELECT report_cache_value from report_cache where report_cache_uploadfk = $upload_pk;";
120  $row = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "select.report_cache");
121  $value = $row['report_cache_value'];
122  $this->assertEquals($CacheValue, $value);
123  $this->resetEnv4ReportCachePut();
124  }
125 
130  {
131  print "test function ReportCacheGet()\n";
132  global $upload_pk;
133  global $uploadtree_pk;
134  global $UserCacheStat;
135 
137 
139  $CacheKey = "?mod=nomoslicense&upload=$upload_pk&item=$uploadtree_pk&show=detail";
140  $CacheValue = "no data";
141  ReportCachePut($CacheKey, $CacheValue);
143  $UserCacheStat = 2;
144  $value = ReportCacheGet($CacheKey);
145  $this->assertEquals($CacheValue, $value);
146  $this->resetEnv4ReportCachePut();
147  print "unit test for common-cache.php end\n";
148  }
149 
154  {
155  global $upload_pk;
158  $sql = "DELETE from report_cache where report_cache_uploadfk = $upload_pk;";
159  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "delete.report_cache");
161  $sql= "DELETE from uploadtree where upload_fk = $upload_pk;";
162  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "delete.uploadtree");
164  $sql = "DELETE from upload where upload_pk = $upload_pk;";
165  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "delete.upload");
166  }
167 
171  protected function tearDown() : void
172  {
173  if (!is_callable('pg_connect')) {
174  return;
175  }
176  $this->testDb->fullDestruct();
177  $this->testDb = null;
178  $this->dbManager = null;
179  }
180 }
setUp()
initialization
resetEnv4ReportCachePut()
reset enviroment after testing ReportCachePut
testReportCachePut_upload_id_null()
test for ReportCachePut upload id is not in $CacheKey
preparation4ReportCachePut()
preparation for testing ReportCachePut
testReportCacheGet()
test for ReportCacheGet
tearDown()
clean the env
testReportCachePut_upload_id_not_null()
test for ReportCachePut upload id is in $CacheKey
ReportCacheGet($CacheKey)
This function is used by Output() to see if the requested report is in the report cache.
ReportCachePut($CacheKey, $CacheValue)
This function is used to write a record to the report cache. If the record already exists,...
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16