1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace itertools; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class FileLineIteratorTest extends PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** @test */ |
11
|
|
|
public function testReadingFileLines() |
12
|
|
|
{ |
13
|
|
|
$fp = $this->getMemoryFileHandle(<<<EOF |
14
|
|
|
line1 |
15
|
|
|
line2 |
16
|
|
|
line3 |
17
|
|
|
EOF |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
$lines = iterator_to_array(new FileLineIterator($fp)); |
21
|
|
|
$this->assertEquals(array('line1', 'line2', 'line3'), $lines); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @test */ |
25
|
|
|
public function testClosingHandle() |
26
|
|
|
{ |
27
|
|
|
$lines = new FileLineIterator(__FILE__); |
28
|
|
|
$lines->__destruct(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @test */ |
32
|
|
|
public function testReadingFileLinesFromFile() |
33
|
|
|
{ |
34
|
|
|
$lines = iterator_to_array(new FileLineIterator(__FILE__)); |
35
|
|
|
$this->assertGreaterThan(10, $lines); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
* @expectedException InvalidArgumentException |
41
|
|
|
*/ |
42
|
|
|
public function testFileLineIteratorWithInvalidOptions() |
43
|
|
|
{ |
44
|
|
|
new FileLineIterator(__FILE__, array('unknownOption' => true)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
* @expectedException InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
public function testFileLineIteratorWithNonExistingFilePath() |
52
|
|
|
{ |
53
|
|
|
new FileLineIterator(uniqid()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
* @expectedException InvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
public function testFileLineIteratorWithInvalidArguments() |
61
|
|
|
{ |
62
|
|
|
new FileLineIterator(1); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @test */ |
66
|
|
|
public function testFileCsvWithCustomCharacterEncoding() |
67
|
|
|
{ |
68
|
|
|
$data = iterator_to_array(new FileLineIterator(__DIR__ . '/testdata/testline-latin1.txt', array('fromEncoding' => 'ISO-8859-1'))); |
69
|
|
|
$this->assertEquals("Josephin\xc3\xa8", $data[0]); |
70
|
|
|
$this->assertEquals("Albr\xc3\xa9ne", $data[1]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getMemoryFileHandle($content) |
74
|
|
|
{ |
75
|
|
|
$fp = fopen('php://memory', 'rw'); |
76
|
|
|
fwrite($fp, $content); |
77
|
|
|
rewind($fp); |
78
|
|
|
return $fp; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|