Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_simplequery.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_document.php';
10 
12 {
13 
14  protected function createDataSearch()
15  {
16 
17  $basics = array(
18  "Poire",
19  "Pomme",
20  "Cerise",
21  "Pomme-Banane",
22  "Banane"
23  );
24  foreach ($basics as $socTitle) {
25  $d1 = createDoc(self::$dbaccess, "BASE", false);
26  $d1->setTitle($socTitle);
27  $err = $d1->add();
28  if ($err != "") return false;
29  }
30  return true;
31  }
32  /**
33  * test basic search criteria
34  * @param string $query filter
35  * @param array $arg filter arg
36  * @param integer $expectedCount expected results count
37  * @return void
38  * @dataProvider dataSimpleQuery
39  */
40  public function testExecuteSimpleQuery($query, array $arg, $expectedCount)
41  {
42  $result = array();
43  $sql = vsprintf($query, $arg);
44  $err = simpleQuery(self::$dbaccess, $sql, $result);
45 
46  $this->assertEmpty($err, sprintf("Simple query error %s", $sql));
47 
48  $this->assertEquals($expectedCount, count($result) , sprintf("Count must be %d (found %d) error %s", $expectedCount, count($result) , $sql));
49  }
50  /**
51  * test basic search criteria
52  * @param string $query filter
53  * @param array $arg filter arg
54  * @param array $expectedErrors expected errors
55  * @return void
56  * @dataProvider dataErrorSimpleQuery
57  */
58  public function testErrorSimpleQuery($query, array $arg, array $expectedErrors)
59  {
60  $result = array();
61  $sql = vsprintf($query, $arg);
62 
63  try {
64  $err = simpleQuery(self::$dbaccess, $sql, $result);
65  }
66  catch(\Exception $e) {
67  $err = $e->getMessage();
68  }
69  $this->assertNotEmpty($err, sprintf("No error found in simple query %s", $sql));
70  foreach ($expectedErrors as $errors) {
71  $this->assertContains($errors, $err, sprintf("Not correct error for %s", $sql));
72  }
73  }
74  /**
75  * test return when no results
76  * @param $singleResult
77  * @param $singleColumn
78  * @param $expectedResult
79  * @return void
80  * @dataProvider dataReturnNothingSimpleQuery
81  */
82  public function testReturnNothingSimpleQuery($singleResult, $singleColumn, $expectedResult)
83  {
84  $result = "undefined";
85  $sql = "select id from docread where false;";
86  simpleQuery(self::$dbaccess, $sql, $result, $singleColumn, $singleResult);
87 
88  $this->assertTrue(($result === $expectedResult) , sprintf("No the good return : %s, expect : %s", print_r($result, true) , print_r($expectedResult, true)));
89  }
90  /**
91  * test basic search criteria
92  * @param string $query filter
93  * @param array $arg filter arg
94  * @param array $expectedErrors expected errors
95  * @return void
96  * @dataProvider dataErrorSimpleQuery
97  */
98  public function testTolerantErrorSimpleQuery($query, array $arg, array $expectedErrors)
99  {
100 
101  $result = array();
102  $sql = vsprintf($query, $arg);
103  $err = simpleQuery(self::$dbaccess, $sql, $result, false, false, $useStrict = false);
104 
105  $this->assertNotEmpty($err, sprintf("No error found in simple query %s", $sql));
106  foreach ($expectedErrors as $errors) {
107  $this->assertContains($errors, $err, sprintf("Not correct error for %s", $sql));
108  }
109  }
110  /**
111  * @return array
112  */
113  public function dataSimpleQuery()
114  {
115 
116  return array(
117  array(
118  "q" => "select * from users where id=%d",
119  "args" => array(
120  self::getAction()->user->id
121  ) ,
122  "count" => 1
123  )
124  );
125  }
126 
128  {
129 
130  return array(
131  array(
132  "singleresult" => false,
133  "singlecolumn" => false,
134  "result" => array()
135  ) ,
136  array(
137  "singleresult" => false,
138  "singlecolumn" => true,
139  "result" => array()
140  ) ,
141  array(
142  "singleresult" => true,
143  "singlecolumn" => false,
144  "result" => array()
145  ) ,
146  array(
147  "singleresult" => true,
148  "singlecolumn" => true,
149  "result" => false
150  )
151  );
152  }
153  /**
154  * @return array
155  */
156  public function dataErrorSimpleQuery()
157  {
158 
159  return array(
160  array(
161  "q" => "select * from users where id=%s",
162  "args" => array(
163  "zut"
164  ) ,
165  "errors" => array(
166  'DB0100',
167  'zut'
168  )
169  )
170  );
171  }
172 }
testExecuteSimpleQuery($query, array $arg, $expectedCount)
$core user
Definition: chgpasswd.php:38
testTolerantErrorSimpleQuery($query, array $arg, array $expectedErrors)
testReturnNothingSimpleQuery($singleResult, $singleColumn, $expectedResult)
testErrorSimpleQuery($query, array $arg, array $expectedErrors)
$d1
Definition: dav.php:11
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
$dbaccess
Definition: checkVault.php:17
if(($docid!==0)&&(!is_numeric($docid))) $query
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
← centre documentaire © anakeen