-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParserTest.php
93 lines (86 loc) · 2.71 KB
/
ParserTest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php declare(strict_types=1);
/*
* This file is part of sebastian/csv-parser.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CsvParser;
use function iterator_to_array;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(Parser::class)]
#[UsesClass(Schema::class)]
#[UsesClass(ColumnDefinition::class)]
#[UsesClass(Type::class)]
#[UsesClass(FloatType::class)]
#[UsesClass(IntegerType::class)]
#[UsesClass(StringType::class)]
#[UsesClass(BooleanType::class)]
#[Small]
final class ParserTest extends TestCase
{
public static function provider(): array
{
return [
'all columns' => [
[
[
'a' => 1,
'b' => 2.0,
'c' => '3',
'd' => true,
'e' => false,
],
],
Schema::from(
[
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
ColumnDefinition::from(5, 'e', Type::boolean()),
]
),
__DIR__ . '/../fixture/fixture.csv',
],
'subset of columns' => [
[
[
'b' => 2.0,
],
],
Schema::from(
[
ColumnDefinition::from(2, 'b', Type::float()),
]
),
__DIR__ . '/../fixture/fixture.csv',
],
];
}
#[DataProvider('provider')]
public function test_Parses_CSV_file_according_to_schema(array $expected, Schema $schema, string $filename): void
{
$parser = new Parser;
$this->assertSame(
$expected,
iterator_to_array(
$parser->parse(
$filename,
$schema
)
)
);
}
public function test_Cannot_read_from_CSV_file_that_does_not_exist(): void
{
$this->expectException(CannotReadCsvFileException::class);
(new Parser)->parse('does_not_exist.csv', Schema::from([]));
}
}