PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/codes-php/phpjakarta/tests/functional/WindowsAzure/Table/TableServiceFunctionalTestUtils.php

http://bukuphpjs.codeplex.com
PHP | 446 lines | 382 code | 36 blank | 28 comment | 204 complexity | 000e1a45fc6cc0695124f10993a4d7a0 MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package Tests\Functional\WindowsAzure\Table
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace Tests\Functional\WindowsAzure\Table;
  24. use Tests\Functional\WindowsAzure\Table\Enums\MutatePivot;
  25. use WindowsAzure\Common\Internal\Utilities;
  26. use WindowsAzure\Table\Models\EdmType;
  27. use WindowsAzure\Table\Models\Entity;
  28. use WindowsAzure\Table\Models\Property;
  29. use WindowsAzure\Table\Models\Filters\BinaryFilter;
  30. use WindowsAzure\Table\Models\Filters\ConstantFilter;
  31. use WindowsAzure\Table\Models\Filters\Filter;
  32. use WindowsAzure\Table\Models\Filters\PropertyNameFilter;
  33. use WindowsAzure\Table\Models\Filters\QueryStringFilter;
  34. use WindowsAzure\Table\Models\Filters\UnaryFilter;
  35. class TableServiceFunctionalTestUtils
  36. {
  37. static function isEqNotInTopLevel($filter)
  38. {
  39. return self::isEqNotInTopLevelWorker($filter, 0);
  40. }
  41. private static function isEqNotInTopLevelWorker($filter, $depth)
  42. {
  43. if (is_null($filter)) {
  44. return false;
  45. } else if ($filter instanceof UnaryFilter) {
  46. return self::isEqNotInTopLevelWorker($filter->getOperand(), $depth + 1);
  47. } else if ($filter instanceof BinaryFilter) {
  48. $binaryFilter = $filter;
  49. if ($binaryFilter->getOperator() == ('eq') && $depth != 0) {
  50. return true;
  51. }
  52. $left = self::isEqNotInTopLevelWorker($binaryFilter->getLeft(), $depth + 1);
  53. $right = self::isEqNotInTopLevelWorker($binaryFilter->getRight(), $depth + 1);
  54. return $left || $right;
  55. } else {
  56. return false;
  57. }
  58. }
  59. static function cloneRemoveEqNotInTopLevel($filter)
  60. {
  61. return self::cloneRemoveEqNotInTopLevelWorker($filter, 0);
  62. }
  63. private static function cloneRemoveEqNotInTopLevelWorker($filter, $depth)
  64. {
  65. if ($filter instanceof PropertyNameFilter) {
  66. $ret = new PropertyNameFilter($filter->getPropertyName());
  67. return $ret;
  68. } else if ($filter instanceof ConstantFilter) {
  69. $ret = new ConstantFilter($filter->getEdmType(), $filter->getValue());
  70. return $ret;
  71. } else if ($filter instanceof UnaryFilter) {
  72. $operand = self::cloneRemoveEqNotInTopLevelWorker($filter->getOperand(), $depth + 1);
  73. $ret = new UnaryFilter($filter->getOperator(), $operand);
  74. return $ret;
  75. } else if ($filter instanceof BinaryFilter) {
  76. if ($filter->getOperator() == ('eq') && $depth != 0) {
  77. return Filter::applyConstant(false);
  78. }
  79. $left = self::cloneRemoveEqNotInTopLevelWorker($filter->getLeft(), $depth + 1);
  80. $right = self::cloneRemoveEqNotInTopLevelWorker($filter->getRight(), $depth + 1);
  81. $ret = new BinaryFilter($left, $filter->getOperator(), $right);
  82. return $ret;
  83. } else if ($filter instanceof QueryStringFilter) {
  84. $ret = new QueryStringFilter($filter->getQueryString());
  85. return $ret;
  86. } else {
  87. var_dump($filter);
  88. throw new \Exception();
  89. }
  90. }
  91. public static function filterList($filter, $input)
  92. {
  93. $output = array();
  94. foreach($input as $i) {
  95. if (self::filterInterperter($filter, $i)) {
  96. array_push($output, $i);
  97. }
  98. }
  99. return $output;
  100. }
  101. public static function filterEntityList($filter, $input)
  102. {
  103. $output = array();
  104. foreach($input as $i) {
  105. $result = self::filterInterperter($filter, $i);
  106. if (!is_null($result) && $result) {
  107. array_push($output, $i);
  108. }
  109. }
  110. return $output;
  111. }
  112. static function cloneEntity($initialEnt)
  113. {
  114. $ret = new Entity();
  115. $initialProps = $initialEnt->getProperties();
  116. $retProps = array();
  117. foreach($initialProps as $propName => $initialProp) {
  118. // Don't mess with the timestamp.
  119. if ($propName == ('Timestamp')) {
  120. continue;
  121. }
  122. $retProp = new Property();
  123. $retProp->setEdmType($initialProp->getEdmType());
  124. $retProp->setValue($initialProp->getValue());
  125. $retProps[$propName] = $retProp;
  126. }
  127. $ret->setProperties($retProps);
  128. $ret->setETag($initialEnt->getETag());
  129. return $ret;
  130. }
  131. static function mutateEntity($ent, $pivot)
  132. {
  133. if ($pivot == MutatePivot::CHANGE_VALUES) {
  134. self::mutateEntityChangeValues($ent);
  135. } else if ($pivot == MutatePivot::ADD_PROPERTY) {
  136. $ent->addProperty('BOOLEAN' . TableServiceFunctionalTestData::getNewKey(), EdmType::BOOLEAN, true);
  137. $ent->addProperty('DATETIME' . TableServiceFunctionalTestData::getNewKey(), EdmType::DATETIME, Utilities::convertToDateTime('2012-01-26T18:26:19.0000473Z'));
  138. $ent->addProperty('DOUBLE' . TableServiceFunctionalTestData::getNewKey(), EdmType::DOUBLE, 12345678901);
  139. $ent->addProperty('GUID' . TableServiceFunctionalTestData::getNewKey(), EdmType::GUID, '90ab64d6-d3f8-49ec-b837-b8b5b6367b74');
  140. $ent->addProperty('INT32' . TableServiceFunctionalTestData::getNewKey(), EdmType::INT32, 23);
  141. $ent->addProperty('INT64' . TableServiceFunctionalTestData::getNewKey(), EdmType::INT64, '-1');
  142. $ent->addProperty('STRING' . TableServiceFunctionalTestData::getNewKey(), EdmType::STRING, 'this is a test!');
  143. } else if ($pivot == MutatePivot::REMOVE_PROPERTY) {
  144. $propToRemove = null;
  145. foreach($ent->getProperties() as $propName => $propValue) {
  146. // Don't mess with the keys.
  147. if ($propName == ('PartitionKey') || $propName == ('RowKey') || $propName == ('Timestamp')) {
  148. continue;
  149. }
  150. $propToRemove = $propName;
  151. break;
  152. }
  153. $props = $ent->getProperties();
  154. unset($props[$propToRemove]);
  155. } else if ($pivot == MutatePivot::NULL_PROPERTY) {
  156. foreach($ent->getProperties() as $propName => $propValue) {
  157. // Don't mess with the keys.
  158. if ($propName == ('PartitionKey') || $propName == ('RowKey') || $propName == ('Timestamp')) {
  159. continue;
  160. }
  161. $propValue->setValue(null);
  162. }
  163. }
  164. }
  165. private static function mutateEntityChangeValues($ent)
  166. {
  167. foreach($ent->getProperties() as $propName => $initialProp) {
  168. // Don't mess with the keys.
  169. if ($propName == ('PartitionKey') || $propName == ('RowKey') || $propName == ('Timestamp')) {
  170. continue;
  171. }
  172. $ptype = $initialProp->getEdmType();
  173. if (is_null($ptype)) {
  174. $eff = $initialProp->getValue();
  175. $initialProp->setValue($eff . 'AndMore');
  176. } else if ($ptype == (EdmType::DATETIME)) {
  177. $value = $initialProp->getValue();
  178. if (is_null($value)) {
  179. $value = new \DateTime("1/26/1692");
  180. }
  181. $value->modify('+1 day');
  182. $initialProp->setValue($value);
  183. } else if ($ptype == (EdmType::BINARY)) {
  184. $eff = $initialProp->getValue();
  185. $initialProp->setValue($eff . 'x');
  186. } else if ($ptype == (EdmType::BOOLEAN)) {
  187. $eff = $initialProp->getValue();
  188. $initialProp->setValue(!$eff);
  189. } else if ($ptype == (EdmType::DOUBLE)) {
  190. $eff = $initialProp->getValue();
  191. $initialProp->setValue($eff + 1);
  192. } else if ($ptype == (EdmType::GUID)) {
  193. $initialProp->setValue(com_create_guid());
  194. } else if ($ptype == (EdmType::INT32)) {
  195. $eff = $initialProp->getValue();
  196. $eff = ($eff > 10 ? 0 : $eff + 1);
  197. $initialProp->setValue($eff);
  198. } else if ($ptype == (EdmType::INT64)) {
  199. $eff = $initialProp->getValue();
  200. $eff = ($eff > 10 ? 0 : $eff + 1);
  201. $initialProp->setValue(strval($eff));
  202. } else if ($ptype == (EdmType::STRING)) {
  203. $eff = $initialProp->getValue();
  204. $initialProp->setValue($eff . 'AndMore');
  205. }
  206. }
  207. }
  208. public static function filterToString($filter, $pad = ' ')
  209. {
  210. if (is_null($filter)) {
  211. return $pad . 'filter <null>' . "\n";
  212. } else if ($filter instanceof PropertyNameFilter) {
  213. return $pad . 'entity.' . $filter->getPropertyName() . "\n";
  214. } else if ($filter instanceof ConstantFilter) {
  215. $ret = $pad;
  216. if (is_null($filter->getValue())) {
  217. $ret .= 'constant <null>';
  218. } else if (is_bool ($filter->getValue())) {
  219. $ret .= ($filter->getValue() ? 'true' : 'false');
  220. } else {
  221. $ret .= '\'' . FunctionalTestBase::tmptostring($filter->getValue()) . '\'';
  222. }
  223. return $ret . "\n";
  224. } else if ($filter instanceof UnaryFilter) {
  225. $ret = $pad . $filter->getOperator() . "\n";
  226. $ret .= self::filterToString($filter->getOperand(), $pad . ' ');
  227. return $ret;
  228. } else if ($filter instanceof BinaryFilter) {
  229. $ret = self::filterToString($filter->getLeft(), $pad . ' ');
  230. $ret .= $pad . $filter->getOperator() . "\n";
  231. $ret .= self::filterToString($filter->getRight(), $pad . ' ');
  232. return $ret;
  233. }
  234. }
  235. private static function filterInterperter($filter, $obj)
  236. {
  237. if (is_null($filter)) {
  238. return true;
  239. } else if (is_null($obj)) {
  240. return false;
  241. } else if ($filter instanceof PropertyNameFilter) {
  242. $name = $filter->getPropertyName();
  243. $value = ($obj instanceof Entity ? $obj->getPropertyValue($name) : $obj->{$name});
  244. return $value;
  245. } else if ($filter instanceof ConstantFilter) {
  246. $value = $filter->getValue();
  247. return $value;
  248. } else if ($filter instanceof UnaryFilter) {
  249. $ret = null;
  250. if ($filter->getOperator() == ('not')) {
  251. $op = self::filterInterperter($filter->getOperand(), $obj);
  252. if (is_null($op)) {
  253. // http://msdn.microsoft/com/en-us/library/ms191504.aspx
  254. $ret = true;
  255. } else {
  256. $ret = !$op;
  257. }
  258. return $ret;
  259. }
  260. } else if ($filter instanceof BinaryFilter) {
  261. $left = self::filterInterperter($filter->getLeft(), $obj);
  262. $right = self::filterInterperter($filter->getRight(), $obj);
  263. $ret = null;
  264. if ($filter->getOperator() == ('and')) {
  265. $ret = self::nullPropAnd($left, $right);
  266. } else if ($filter->getOperator() == ('or')) {
  267. $ret = self::nullPropOr($left, $right);
  268. } else if ($filter->getOperator() == ('eq')) {
  269. $ret = self::nullPropEq($left, $right);
  270. } else if ($filter->getOperator() == ('ne')) {
  271. $ret = self::nullPropNe($left, $right);
  272. } else if ($filter->getOperator() == ('ge')) {
  273. $ret = self::nullPropGe($left, $right);
  274. } else if ($filter->getOperator() == ('gt')) {
  275. $ret = self::nullPropGt($left, $right);
  276. } else if ($filter->getOperator() == ('lt')) {
  277. $ret = self::nullPropLt($left, $right);
  278. } else if ($filter->getOperator() == ('le')) {
  279. $ret = self::nullPropLe($left, $right);
  280. }
  281. return $ret;
  282. }
  283. var_dump(array($filter, $obj));
  284. throw new \Exception();
  285. }
  286. private static function nullPropAnd($left, $right)
  287. {
  288. // http://msdn.microsoft.com/en-us/library/ms191504.aspx
  289. if (is_null($left) && is_null($right)) {
  290. return null;
  291. } else if (is_null($left)) {
  292. return ($right ? null : false);
  293. } else if (is_null($right)) {
  294. return ($left ? null : false);
  295. } else {
  296. return $left && $right;
  297. }
  298. }
  299. private static function nullPropOr($left, $right)
  300. {
  301. // http://msdn.microsoft.com/en-us/library/ms191504.aspx
  302. if (is_null($left) && is_null($right)) {
  303. return null;
  304. } else if (is_null($left)) {
  305. return ($right ? true : null);
  306. } else if (is_null($right)) {
  307. return ($left ? true : null);
  308. } else {
  309. return $left || $right;
  310. }
  311. }
  312. private static function nullPropEq($left, $right)
  313. {
  314. if (is_null($left) || is_null($right)) {
  315. return null;
  316. } else if (is_string($left) || is_string($right)) {
  317. return ('' . $left) == ('' . $right);
  318. }
  319. return $left == $right;
  320. }
  321. private static function nullPropNe($left, $right)
  322. {
  323. if (is_null($left) || is_null($right)) {
  324. return null;
  325. } else if (is_string($left) || is_string($right)) {
  326. return ('' . $left) != ('' . $right);
  327. }
  328. return $left != $right;
  329. }
  330. private static function nullPropGt($left, $right)
  331. {
  332. if (is_null($left) || is_null($right)) {
  333. return null;
  334. } else if (is_string($left) || is_string($right)) {
  335. return ('' . $left) > ('' . $right);
  336. }
  337. return $left > $right;
  338. }
  339. private static function nullPropGe($left, $right)
  340. {
  341. if (is_null($left) || is_null($right)) {
  342. return null;
  343. } else if (is_string($left) || is_string($right)) {
  344. return ('' . $left) >= ('' . $right);
  345. }
  346. return $left >= $right;
  347. }
  348. private static function nullPropLt($left, $right)
  349. {
  350. if (is_null($left) || is_null($right)) {
  351. return null;
  352. } else if (is_string($left) || is_string($right)) {
  353. return ('' . $left) < ('' . $right);
  354. }
  355. return $left < $right;
  356. }
  357. private static function nullPropLe($left, $right)
  358. {
  359. if (is_null($left) || is_null($right)) {
  360. return null;
  361. } else if (is_string($left) || is_string($right)) {
  362. return ('' . $left) <= ('' . $right);
  363. }
  364. return $left <= $right;
  365. }
  366. public static function showEntityListDiff($actualData, $expectedData)
  367. {
  368. $ret = '';
  369. if (count($expectedData) != count($actualData)) {
  370. $ret .= 'VVV actual VVV' . "\n";
  371. for ($i = 0; $i < count($actualData); $i++) {
  372. $e = $actualData[$i];
  373. $ret .= $e->getPartitionKey() . '/' . $e->getRowKey() . "\n";
  374. }
  375. $ret .= '-----------------' . "\n";
  376. for ($i = 0; $i < count($expectedData); $i++) {
  377. $e = $expectedData[$i];
  378. $ret .= $e->getPartitionKey() . '/' . $e->getRowKey() . "\n";
  379. }
  380. $ret .= '^^^ expected ^^^' . "\n";
  381. for ($i = 0; $i < count($actualData); $i++) {
  382. $in = false;
  383. $ei = $actualData[$i];
  384. for ($j = 0; $j < count($expectedData); $j++) {
  385. $ej = $expectedData[$j];
  386. if ($ei->getPartitionKey() == $ej->getPartitionKey() && $ei->getRowKey() == $ej->getRowKey()) {
  387. $in = true;
  388. }
  389. }
  390. if (!$in) {
  391. $ret .= 'returned ' . $this->tmptostring($ei). "\n";
  392. }
  393. }
  394. for ($j = 0; $j < count($expectedData); $j++) {
  395. $in = false;
  396. $ej = $expectedData[$j];
  397. for ($i = 0; $i < count($actualData); $i++) {
  398. $ei = $actualData[$i];
  399. if ($ei->getPartitionKey() == $ej->getPartitionKey() && $ei->getRowKey() == $ej->getRowKey()) {
  400. $in = true;
  401. }
  402. }
  403. if (!$in) {
  404. $ret .= 'expected ' . $this->tmptostring($ej). "\n";
  405. }
  406. }
  407. }
  408. return $ret;
  409. }
  410. }