|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yokai\Batch\Tests\Job\Item\Reader\Filesystem; |
| 6 | + |
| 7 | +use Generator; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Yokai\Batch\Exception\RuntimeException; |
| 10 | +use Yokai\Batch\Exception\UnexpectedValueException; |
| 11 | +use Yokai\Batch\Job\Item\Reader\Filesystem\FixedColumnSizeFileReader; |
| 12 | +use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor; |
| 13 | +use Yokai\Batch\JobExecution; |
| 14 | + |
| 15 | +class FixedColumnSizeFileReaderTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @dataProvider config |
| 19 | + */ |
| 20 | + public function test(array $columns, string $headersMode, array $expected): void |
| 21 | + { |
| 22 | + $execution = JobExecution::createRoot('123456', 'testing'); |
| 23 | + $reader = new FixedColumnSizeFileReader( |
| 24 | + $columns, |
| 25 | + new StaticValueParameterAccessor(__DIR__ . '/fixtures/fixed-column-size.txt'), |
| 26 | + $headersMode |
| 27 | + ); |
| 28 | + $reader->setJobExecution($execution); |
| 29 | + self::assertSame($expected, \iterator_to_array($reader->read())); |
| 30 | + } |
| 31 | + |
| 32 | + public function testInvalidHeaderMode(): void |
| 33 | + { |
| 34 | + $this->expectException(UnexpectedValueException::class); |
| 35 | + new FixedColumnSizeFileReader([10, 10], new StaticValueParameterAccessor(null), 'wrong header mode'); |
| 36 | + } |
| 37 | + |
| 38 | + public function testFileNotFound(): void |
| 39 | + { |
| 40 | + $this->expectException(RuntimeException::class); |
| 41 | + $execution = JobExecution::createRoot('123456', 'testing'); |
| 42 | + $reader = new FixedColumnSizeFileReader( |
| 43 | + [10, 10], |
| 44 | + new StaticValueParameterAccessor(__DIR__ . '/fixtures/unknown-file.ext') |
| 45 | + ); |
| 46 | + $reader->setJobExecution($execution); |
| 47 | + \iterator_to_array($reader->read()); |
| 48 | + } |
| 49 | + |
| 50 | + public function config(): Generator |
| 51 | + { |
| 52 | + $columnsWithoutNames = [10, 9, 8, -1]; |
| 53 | + $columnsWithNames = ['firstName' => 10, 'lastName' => 9, 'country' => 8, 'city' => -1]; |
| 54 | + |
| 55 | + yield [ |
| 56 | + $columnsWithoutNames, |
| 57 | + FixedColumnSizeFileReader::HEADERS_MODE_COMBINE, |
| 58 | + [ |
| 59 | + ['firstName' => 'John', 'lastName' => 'Doe', 'country' => 'USA', 'city' => 'Washington'], |
| 60 | + ['firstName' => 'Jane', 'lastName' => 'Doe', 'country' => 'USA', 'city' => 'Seattle'], |
| 61 | + ['firstName' => 'Jack', 'lastName' => 'Doe', 'country' => 'USA', 'city' => 'San Francisco'], |
| 62 | + ], |
| 63 | + ]; |
| 64 | + yield [ |
| 65 | + $columnsWithNames, |
| 66 | + FixedColumnSizeFileReader::HEADERS_MODE_SKIP, |
| 67 | + [ |
| 68 | + ['firstName' => 'John', 'lastName' => 'Doe', 'country' => 'USA', 'city' => 'Washington'], |
| 69 | + ['firstName' => 'Jane', 'lastName' => 'Doe', 'country' => 'USA', 'city' => 'Seattle'], |
| 70 | + ['firstName' => 'Jack', 'lastName' => 'Doe', 'country' => 'USA', 'city' => 'San Francisco'], |
| 71 | + ], |
| 72 | + ]; |
| 73 | + yield [ |
| 74 | + $columnsWithoutNames, |
| 75 | + FixedColumnSizeFileReader::HEADERS_MODE_NONE, |
| 76 | + [ |
| 77 | + ['firstName', 'lastName', 'country', 'city'], |
| 78 | + ['John', 'Doe', 'USA', 'Washington'], |
| 79 | + ['Jane', 'Doe', 'USA', 'Seattle'], |
| 80 | + ['Jack', 'Doe', 'USA', 'San Francisco'], |
| 81 | + ], |
| 82 | + ]; |
| 83 | + yield [ |
| 84 | + $columnsWithoutNames, |
| 85 | + FixedColumnSizeFileReader::HEADERS_MODE_SKIP, |
| 86 | + [ |
| 87 | + ['John', 'Doe', 'USA', 'Washington'], |
| 88 | + ['Jane', 'Doe', 'USA', 'Seattle'], |
| 89 | + ['Jack', 'Doe', 'USA', 'San Francisco'], |
| 90 | + ], |
| 91 | + ]; |
| 92 | + } |
| 93 | +} |
0 commit comments