Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_usage.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp\Pu;
8 /**
9  * @author Anakeen
10  * @package Dcp\Pu
11  */
12 
13 require_once 'PU_testcase_dcp.php';
14 
15 class TestUsage extends TestCaseDcp
16 {
17  /**
18  *
19  * @dataProvider dataTextUsage
20  *
21  */
22  public function testTextApiUsage($text)
23  {
24  $u = new \ApiUsage();
25  $u->setDefinitionText($text);
26 
27  $this->assertContains($text, $u->getUsage());
28  }
29  /**
30  *
31  * @dataProvider dataNeedUsage
32  *
33  */
34  public function testNeededApiUsage($argNeeded, $def)
35  {
36  $usage = '';
37  try {
38  $u = new \ApiUsage();
39  $u->addRequiredParameter($argNeeded, $def);
40  $u->verify();
41  }
42  catch(\Exception $e) {
43  $usage = $e->getMessage();
44  }
45  $this->assertContains($argNeeded, $usage);
46  $this->assertContains($def, $usage);
47  }
48  /**
49  *
50  * @dataProvider dataNeedUsage
51  *
52  */
53  public function testNeededApiUsageForceException($argNeeded, $def)
54  {
55  $usage = '';
56  $error = '';
57  try {
58  $u = new \ApiUsage();
59  $u->addRequiredParameter($argNeeded, $def);
60  $u->verify(true);
61  }
62  catch(\Dcp\ApiUsage\Exception $e) {
63  $error = $e->getMessage();
64  $usage = $e->getUsage();
65  }
66  $this->assertContains($argNeeded, $error);
67  $this->assertNotContains($def, $error);
68  $this->assertContains($argNeeded, $usage);
69  $this->assertContains($def, $usage);
70  }
71 
72  public static function usageCallback($values, $argName, $apiUsage)
73  {
74  if ($values === null) return sprintf("This is the usage for argument %s", $argName);
75  if (!is_scalar($values)) return sprintf("Error in usageCallback for argument %s: type of value %s must be string", $argName, gettype($values));
76  return "";
77  }
78  /**
79  * @dataProvider dataCallbackUsage
80  *
81  */
82  public function testGoodCallbackUsage($callback)
83  {
84  $usage = '';
85  try {
86  SetHttpVar("needed", "needed");
87  $u = new \ApiUsage();
88  $u->addRequiredParameter("needed", "A needed argument", $callback);
89  $u->addOptionalParameter("optional", "An optional argument", $callback);
90  $u->addHiddenParameter("hidden", "An hidden argument", $callback);
91  $u->verify(true);
92  }
93  catch(\Exception $e) {
94  $usage = $e->getMessage();
95  }
96  $this->assertEmpty($usage);
97  }
98  /**
99  * @dataProvider dataCallbackUsage
100  *
101  */
102  public function testBadCallbackNeededUsage($callback)
103  {
104  $error = '';
105  $myvar = "myvariable";
106  try {
107  SetHttpVar($myvar, array(
108  $myvar
109  ));
110  $u = new \ApiUsage();
111  $u->addRequiredParameter($myvar, "A needed argument", $callback);
112  $u->verify(true);
113  }
114  catch(\Exception $e) {
115  $error = $e->getMessage();
116  }
117  $this->assertContains("usageCallback", $error);
118  $this->assertContains($myvar, $error);
119  }
120  /**
121  * @dataProvider dataCallbackUsage
122  *
123  */
124  public function testBadCallbackOptinalUsage($callback)
125  {
126  $error = '';
127  $myvar = "myvariable";
128  try {
129  SetHttpVar($myvar, array(
130  $myvar
131  ));
132  $u = new \ApiUsage();
133  $u->addOptionalParameter($myvar, "An optional argument", $callback);
134  $u->verify(true);
135  }
136  catch(\Exception $e) {
137  $error = $e->getMessage();
138  }
139  $this->assertContains("usageCallback", $error);
140  $this->assertContains($myvar, $error);
141  }
142  /**
143  * @dataProvider dataCallbackUsage
144  *
145  */
146  public function testCallbackUsage($callback)
147  {
148  $usage = '';
149  try {
150  SetHttpVar("help", true);
151  $u = new \ApiUsage();
152  $u->addRequiredParameter("needed", "A needed argument", $callback);
153  $u->addOptionalParameter("optional", "An optional argument", $callback);
154  $u->addHiddenParameter("hidden", "An hidden argument", $callback);
155  $u->verify();
156  }
157  catch(\Exception $e) {
158  $usage = $e->getMessage();
159  }
160  $this->assertContains("CORE0003", $usage, sprintf("usage found is %s", $usage));
161  $this->assertNotContains("CORE0002", $usage, sprintf("usage found is %s", $usage));
162  }
163 
164  public function dataCallbackUsage()
165  {
166  return array(
167  array(
168  "\Dcp\Pu\TestUsage::usageCallback",
169  "\Dcp\Pu\simpleFunctionUsageCallback",
170  array(
171  $this,
172  "usageCallback"
173  ) ,
174  function ($values, $argName, $apiUsage)
175  {
176  return TestUsage::usageCallback($values, $argName, $apiUsage);
177  }
178  )
179  );
180  }
181 
182  public function dataTextUsage()
183  {
184  return array(
185  array(
186  "hello world"
187  )
188  );
189  }
190 
191  public function dataNeedUsage()
192  {
193  return array(
194  array(
195  "needAbsolut",
196  "necessary"
197  )
198  );
199  }
200 }
201 function simpleFunctionUsageCallback($values, $argName, \ApiUsage $apiUsage)
202 {
203  return TestUsage::usageCallback($values, $argName, $apiUsage);
204 }
205 ?>
testBadCallbackNeededUsage($callback)
static usageCallback($values, $argName, $apiUsage)
SetHttpVar($name, $def)
Definition: Lib.Http.php:150
testGoodCallbackUsage($callback)
testBadCallbackOptinalUsage($callback)
testCallbackUsage($callback)
testNeededApiUsageForceException($argNeeded, $def)
testNeededApiUsage($argNeeded, $def)
simpleFunctionUsageCallback($values, $argName,\ApiUsage $apiUsage)
$usage
Verify arguments for wsh programs.
← centre documentaire © anakeen