fixed the unit test

This commit is contained in:
Nick 2022-05-27 11:24:04 +02:00
parent a4aa8444e3
commit fdf00d5c95
4 changed files with 24 additions and 41 deletions

View File

@ -31,8 +31,9 @@ final class TimeBasedFieldUpdater {
$query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery();
$conditions[] = [$this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<"];
$conditions[] = ['type', $this->bundle, '='];
foreach ($conditions as $condition) {
$query->condition($condition);
$query->condition($condition[0], $condition[1], $condition[2] ?? "=");
}
foreach($query->execute() as $id) {
$this->updateEntity($id, $new_values);

View File

@ -17,15 +17,6 @@ class Utils {
$this->entityTypeManager = $entityTypeManager;
}
public function addConditionsToQuery(QueryInterface $query, array $conditions): void {
foreach($conditions as $condition) {
$field = $condition[0];
$value = $condition[1];
$operator = isset($condition[2]) ? $condition[2] : "=";
$query->condition($field, $value, $operator);
}
}
/**
* Utility: find term by name and vid.
*

View File

@ -48,13 +48,13 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
function testBundleAndDateAndExtraConditionsAreAllAddedAsQueryConditions(): void {
$this->query->method('execute')->willReturn([]);
$expected_conditions= [
$this->query->expects($this->exactly(4))->method('condition')->withConsecutive(
['dummy_field', 'dummy_value', '<'],
['dummy_field_2', 'dummy_value_2', '='],
['dummy_date_field', date('Y-m-d', strtotime('- 3 months')), "<"],
['type', 'dummy_bundle', '=']
];
$this->utils->expects($this->once())->method('addConditionsToQuery')->with($this->query, $expected_conditions);
['type', 'dummy_bundle', '=']);
$this->updater->update([['dummy_field', 'dummy_value', '<'], ['dummy_field_2', 'dummy_value_2', '='] ], '3 months', ['dummy_field' => 4]);
}
}

View File

@ -22,13 +22,4 @@ class UtilsTest extends UnitTestCase{
->willReturn([$term_entity]);
$this->assertEquals($this->utils->getTidByName('foo', 'bar'), 3);
}
public function testAddConditionToQueryAddsEqualsIfNoOperatorProvided():void {
$this->query->expects($this->exactly(1))->method('condition')->with("1", "2", "=");
$this->utils->addConditionsToQuery($this->query, [["1","2"]]);
}
public function testAddConditionToQueryAddsTheRightAmountOfConditions():void {
$this->query->expects($this->exactly(4))->method('condition');
$this->utils->addConditionsToQuery($this->query, [["1","2","3"], ["lk", "n", "kk"], ['sfd', 'ds', 'fds'], ["1","2","3"]]);
}
}