Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/batch/src/Job/Item/Reader/ParameterAccessorReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Job\Item\Reader;

use Yokai\Batch\Job\Item\ItemReaderInterface;
use Yokai\Batch\Job\JobExecutionAwareInterface;
use Yokai\Batch\Job\JobExecutionAwareTrait;
use Yokai\Batch\Job\Parameters\JobParameterAccessorInterface;

/**
* This {@see ItemReaderInterface} uses a {@see JobParameterAccessorInterface} to read data from.
*/
final class ParameterAccessorReader implements ItemReaderInterface, JobExecutionAwareInterface
{
use JobExecutionAwareTrait;

private JobParameterAccessorInterface $data;

public function __construct(JobParameterAccessorInterface $data)
{
$this->data = $data;
}

/**
* @inheritdoc
*/
public function read(): iterable
{
$data = $this->data->get($this->jobExecution);
if (\is_iterable($data)) {
return $data;
}

return [$data];
}
}
44 changes: 44 additions & 0 deletions src/batch/tests/Job/Item/Reader/ParameterAccessorReaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Tests\Job\Item\Reader;

use Generator;
use PHPUnit\Framework\TestCase;
use Yokai\Batch\Job\Item\Reader\ParameterAccessorReader;
use Yokai\Batch\Job\Parameters\JobParameterAccessorInterface;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
use Yokai\Batch\JobExecution;

class ParameterAccessorReaderTest extends TestCase
{
/**
* @dataProvider provider
*/
public function test(JobParameterAccessorInterface $accessor, array $expected): void
{
$reader = new ParameterAccessorReader($accessor);
$reader->setJobExecution(JobExecution::createRoot('123456', 'testing'));

$actual = [];
foreach ($reader->read() as $idx => $item) {
$actual[$idx] = $item;
}

self::assertSame($expected, $actual);
}

public function provider(): Generator
{
yield 'Read from preserved iterable' => [
new StaticValueParameterAccessor([1 => 'One', 2 => 'Two', 3 => 'Three']),
[1 => 'One', 2 => 'Two', 3 => 'Three']
];

yield 'Read from static' => [
new StaticValueParameterAccessor('Not iterable and converted to array'),
['Not iterable and converted to array']
];
}
}