Signed-off-by: Christian Wolf <github@christianwolf.email>
This commit is contained in:
Christian Wolf 2023-10-09 23:36:58 +02:00
Родитель 6527aa1f32
Коммит a4d7138fd7
3 изменённых файлов: 113 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,18 @@
<?php
namespace OCA\Cookbook\Helper\Filter\JSON;
use OCA\Cookbook\Helper\Filter\AbstractJSONFilter;
/**
* Copy the id from the recipe_id to the id field if there is no id present so far.
*/
class RecipeIdCopyFilter extends AbstractJSONFilter {
public function apply(array &$json): bool {
$copy = $json;
if (! isset($json['id'])) {
$json['id'] = $json['recipe_id'];
}
return $json !== $copy;
}
}

Просмотреть файл

@ -0,0 +1,45 @@
<?php
namespace OCA\Cookbook\Helper\Filter;
use OCA\Cookbook\Helper\Filter\JSON\CleanCategoryFilter;
use OCA\Cookbook\Helper\Filter\JSON\ExtractImageUrlFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixDescriptionFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixDurationsFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixImageSchemeFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixIngredientsFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixInstructionsFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixKeywordsFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixNutritionFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixRecipeYieldFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixToolsFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixUrlFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeIdCopyFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeIdTypeFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeNameFilter;
use OCA\Cookbook\Helper\Filter\JSON\SchemaConformityFilter;
class RecipeStubFilter
{
/** @var AbstractJSONFilter[] */
private $filters;
public function __construct(
RecipeIdTypeFilter $recipeIdTypeFilter,
RecipeIdCopyFilter $recipeIdCopyFilter,
) {
$this->filters = [
$recipeIdCopyFilter,
$recipeIdTypeFilter,
];
}
public function apply(array $json): array
{
foreach ($this->filters as $filter) {
$filter->apply($json);
}
return $json;
}
}

Просмотреть файл

@ -0,0 +1,50 @@
<?php
namespace OCA\Cookbook\tests\Integration\Helper\Filter\JSON;
use OCA\Cookbook\AppInfo\Application;
use OCA\Cookbook\Helper\Filter\RecipeStubFilter;
use PHPUnit\Framework\TestCase;
class RecipeFixIdsTest extends TestCase {
/** @var RecipeStubFilter */
private $dut;
protected function setUp(): void
{
parent::setUp();
$app = new Application();
$container = $app->getContainer();
$this->dut = $container->get(RecipeStubFilter::class);
}
public function dp(){
$stub = [
'name' => 'The name of te recipe',
'date' => '1970-01-12',
];
$src = $stub;
$src['recipe_id'] = 123;
$expected = $stub;
$expected['recipe_id'] = 123;
$expected['id'] = '123';
yield [$src, $expected];
$src['id'] = '234';
$expected['id'] = '234';
yield [$src, $expected];
}
/**
* @dataProvider dp
*/
public function testRecipeStubs($original, $expected){
$ret = $this->dut->apply($original);
$this->assertSame($expected, $ret);
}
}