Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_getResPhpFunc.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package Dcp\Pu
5 */
6 
7 namespace Dcp\Pu;
8 
9 require_once 'PU_testcase_dcp.php';
10 include_once 'WHAT/Lib.Http.php';
11 include_once 'FDL/enum_choice.php';
12 
14 {
15  static public $externalsList = array();
16 
17  public static function setUpBeforeClass()
18  {
19  parent::setUpBeforeClass();
20 
21  self::addExternals("PU_data_dcp_getResPhpFunc.php");
22 
23  self::connectUser();
24  self::beginTransaction();
25  self::importDocument("PU_data_dcp_getResPhpFunc_family.ods");
26  }
27 
28  public static function tearDownAfterClass()
29  {
30  self::rollbackTransaction();
31 
32  self::rollbackExternals();
33 
34  parent::tearDownAfterClass();
35  }
36 
37  public function setUp()
38  {
39  return;
40  }
41 
42  public function tearDown()
43  {
44  return;
45  }
46 
47  public static function addExternals($file)
48  {
49  $source = '..' . DIRECTORY_SEPARATOR . 'DCPTEST' . DIRECTORY_SEPARATOR . $file;
50  $destination = 'EXTERNALS' . DIRECTORY_SEPARATOR . basename($file);
51  /* Check that the destination does not already exists */
52  if (file_exists($destination)) {
53  throw new \Exception(sprintf("External destination file '%s' already exists.", $destination));
54  }
55  /* Create the symlink */
56  $ret = symlink($source, $destination);
57  if ($ret === false) {
58  self::rollbackExternals();
59  throw new \Exception(sprintf("Could not symlink '%s' into '%s'.", $source, $destination));
60  }
61 
62  self::$externalsList[] = $destination;
63  }
64 
65  public static function rollbackExternals()
66  {
67  foreach (self::$externalsList as $file) {
68  if (is_link($file)) {
69  unlink($file);
70  }
71  }
72  }
73  /**
74  * @dataProvider dataGetResPhpFunc
75  */
77  {
78  global $ZONE_ARGS;
79  $ZONE_ARGS = array();
80 
81  $doc = false;
82  if (isset($data['document'])) {
83  $doc = new_Doc(self::$dbaccess, $data['document']);
84  $this->assertTrue(is_object($doc) , sprintf("Error retrieving document '%s'.", $data['document']));
85  } else {
87  $this->assertTrue(is_object($doc) , sprintf("Could not create new document from family '%s'.", $data['fam']));
88  $err = $doc->add();
89  $this->assertEmpty($err, sprintf("Could not add new document to database: %s", $err));
90  }
91 
92  $oattr = $doc->getAttribute($data['attr']);
93  $this->assertTrue(is_object($oattr) , sprintf("Could not get attribute '%s' on document '%s' (id=%s).", $data['attr'], $doc->name, $doc->id));
94 
95  if (isset($data['http:vars'])) {
96  foreach ($data['http:vars'] as $name => $value) {
97  SetHttpVar($name, $value);
98  }
99  }
100 
101  $rargids = array();
102  $tselect = array();
103  $tval = array();
104  $res = getResPhpFunc($doc, $oattr, $rargids, $tselect, $tval);
105  $this->assertTrue(is_array($res) , sprintf("getResPhpFunc did not returned an array."));
106 
107  if (isset($data['expected:results'])) {
108  $err = $this->checkResult($data['expected:results'], $res);
109  $this->assertEmpty($err, sprintf("Expected results not met: %s", $err));
110  }
111 
112  if (isset($data['expected:rargids'])) {
113  $err = $this->checkRargids($data['expected:rargids'], $rargids);
114  $this->assertEmpty($err, sprintf("Expected rargids not met: %s", $err));
115  }
116  }
117  /**
118  * @dataProvider dataBadGetResPhpFunc
119  */
120  public function testGetResPhpFuncError($family, $attrid, array $inputs, array $expectedErrors)
121  {
122  global $ZONE_ARGS;
123  $ZONE_ARGS = array();
124 
125  $doc = createDoc(self::$dbaccess, $family);
126  $this->assertTrue(is_object($doc) , sprintf("Could not create new document from family '%s'.", $family));
127  $err = $doc->add();
128  $this->assertEmpty($err, sprintf("Could not add new document to database: %s", $err));
129 
130  $oattr = $doc->getAttribute($attrid);
131  $this->assertTrue(is_object($oattr) , sprintf("Could not get attribute '%s' on document '%s' (id=%s).", $attrid, $doc->name, $doc->id));
132 
133  foreach ($inputs as $name => $value) {
134  SetHttpVar($name, $value);
135  }
136 
137  $rargids = array();
138  $tselect = array();
139  $tval = array();
140  $res = getResPhpFunc($doc, $oattr, $rargids, $tselect, $tval);
141  $this->assertTrue(is_string($res) , sprintf("getResPhpFunc mist return errors."));
142  //error_log($res);
143  foreach ($expectedErrors as $error) {
144  $this->assertContains($error, $res, "getResPhpFunc not the expected error");
145  }
146  }
147  private function checkResult(&$expectedResult, &$actualResult)
148  {
149  $expectedCount = count($expectedResult);
150  $actualCount = count($actualResult);
151  if ($actualCount != $expectedCount) {
152  return sprintf("Result count mismatch: found '%s' while expecting '%s'", $actualCount, $expectedCount);
153  }
154 
155  for ($line = 0; $line < $expectedCount; $line++) {
156  $expectedLine = $expectedResult[$line];
157  $actualLine = $actualResult[$line];
158  $expectedLineCount = count($expectedLine);
159  $actualLineCount = count($actualLine);
160  if ($actualLineCount != $expectedLineCount) {
161  return sprintf("Line #%s count mismatch: found '%s' while expecting '%s'", $line, $actualLineCount, $expectedLineCount);
162  }
163 
164  for ($col = 0; $col < $expectedLineCount; $col++) {
165  $actualCell = $actualLine[$col];
166  $expectedCell = $expectedLine[$col];
167  if ($actualCell != $expectedCell) {
168  return sprintf("Data mismatch at Line #%s, Column #%s: found '%s' while expecting '%s'", $line, $col, $actualCell, $expectedCell);
169  }
170  }
171  }
172  return '';
173  }
174 
175  private function checkRargids(&$expectedRargsid, &$actualRargids)
176  {
177  $expectedCount = count($expectedRargsid);
178  $actualCount = count($actualRargids);
179  if ($actualCount != $expectedCount) {
180  return sprintf("Rargids count mismatch: found '%s' while expecting '%s'", $actualCount, $expectedCount);
181  }
182 
183  for ($i = 0; $i < $expectedCount; $i++) {
184  $expected = $expectedRargsid[$i];
185  $actual = $actualRargids[$i];
186  if ($actual != $expected) {
187  return sprintf("Data mismatch at index #%s: found '%s' while expecting '%s'", $i, $actual, $expected);
188  }
189  }
190  return '';
191  }
192 
193  public function dataGetResPhpFunc()
194  {
195  return array(
196  array(
197  array(
198  'fam' => 'TST_GETRESPHPFUNC',
199  'attr' => 'S_GRAVITE',
200  'expected:results' => array(
201  array(
202  'mineure',
203  'Mi'
204  ) ,
205  array(
206  'majeure',
207  'Ma'
208  ) ,
209  array(
210  'bloquante',
211  'Bl'
212  )
213  ) ,
214  'expected:rargids' => array(
215  'S_GRAVITE'
216  )
217  )
218  ) ,
219  array(
220  array(
221  'fam' => 'TST_GETRESPHPFUNC',
222  'attr' => 'S_TITLE_1',
223  'http:vars' => array(
224  '_s_title_1' => 'Relation'
225  ) ,
226  'expected:results' => array(
227  array(
228  'Test Relation 2',
229  'Test Relation 2'
230  ) ,
231  array(
232  'Test Relation 3',
233  'Test Relation 3'
234  )
235  ) ,
236  'expected:rargids' => array(
237  'S_TITLE_1'
238  )
239  )
240  ) ,
241  array(
242  array(
243  'fam' => 'TST_GETRESPHPFUNC',
244  'attr' => 'S_TITLE_2',
245  'http:vars' => array(
246  '_s_title_2' => 'Relation'
247  ) ,
248  'expected:results' => array(
249  array(
250  'Test Relation 2',
251  'Test Relation 2'
252  ) ,
253  array(
254  'Test Relation 3',
255  'Test Relation 3'
256  )
257  ) ,
258  'expected:rargids' => array(
259  'S_TITLE_2'
260  )
261  )
262  ) ,
263  /*
264  array(
265  array(
266  'fam' => 'TST_GETRESPHPFUNC',
267  'attr' => 'S_SPACE_1',
268  'http:vars' => array(
269  '_arg_1' => 'Arg 1',
270  '_arg_2' => 'Arg 2',
271  '_arg_3' => 'Arg 3'
272  ),
273  'expected:results' => array(
274  array(
275  'Arg 1, Arg 2, Arg 3',
276  'Arg 1, Arg 2, Arg 3'
277  )
278  ),
279  'expected:rargids' => array(
280  'RET_1',
281  'RET_2',
282  'RET_3'
283  )
284  )
285  ) ,
286  array(
287  array(
288  'fam' => 'TST_GETRESPHPFUNC',
289  'attr' => 'S_QUOTE_1',
290  'expected:results' => array(
291  array(
292  'Arg 1, Arg 2"Deux, Arg 3'.chr(0x27).'Trois',
293  'Arg 1, Arg 2"Deux, Arg 3'.chr(0x27).'Trois'
294  )
295  ),
296  'expected:rargids' => array(
297  'RET'
298  )
299  )
300  )
301  */
302  );
303  }
304 
305  public function dataBadGetResPhpFunc()
306  {
307  return array(
308  array(
309  "TST_GETRESPHPFUNC",
310  'S_LATIN1',
311  array(
312  "_s_latin1" => "été"
313  ) ,
314  array(
315  'INH0002',
316  "s_latin1"
317  )
318  ) ,
319  array(
320  "TST_GETRESPHPFUNC",
321  'S_WRONGARRAY',
322  array(
323  "_s_wrongarray" => "test"
324  ) ,
325  array(
326  'INH0001',
327  "s_wrongarray"
328  )
329  )
330  );
331  }
332 }
333 ?>
getResPhpFunc(Doc &$doc, NormalAttribute &$oattr, &$rargids, &$tselect, &$tval, $whttpvars=true, $index="")
Definition: enum_choice.php:62
SetHttpVar($name, $def)
Definition: Lib.Http.php:150
$ret
$file
testGetResPhpFuncError($family, $attrid, array $inputs, array $expectedErrors)
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
new_Doc($dbaccess, $id= '', $latest=false)
$dbaccess
Definition: checkVault.php:17
if($file) if($subject==""&&$file) if($subject=="") $err
$value
$data
← centre documentaire © anakeen