PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/integration/price/datagen.php

https://github.com/GM-Alex/oxideshop_ce
PHP | 466 lines | 323 code | 31 blank | 112 comment | 27 complexity | bfa851cb197277d52f886648a0ff0037 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-3.0
  1. <?php
  2. require_once realpath(dirname(__FILE__) . '/../../') . '/unit/OxidTestCase.php';
  3. require_once realpath(dirname(__FILE__) . '/../../') . '/integration/price/basketconstruct.php';
  4. class DataGenerator extends OxidTestCase
  5. {
  6. // Shop modes: brutto-brutto or netto-brutto
  7. private $blEnterNetPrice = true;
  8. private $blShowNetPrice = false;
  9. // Test case variants
  10. private $iVariants = 1;
  11. // Custom general name of test cases. Will produce files like RandomCase_x for each case.
  12. private $sCaseName = "nb_";
  13. // Databomb folder path
  14. private $sFilepath = "integration/price/testcases/databomb/netto_brutto/";
  15. // Price in cents
  16. private $dPriceFrom = 1;
  17. private $dPriceTo = 100099;
  18. // Min basket positions
  19. private $iBasketPosMin = 1;
  20. // Max basket positions
  21. private $iBasketPosMax = 20;
  22. // Min diff vats
  23. private $iDiffVatCountMin = 1;
  24. // Max diff vats
  25. private $iDiffVatCountMax = 3;
  26. // Min article amount at one position
  27. private $iAmountMin = 1;
  28. // Max article amount at one position
  29. private $iAmountMax = 250;
  30. // Active currency rate
  31. private $activeCurrencyRate = 1;
  32. // Discount params
  33. private $iDisVariants = 5;
  34. private $sDisName = "bombDiscount";
  35. private $iDisMinAddsum = 1;
  36. private $iDisMaxAddsum = 99;
  37. private $aDisTypes = array(
  38. "abs",
  39. "%",
  40. //"itm"
  41. );
  42. private $iDisAmount = 1;
  43. private $iDisAmountTo = 9999999;
  44. private $iDisPrice = 1;
  45. private $iDisPriceTo = 9999999;
  46. private $iDisMaxNrArtsApply = 15;
  47. // Wrapping params
  48. private $iWrapMinPrice = 0.1;
  49. private $iWrapMaxPrice = 9.9;
  50. private $iWrapMaxNrArtsApply = 2;
  51. private $aWrapTypes = array(
  52. "WRAP",
  53. //"CARD"
  54. );
  55. // Payment params
  56. private $iPayMinAddSum = 1;
  57. private $iPayMaxAddSum = 33;
  58. private $aPayAddSumTypes = array("%", "abs");
  59. private $iPayFromAmount = 0;
  60. private $iPayToAmount = 1000000;
  61. private $iPayChecked = 1;
  62. // Delivery params
  63. private $iDelMinAddSum = 1;
  64. private $iDelMaxAddSum = 25;
  65. private $aDelAddSumTypes = array("abs", "%");
  66. private $aDelTypes = array(
  67. //"a", // amount
  68. //"s", // size
  69. //"w", // weight
  70. "p" // price
  71. );
  72. private $iDelFinalize = 0;
  73. private $iDelParam = 0;
  74. private $iDelParamend = 999999999;
  75. private $iDelFixed = 0;
  76. // Voucherseries params + voucher amount
  77. private $iVouseries = 1;
  78. private $iVouNumber = 2;
  79. private $iVouSerieMinDiscount = 0.5;
  80. private $iVouSerieMaxDiscount = 75;
  81. private $aVouSerieTypes = array(
  82. 'absolute',
  83. 'percent'
  84. );
  85. private $iVouAllowSameSeries = 1;
  86. private $iVouAllowOtherSeries = 1;
  87. private $iVouAllowUseAnother = 1;
  88. // What additional costs to generate
  89. private $aGenCosts = array(
  90. array("wrapping", 1),
  91. //array( "payment", 3 ),
  92. //array( "delivery", 3 )
  93. );
  94. private $blGenDiscounts = false;
  95. private $blGenVouchers = false;
  96. protected function _cleanUpCalcDb()
  97. {
  98. $this->_truncateTable("oxarticles");
  99. $this->_truncateTable("oxdiscount");
  100. $this->_truncateTable("oxobject2discount");
  101. $this->_truncateTable("oxwrapping");
  102. $this->_truncateTable("oxdelivery");
  103. $this->_truncateTable("oxdel2delset");
  104. $this->_truncateTable("oxobject2payment");
  105. $this->_truncateTable("oxvouchers");
  106. $this->_truncateTable("oxvoucherseries");
  107. $this->_truncateTable("oxobject2delivery");
  108. $this->_truncateTable("oxdeliveryset");
  109. }
  110. /**
  111. * Function to return vat set of world's vats
  112. *
  113. * @return array of different vats
  114. */
  115. protected function _getVatSet()
  116. {
  117. return array(
  118. 27, 25.5, 25, 24, 23, 22, 21, 21.2, 20,
  119. 19.6, 19, 18, 17.5, 17, 16, 15, 14.5, 14, 13, 13.5, 12.5, 12, 11, 10.5, 10,
  120. 9, 8.5, 8, 7, 6, 6.5, 5.6, 5.5, 5, 4.8, 4.5, 4, 3.8, 3, 2.5, 2.1, 2, 1, 0
  121. );
  122. }
  123. /**
  124. * Create test case file
  125. *
  126. * @param string $sFilename test case filename
  127. *
  128. * @return file resource
  129. */
  130. protected function _createFile($sFilename)
  131. {
  132. return fopen($this->sFilepath . $sFilename, "w");
  133. }
  134. /**
  135. * Writes data array to file with provided handle
  136. *
  137. * @param file resource to write $rHandle
  138. * @param array $aData of data needed to write
  139. *
  140. * @return mixed
  141. */
  142. protected function _writeToFile($rHandle, $aData)
  143. {
  144. $sStart = "<?php\r";
  145. $sStart .= "\$aData = ";
  146. $sData = var_export($aData, true);
  147. $sEnd = ";";
  148. return fwrite($rHandle, $sStart . $sData . $sEnd);
  149. }
  150. /**
  151. * Main generator startup function, calls other utilities
  152. *
  153. * @test
  154. */
  155. public function generate()
  156. {
  157. if (!is_dir($this->sFilepath)) {
  158. mkdir($this->sFilepath, '0777');
  159. }
  160. for ($i = 1; $i <= $this->iVariants; $i++) {
  161. parent::setUp();
  162. $this->_cleanUpCalcDb();
  163. $aData = $this->_generateData($i);
  164. $sFilename = "{$this->sCaseName}{$i}.php";
  165. $rHandle = $this->_createFile($sFilename);
  166. $this->_writeToFile($rHandle, $aData);
  167. print("o-");
  168. parent::tearDown();
  169. }
  170. }
  171. /**
  172. * Data generator
  173. *
  174. * @param integer $i variant number
  175. *
  176. * @return array $aData of basket data and expectations
  177. */
  178. protected function _generateData($i)
  179. {
  180. $oUtil = oxUtilsObject::getInstance();
  181. // init result array
  182. $aData = array();
  183. // new user gen data
  184. $aData['user'] = array(
  185. 'oxactive' => 1,
  186. 'oxusername' => $this->sCaseName . 'databomb_user_' . $i,
  187. );
  188. // get basket position count
  189. $iRandArtCount = rand($this->iBasketPosMin, $this->iBasketPosMax);
  190. // get different vat count
  191. $iDiffVatCount = rand($this->iDiffVatCountMin, $this->iDiffVatCountMax);
  192. // get $iDiffVatCount vats from vat set
  193. $aVats = array_rand($this->_getVatSet(), $iDiffVatCount);
  194. // create articles array
  195. for ($i = 0; $i < $iRandArtCount; $i++) {
  196. $aArticle = array();
  197. $sUID = $oUtil->generateUId();
  198. $aArticle['oxid'] = $sUID;
  199. $aArticle['oxprice'] = mt_rand($this->dPriceFrom, $this->dPriceTo) / 100;
  200. // check if got any special vat
  201. if (count($aVats) > 0) {
  202. // check if got vat set vs single vat
  203. if (count($aVats) == 1) {
  204. $aArticle['oxvat'] = $aVats;
  205. } else {
  206. $aArticle['oxvat'] = $aVats[array_rand($aVats, 1)];
  207. }
  208. }
  209. $aArticle['amount'] = rand($this->iAmountMin, $this->iAmountMax);
  210. $aData['articles'][$i] = $aArticle;
  211. }
  212. if ($this->blGenDiscounts) {
  213. // create discount array
  214. $aData['discounts'] = $this->_generateDiscounts($aData);
  215. }
  216. if (!empty($this->aGenCosts)) {
  217. // create costs array
  218. $aData['costs'] = $this->_generateCosts($aData);
  219. }
  220. if ($this->blGenVouchers) {
  221. // create voucher discounts
  222. $aData['costs']['voucherserie'] = $this->_generateVouchers($aData);
  223. }
  224. // create options array
  225. $aData['options'] = array();
  226. $aData['options']['config']['blEnterNetPrice'] = $this->blEnterNetPrice;
  227. $aData['options']['config']['blShowNetPrice'] = $this->blShowNetPrice;
  228. $aData['options']['activeCurrencyRate'] = $this->activeCurrencyRate;
  229. // create expected array
  230. $aData['expected'] = $this->_gatherExpectedData($aData);
  231. return $aData;
  232. }
  233. /**
  234. * Generate vouchers
  235. *
  236. * @param array $aData
  237. */
  238. protected function _generateVouchers($aData)
  239. {
  240. $aVouchers = array();
  241. for ($i = 0; $i < $this->iVouseries; $i++) {
  242. $aVouchers[$i]['oxdiscount'] = mt_rand($this->iVouSerieMinDiscount, $this->iVouSerieMaxDiscount);
  243. $aVouchers[$i]['oxdiscounttype'] = $this->aVouSerieTypes[array_rand($this->aVouSerieTypes, 1)];
  244. $aVouchers[$i]['oxallowsameseries'] = $this->iVouAllowSameSeries;
  245. $aVouchers[$i]['oxallowotherseries'] = $this->iVouAllowOtherSeries;
  246. $aVouchers[$i]['oxallowuseanother'] = $this->iVouAllowUseAnother;
  247. $aVouchers[$i]['voucher_count'] = $this->iVouNumber;
  248. }
  249. return $aVouchers;
  250. }
  251. /**
  252. * Generate costs
  253. *
  254. * @param array $aData
  255. */
  256. protected function _generateCosts($aData)
  257. {
  258. $aCosts = array();
  259. foreach ($this->aGenCosts as $aCostData) {
  260. switch ($aCostData[0]) {
  261. case 'wrapping':
  262. $aCosts['wrapping'] = array();
  263. for ($i = 0; $i < $aCostData[1]; $i++) {
  264. $aCost = array();
  265. $aCost['oxtype'] = $this->aWrapTypes[array_rand($this->aWrapTypes, 1)];
  266. $aCost['oxprice'] = mt_rand($this->iWrapMinPrice, $this->iWrapMaxPrice);
  267. $aCost['oxactive'] = 1;
  268. if ($this->iWrapMaxNrArtsApply > 0) {
  269. $aCost['oxarticles'] = array();
  270. if ($this->iWrapMaxNrArtsApply <= count($aData['articles'])) {
  271. $iRandCount = mt_rand(1, $this->iWrapMaxNrArtsApply);
  272. } else {
  273. $iRandCount = mt_rand(1, count($aData['articles']));
  274. }
  275. $mxRand = array_rand($aData['articles'], $iRandCount);
  276. $iMxRandCount = count($mxRand);
  277. for ($j = 0; $j < $iMxRandCount; $j++) {
  278. array_push($aCost['oxarticles'], $aData['articles'][$j]['oxid']);
  279. }
  280. }
  281. $aCosts['wrapping'][$i] = $aCost;
  282. }
  283. break;
  284. case 'payment':
  285. $aCosts['payment'] = array();
  286. for ($i = 0; $i < $aCostData[1]; $i++) {
  287. $aCost = array();
  288. $aCost['oxaddsumtype'] = $this->aPayAddSumTypes[array_rand($this->aPayAddSumTypes, 1)];
  289. $aCost['oxaddsum'] = mt_rand($this->iPayMinAddSum, $this->iPayMaxAddSum);
  290. $aCost['oxactive'] = 1;
  291. $aCost['oxchecked'] = $this->iPayChecked;
  292. $aCost['oxfromamount'] = $this->iPayFromAmount;
  293. $aCost['oxtoamount'] = $this->iPayToAmount;
  294. $aCosts['payment'][$i] = $aCost;
  295. }
  296. break;
  297. case 'delivery':
  298. $aCosts['delivery'] = array();
  299. for ($i = 0; $i < $aCostData[1]; $i++) {
  300. $aCost = array();
  301. $aCost['oxaddsumtype'] = $this->aDelAddSumTypes[array_rand($this->aDelAddSumTypes, 1)];
  302. $aCost['oxaddsum'] = mt_rand($this->iDelMinAddSum, $this->iDelMaxAddSum);
  303. $aCost['oxactive'] = 1;
  304. $aCost['oxdeltype'] = $this->aDelTypes[array_rand($this->aDelTypes, 1)];
  305. $aCost['oxfinalize'] = $this->iDelFinalize;
  306. $aCost['oxparam'] = $this->iDelParam;
  307. $aCost['oxparamend'] = $this->iDelParamend;
  308. $aCost['oxfixed'] = $this->iDelFixed;
  309. $aCosts['delivery'][$i] = $aCost;
  310. }
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. return $aCosts;
  317. }
  318. /**
  319. * Generate discounts
  320. *
  321. * @param array $aData
  322. */
  323. protected function _generateDiscounts($aData)
  324. {
  325. $aDiscounts = array();
  326. for ($i = 0; $i < $this->iDisVariants; $i++) {
  327. $aDiscounts[$i]['oxaddsum'] = mt_rand($this->iDisMinAddsum, $this->iDisMaxAddsum);
  328. $aDiscounts[$i]['oxid'] = $this->sDisName . '_' . $i;
  329. $aDiscounts[$i]['oxaddsumtype'] = $this->aDisTypes[array_rand($this->aDisTypes, 1)];
  330. $aDiscounts[$i]['oxamount'] = $this->iDisAmount;
  331. $aDiscounts[$i]['oxamountto'] = $this->iDisAmountTo;
  332. $aDiscounts[$i]['oxprice'] = $this->iDisPrice;
  333. $aDiscounts[$i]['oxpriceto'] = $this->iDisPriceTo;
  334. $aDiscounts[$i]['oxactive'] = 1;
  335. if ($this->iDisMaxNrArtsApply > 0) {
  336. $aDiscounts[$i]['oxarticles'] = array();
  337. if ($this->iDisMaxNrArtsApply <= count($aData['articles'])) {
  338. $iRandCount = mt_rand(1, $this->iDisMaxNrArtsApply);
  339. } else {
  340. $iRandCount = mt_rand(1, count($aData['articles']));
  341. }
  342. $mxRand = array_rand($aData['articles'], $iRandCount);
  343. $iMxRandCount = count($mxRand);
  344. for ($j = 0; $j < $iMxRandCount; $j++) {
  345. array_push($aDiscounts[$i]['oxarticles'], $aData['articles'][$j]['oxid']);
  346. }
  347. }
  348. }
  349. return $aDiscounts;
  350. }
  351. /**
  352. * Gathering expectations
  353. *
  354. * @param array 's of articles, discounts, costs, options
  355. *
  356. * @return array $aExpected of expected data
  357. */
  358. protected function _gatherExpectedData($aTestCase)
  359. {
  360. // load calculated basket
  361. $oBasketConstruct = new BasketConstruct();
  362. $oBasket = $oBasketConstruct->calculateBasket($aTestCase);
  363. // gathering data arrays
  364. $aExpected = array();
  365. // Basket item list
  366. $aBasketItemList = $oBasket->getContents();
  367. if ($aBasketItemList) {
  368. foreach ($aBasketItemList as $iKey => $oBasketItem) {
  369. $iArtId = $oBasketItem->getArticle()->getID();
  370. $aExpected['articles'][$iArtId] = array($oBasketItem->getFUnitPrice(), $oBasketItem->getFTotalPrice());
  371. }
  372. }
  373. // Basket total discounts
  374. $aProductDiscounts = $oBasket->getDiscounts();
  375. if ($aProductDiscounts) {
  376. foreach ($aProductDiscounts as $oDiscount) {
  377. $aExpected['totals']['discounts'][$oDiscount->sOXID] = $oDiscount->fDiscount;
  378. }
  379. }
  380. // VAT's
  381. $aProductVats = $oBasket->getProductVats();
  382. if ($aProductVats) {
  383. foreach ($aProductVats as $sPercent => $sSum) {
  384. $aExpected['totals']['vats'][$sPercent] = $sSum;
  385. }
  386. }
  387. // Wrapping costs
  388. $aExpected['totals']['wrapping']['brutto'] = $oBasket->getFWrappingCosts();
  389. $aExpected['totals']['wrapping']['netto'] = $oBasket->getWrappCostNet();
  390. $aExpected['totals']['wrapping']['vat'] = $oBasket->getWrappCostVat();
  391. // Giftcard costs
  392. $aExpected['totals']['giftcard']['brutto'] = $oBasket->getFGiftCardCosts();
  393. $aExpected['totals']['giftcard']['netto'] = $oBasket->getGiftCardCostNet();
  394. $aExpected['totals']['giftcard']['vat'] = $oBasket->getGiftCardCostVat();
  395. // Delivery costs
  396. $aExpected['totals']['delivery']['brutto'] = number_format(round($oBasket->getDeliveryCosts(), 2), 2, ',', '.');
  397. $aExpected['totals']['delivery']['netto'] = $oBasket->getDelCostNet();
  398. $aExpected['totals']['delivery']['vat'] = $oBasket->getDelCostVat();
  399. // Payment costs
  400. $aExpected['totals']['payment']['brutto'] = number_format(round($oBasket->getPaymentCosts(), 2), 2, ',', '.');
  401. $aExpected['totals']['payment']['netto'] = $oBasket->getPayCostNet();
  402. $aExpected['totals']['payment']['vat'] = $oBasket->getPayCostVat();
  403. // Vouchers
  404. $aExpected['totals']['voucher']['brutto'] = number_format(round($oBasket->getVoucherDiscValue(), 2), 2, ',', '.');
  405. // Total netto & brutto, grand total
  406. $aExpected['totals']['totalNetto'] = $oBasket->getProductsNetPrice();
  407. $aExpected['totals']['totalBrutto'] = $oBasket->getFProductsPrice();
  408. $aExpected['totals']['grandTotal'] = $oBasket->getFPrice();
  409. // Finished generating expectations
  410. return $aExpected;
  411. }
  412. /**
  413. * Generating sql dump of required tables (oxarticles)
  414. */
  415. protected function _generateSqlDump()
  416. {
  417. $dbhost = $this->getConfigParam("dbHost");
  418. $dbuser = $this->getConfigParam("dbUser");
  419. $dbpwd = $this->getConfigParam("dbPwd");
  420. $dbname = $this->getConfigParam("dbName");
  421. $dumpfile = "oxarticles.sql";
  422. passthru("/usr/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname oxarticles > $this->sFilepath/$dumpfile");
  423. echo "$dumpfile ";
  424. passthru("tail -1 $this->sFilepath/$dumpfile");
  425. }
  426. /**
  427. * Truncates specified table
  428. *
  429. * @param string $sTable table name
  430. */
  431. protected function _truncateTable($sTable)
  432. {
  433. return oxDb::getDb()->execute("TRUNCATE {$sTable}");
  434. }
  435. }