/
RecordCollectionTest.php
69 lines (57 loc) · 1.89 KB
/
RecordCollectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* League.Csv (https://csv.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Csv\Doctrine;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
use League\Csv\Reader;
use League\Csv\Statement;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
#[Group('reader')]
final class RecordCollectionTest extends TestCase
{
private Reader $csv;
protected function setUp(): void
{
$this->csv = Reader::createFromPath(dirname(__DIR__, 2).'/test_files/prenoms.csv');
$this->csv->setDelimiter(';');
$this->csv->setHeaderOffset(0);
}
public function testConstructorWithReader(): void
{
self::assertCount(10121, new RecordCollection($this->csv));
}
public function testDoInitialize(): void
{
$result = Statement::create()
->offset(10)
->limit(15)
->process($this->csv);
self::assertCount(15, new RecordCollection($result));
}
public function testMatching(): void
{
/** @var resource $fp */
$fp = tmpfile();
fputcsv($fp, ['field 1', 'field 2', 'field 3']);
fputcsv($fp, ['foo', 'bar', 'baz']);
fputcsv($fp, ['foofoo', 'barbar', 'bazbaz']);
$csv = Reader::createFromStream($fp);
$csv->setHeaderOffset(0);
$collection = new RecordCollection($csv);
self::assertSame([
1 => ['field 1' => 'foo', 'field 2' => 'bar', 'field 3' => 'baz'],
2 => ['field 1' => 'foofoo', 'field 2' => 'barbar', 'field 3' => 'bazbaz'],
], $collection->matching(new Criteria(null, ['field 1' => Order::Ascending]))->toArray());
$csv = null;
fclose($fp);
}
}