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