Skip to content

Commit

Permalink
OXDEV-8357 Fix data-providers
Browse files Browse the repository at this point in the history
  • Loading branch information
liulka-oxid committed May 17, 2024
1 parent f6c846f commit 61896b9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 163 deletions.
46 changes: 7 additions & 39 deletions tests/Integration/Legacy/Price/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,32 @@

require_once __DIR__ . '/BasketConstruct.php';

/**
* Base test case for price calculation tests.
*/
abstract class BaseTestCase extends IntegrationTestCase
{
/**
* Returns test cases from specified paths.
*
* @param array|string $directoriesToScan directory name
* @param array $testCases of specified test cases
*
* @return array
*/
protected function getTestCases($directoriesToScan, $testCases = [])
protected static function getTestCases(array $directoriesToScan): array
{
$directoriesToScan = (array) $directoriesToScan;
$allFiles = [];
foreach ($directoriesToScan as $directory) {
$directory = __DIR__ . "/{$directory}/";
$files = $testCases ? $this->getTestCasesFiles($testCases, $directory) : $this->collectFilesFromPath(
$directory
);
$files = self::collectFilesFromPath($directory);
$allFiles = array_merge($allFiles, $files);
}

return $this->collectDataFromFiles($allFiles);
return self::collectDataFromFiles($allFiles);
}

/**
* @return array
*/
private function collectFilesFromPath(string $path, string $collector = '*.php'): array|false
private static function collectFilesFromPath(string $path): array
{
$files = glob($path . $collector, GLOB_NOSORT);
$files = glob("$path*.php", GLOB_NOSORT);
$directories = glob($path . '*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$files = array_merge($files, $this->collectFilesFromPath($directory));
$files = array_merge($files, self::collectFilesFromPath($directory));
}

return $files;
}

/**
* @param array $testCases
*/
private function getTestCasesFiles($testCases, string $basePath): array
{
$files = [];
foreach ($testCases as $sTestCase) {
$file = $basePath . $sTestCase;
if (file_exists($file)) {
$files[] = $file;
}
}
return $files;
}

private function collectDataFromFiles(array $files): array
private static function collectDataFromFiles(array $files): array
{
$testCaseFiles = [];
foreach ($files as $filePath) {
Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Legacy/Price/BasketConstruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function calculateBasket($testCase)
}
$orderArticle = $basket->addToBasket($article['id'], $article['amount']);
// adding wrapping if need
if (! empty($wrapping)) {
if ($wrapping !== []) {
$orderArticle->setWrapping($wrapping[$article['id']] ?? null);
}
}
Expand All @@ -113,13 +113,13 @@ public function calculateBasket($testCase)
}

// try to add payment
if (! empty($payment)) {
if ($payment !== []) {
$basket->setPayment($payment[0]);
}

// try to add vouchers
$basket->setSkipVouchersChecking(true);
if (! empty($voucherIDs)) {
if ($voucherIDs !== []) {
$count = count($voucherIDs);
for ($i = 0; $i < $count; $i++) {
$basket->addVoucher($voucherIDs[$i]);
Expand All @@ -139,7 +139,7 @@ public function calculateBasket($testCase)
*
* @return array Created articles id's
*/
public function getArticles($articles)
public function getArticles($articles): array
{
return $this->createArticles($articles);
}
Expand Down Expand Up @@ -592,7 +592,7 @@ protected function setVouchers($voucherSeries): array
for ($i = 1; $i <= $voucherData['voucher_count']; $i++) {
$data = [
'oxreserved' => 0,
'oxvouchernr' => md5(uniqid(random_int(0, mt_getrandmax()), true)),
'oxvouchernr' => md5(uniqid((string)random_int(0, mt_getrandmax()), true)),
'oxvoucherserieid' => $voucherSerie->getId(),
];
$voucher = $this->createObj($data, 'oxvoucher', 'oxvouchers');
Expand Down
66 changes: 4 additions & 62 deletions tests/Integration/Legacy/Price/BasketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use oxRegistry;
use PHPUnit\Framework\Attributes\DataProvider;

require_once __DIR__ . '/BaseTestCase.php';

/**
* Basket price calculation test
* Check:
Expand All @@ -28,42 +26,12 @@
*/
final class BasketTest extends BaseTestCase
{
/**
* @var array Test case directory array
*/
private array $testCaseDirectories = ['testcases/basket'];

/**
* @var string Specified test cases (optional)
*/
private array $testCases = [];

/**
* Initialize the fixture.
*/
public function setUp(): void
{
parent::setUp();
$this->reset();
}

/**
* Tear down the fixture.
*/
public function tearDown(): void
{
$this->addTableForCleanup('oxobject2category');
parent::tearDown();
}

/**
* Basket startup data and expected calculations results
*
* @return array
*/
public function providerBasketCalculation()
public static function providerBasketCalculation(): array
{
return $this->getTestCases($this->testCaseDirectories, $this->testCases);
return self::getTestCases(['testcases/basket']);
}

/**
Expand Down Expand Up @@ -91,9 +59,9 @@ public function testBasketCalculation(array $testCase): void
$expectedArticles = $expected['articles'];
$basketItemList = $basket->getContents();

$this->assertEquals(
$this->assertCount(
count($expectedArticles),
count($basketItemList),
$basketItemList,
"Expected basket articles amount doesn't match actual"
);

Expand Down Expand Up @@ -246,30 +214,4 @@ public function testBasketCalculation(array $testCase): void
$this->assertEquals($expected['totals']['totalBrutto'], $basket->getFProductsPrice(), 'Total Brutto');
$this->assertEquals($expected['totals']['grandTotal'], $basket->getFPrice(), 'Grand Total');
}

/**
* Resets db tables, required configs
*/
protected function reset()
{
$database = oxDb::getDb();
$config = oxRegistry::getConfig();
$database->execute('TRUNCATE oxarticles');
$database->execute('TRUNCATE oxcategories');
$database->execute('TRUNCATE oxdiscount');
$database->execute('TRUNCATE oxobject2discount');
$database->execute('TRUNCATE oxwrapping');
$database->execute('TRUNCATE oxdelivery');
$database->execute('TRUNCATE oxdel2delset');
$database->execute('TRUNCATE oxobject2payment');
$database->execute('TRUNCATE oxvouchers');
$database->execute('TRUNCATE oxvoucherseries');
$database->execute('TRUNCATE oxobject2delivery');
$database->execute('TRUNCATE oxobject2category');
$database->execute('TRUNCATE oxdeliveryset');
$database->execute('TRUNCATE oxuser');
$database->execute('TRUNCATE oxprice2article');
$config->setConfigParam('blShowVATForDelivery', true);
$config->setConfigParam('blShowVATForPayCharge', true);
}
}
2 changes: 1 addition & 1 deletion tests/Integration/Legacy/Price/DataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ protected function generateData($i): array
// create discount array
$aData['discounts'] = $this->generateDiscounts($aData);
}
if (! empty($this->aGenCosts)) {
if ($this->aGenCosts !== []) {
// create costs array
$aData['costs'] = $this->generateCosts($aData);
}
Expand Down
16 changes: 2 additions & 14 deletions tests/Integration/Legacy/Price/OrderNumberingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@

final class OrderNumberingTest extends BaseTestCase
{
/**
* @var string Test case directory
*/
private string $testCasesDirectory = 'testcases/numbering';

/**
* @var array Specified test cases (optional)
*/
private array $testCases = [];

public function setUp(): void
{
parent::setUp();
Expand All @@ -46,12 +36,10 @@ public function tearDown(): void

/**
* Order startup data and expected calculations results
*
* @return array
*/
public function providerOrderNumberingForDifferentShops()
public function providerOrderNumberingForDifferentShops(): array
{
return $this->getTestCases($this->testCasesDirectory, $this->testCases);
return $this->getTestCases(['testcases/numbering']);
}

/**
Expand Down
31 changes: 8 additions & 23 deletions tests/Integration/Legacy/Price/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@
#[Group('slow-tests')]
final class OrderTest extends BaseTestCase
{
/**
* @var string Test case directory
*/
private string $testCaseDirectory = 'testcases/order';

/**
* @var array Specified test cases (optional)
*/
private array $testCases = [];

/**
* Initialize the fixture.
*/
public function setUp(): void
{
parent::setUp();
Expand All @@ -62,12 +49,10 @@ public function setUp(): void

/**
* Order startup data and expected calculations results
*
* @return array
*/
public function providerOrderCalculation()
public function providerOrderCalculation(): array
{
return $this->getTestCases($this->testCaseDirectory, $this->testCases);
return $this->getTestCases(['testcases/order']);
}

/**
Expand Down Expand Up @@ -130,7 +115,7 @@ public function testOrderCalculation($testCase): void
/**
* Resets db tables, required configs
*/
protected function reset()
protected function reset(): void
{
$database = oxDb::getDb();
$database->execute('TRUNCATE oxarticles');
Expand All @@ -156,7 +141,7 @@ protected function reset()
* @param int $approach number of order recalculate events starting at 1
* @param oxOrder $order
*/
protected function checkTotals(array $expected, $approach, $order)
private function checkTotals(array $expected, int $approach, $order): void
{
$expectedTotals = $expected[$approach]['totals'];
$articles = $expected[$approach]['articles'];
Expand Down Expand Up @@ -254,7 +239,7 @@ protected function checkTotals(array $expected, $approach, $order)
*
* @param array $configOptions
*/
protected function changeConfigs($configOptions)
private function changeConfigs($configOptions): void
{
$config = oxRegistry::getConfig();
if (! empty($configOptions)) {
Expand All @@ -270,7 +255,7 @@ protected function changeConfigs($configOptions)
* @param array $articlesData new articles to add
* @param object $order
*/
protected function addArticles($articlesData, $order)
private function addArticles($articlesData, $order): void
{
$basketConstruct = new BasketConstruct();
$articles = $basketConstruct->getArticles($articlesData);
Expand All @@ -295,7 +280,7 @@ protected function addArticles($articlesData, $order)
* @param array $articleIds article id's to remove
* @param object $order
*/
protected function removeArticles(array $articleIds, $order)
private function removeArticles(array $articleIds, $order): void
{
$articleCount = count($articleIds);
$orderArticles = $order->getOrderArticles();
Expand All @@ -314,7 +299,7 @@ protected function removeArticles(array $articleIds, $order)
*
* @param object $order
*/
protected function changeArticles(array $articleAmounts, $order)
private function changeArticles(array $articleAmounts, $order): void
{
$articlesCount = count($articleAmounts);
$orderArticles = $order->getOrderArticles();
Expand Down
24 changes: 5 additions & 19 deletions tests/Integration/Legacy/Price/PriceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@
*/
final class PriceTest extends BaseTestCase
{
/**
* @var array Test case directories.
*/
private string $testCasesDirectory = 'testcases/price';

/**
* @var array If specified, runs only these test cases.
*/
private array $testCases = [];

/**
* Initialize the fixture.
*/
public function setUp(): void
{
parent::setUp();
Expand All @@ -45,16 +32,15 @@ public function setUp(): void

/**
* Order startup data and expected calculations results
*
* @return array
*/
public function providerPrice()
public function providerPrice(): array
{
$directoriesToScan = [$this->testCasesDirectory . '/community/'];
$testCasesDirectory = 'testcases/price';
$directoriesToScan = [$testCasesDirectory . '/community/'];
if ($this->getTestConfig()->getShopEdition() === 'EE') {
$directoriesToScan[] = $this->testCasesDirectory . '/enterprise/';
$directoriesToScan[] = $testCasesDirectory . '/enterprise/';
}
return $this->getTestCases($directoriesToScan, $this->testCases);
return $this->getTestCases($directoriesToScan);
}

/**
Expand Down

0 comments on commit 61896b9

Please sign in to comment.