Skip to content

Commit

Permalink
added RuleHelperTest
Browse files Browse the repository at this point in the history
  • Loading branch information
TonisOrmisson committed Oct 12, 2024
1 parent e636e64 commit 8a0768a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ class Translator
* @param array<mixed> $data Rules configuraion
* @param ?string $paramPrefix prefix added to parameters, to be changed in case of multiple translator params being merged
*/
public function __construct(private readonly array $data, ?string $paramPrefix = null)
public function __construct(private readonly array|Rule $data, ?string $paramPrefix = null)
{
$this->init();
if(!is_null($paramPrefix)){
$this->paramPrefix = $paramPrefix;
}
/** @var Rule $rules */
$rules = \Yii::createObject(array_merge([
'class' => Rule::class,
],$this->data));
$rules = $this->data;
if(is_array($this->data)) {
/** @var Rule $rules */
$rules = \Yii::createObject(array_merge([
'class' => Rule::class,
],$this->data));
}

$this->_where = $this->buildWhere($rules);

Check failure on line 68 in src/Translator.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.2)

Parameter #1 $rule of method leandrogehlen\querybuilder\Translator::buildWhere() expects leandrogehlen\querybuilder\Rule, array|leandrogehlen\querybuilder\Rule given.

Check failure on line 68 in src/Translator.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.3)

Parameter #1 $rule of method leandrogehlen\querybuilder\Translator::buildWhere() expects leandrogehlen\querybuilder\Rule, array|leandrogehlen\querybuilder\Rule given.

Expand Down
84 changes: 84 additions & 0 deletions tests/RuleHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace leandrogehlen\querybuilder\tests;

use leandrogehlen\querybuilder\Rule;
use leandrogehlen\querybuilder\RuleHelper;
use leandrogehlen\querybuilder\Translator;

class RuleHelperTest extends TestCase
{


/**
* @dataProvider rulesProvider
*/
public function testPrefixesGetAdded(?string $paramPrefix,array $ruleData, array $expected1,?string $tablePrefix, string $expected)
{
/** @var RuleHelper $helper */
$helper = \Yii::createObject(RuleHelper::class);

/** @var Rule $rules */
$rules = \Yii::createObject(array_merge([
'class' => Rule::class,
],$ruleData));

$rules = $helper->addPrefixToRules($rules, $tablePrefix);
/** @var Translator $translator */
$translator = \Yii::createObject(Translator::class, [$rules, $paramPrefix]);
$this->assertEquals($expected, $translator->where());
}


public static function rulesProvider()
{
$baseData = TranslatorTest::rulesProvider();
$newData = [];
$addParams = [
[
'myTableName',
'(myTableName.name = :p_0 and myTableName.name <> :p_1)',
],
[
'table2',
'(table2.id IN (:a_0, :a_1, :a_2) and table2.id NOT IN (:a_3, :a_4))',
],
[
'table3',
'(table3.id < :b_0 and table3.id <= :b_1)',
],
[
'table4',
'(table4.id > :c_0 and table4.id >= :c_1)',
],
[
'table5',
'(table5.date BETWEEN :d_0 AND :d_1)',
],
[
'table6',
'(table6.date NOT BETWEEN :e_0 AND :e_1)',
],
[
'table7',
'(table7.name LIKE :f_0 and table7.name NOT LIKE :f_1)',
],
[
'table8',
'(table8.name LIKE :g_0 and table8.name NOT LIKE :g_1)',
],
[
'table9',
'(table9.name LIKE :h_0 and table9.name NOT LIKE :h_1)',
],

];
foreach ($addParams as $key => $item) {
$newData[$key] = $baseData[$key];
$newData[$key][] = $item[0];
$newData[$key][] = $item[1];
}
return $newData;
}

}

0 comments on commit 8a0768a

Please sign in to comment.