|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SubjectivePHPTest\Csv; |
| 4 | + |
| 5 | +use SplFileObject; |
| 6 | +use SubjectivePHP\Csv\HeaderStrategy; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * @coversDefaultClass \SubjectivePHP\Csv\HeaderStrategy |
| 11 | + * @covers ::getHeaders |
| 12 | + * @covers ::<private> |
| 13 | + */ |
| 14 | +final class HeaderStrategyTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @test |
| 18 | + * @covers ::derive |
| 19 | + */ |
| 20 | + public function derive() |
| 21 | + { |
| 22 | + $fileObject = $this->getFileObject('pipe_delimited.txt', '|'); |
| 23 | + $strategy = HeaderStrategy::derive(); |
| 24 | + $this->assertSame( |
| 25 | + ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description'], |
| 26 | + $strategy->getHeaders($fileObject) |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @test |
| 32 | + * @covers ::provide |
| 33 | + */ |
| 34 | + public function provide() |
| 35 | + { |
| 36 | + $headers = ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description']; |
| 37 | + $fileObject = $this->getFileObject('basic.csv'); |
| 38 | + $strategy = HeaderStrategy::provide($headers); |
| 39 | + $this->assertSame($headers, $strategy->getHeaders($fileObject)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @test |
| 44 | + * @covers ::none |
| 45 | + */ |
| 46 | + public function none() |
| 47 | + { |
| 48 | + $fileObject = $this->getFileObject('no_headers.csv'); |
| 49 | + $strategy = HeaderStrategy::none(); |
| 50 | + $this->assertSame( |
| 51 | + [0, 1, 2, 3, 4, 5, 6], |
| 52 | + $strategy->getHeaders($fileObject) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function getFileObject(string $fileName, string $delimiter = ',') : SplFileObject |
| 57 | + { |
| 58 | + $fileObject = new SplFileObject(__DIR__ . "/_files/{$fileName}"); |
| 59 | + $fileObject->setFlags(SplFileObject::READ_CSV); |
| 60 | + $fileObject->setCsvControl($delimiter); |
| 61 | + return $fileObject; |
| 62 | + } |
| 63 | +} |
0 commit comments