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
94
95
96
97
98
99
100
101
102
103
104
105
106
|
<?php
namespace League\Csv\test;
use DateTime;
use League\Csv\Reader;
use PHPUnit_Framework_TestCase;
use SplFileInfo;
use SplFileObject;
use SplTempFileObject;
use StdClass;
date_default_timezone_set('UTC');
/**
* @group factory
*/
class FactoryTest extends PHPUnit_Framework_TestCase
{
public function testCreateFromPathWithFilePath()
{
$path = __DIR__.'/foo.csv';
$csv = Reader::createFromPath($path);
$this->assertSame($path, $csv->getIterator()->getRealPath());
}
public function testCreateFromPathWithFileObject()
{
$path = __DIR__.'/foo.csv';
$csv = Reader::createFromPath(new SplFileInfo($path));
$this->assertSame($path, $csv->getIterator()->getRealPath());
}
public function testConstructorWithSplFileInfo()
{
$path = __DIR__.'/foo.csv';
$csv = new Reader(new SplFileInfo($path));
$this->assertSame($path, $csv->getIterator()->getRealPath());
}
public function testCreateFromPathWithPHPWrapper()
{
$path = __DIR__.'/foo.csv';
$csv = Reader::createFromPath('php://filter/read=string.toupper/resource='.$path);
$this->assertFalse($csv->getIterator()->getRealPath());
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage an `SplTempFileObject` object does not contain a valid path
*/
public function testCreateFromPathWithSplTempFileObject()
{
Reader::createFromPath(new SplTempFileObject());
}
public function testCreateFromString()
{
$expected = "john,doe,john.doe@example.com".PHP_EOL
."jane,doe,jane.doe@example.com".PHP_EOL;
$reader = Reader::createFromString($expected);
$this->assertInstanceof('League\Csv\Reader', $reader);
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testCreateFromStringThrowExceptionWithBadNewline()
{
$expected = "john,doe,john.doe@example.com".PHP_EOL
."jane,doe,jane.doe@example.com".PHP_EOL;
Reader::createFromString($expected, new \StdClass);
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testCreateFromStringFromNotStringableObject()
{
Reader::createFromString(new DateTime());
}
public function testCreateFromFileObject()
{
$reader = Reader::createFromFileObject(new SplTempFileObject());
$this->assertInstanceof('League\Csv\Reader', $reader);
$this->assertInstanceof('SplTempFileObject', $reader->getIterator());
}
public function testCreateFromFileObjectWithSplFileObject()
{
$path = __DIR__.'/foo.csv';
$obj = new SplFileObject($path);
$reader = Reader::createFromFileObject($obj);
$this->assertInstanceof('League\Csv\Reader', $reader);
$this->assertInstanceof('SplFileObject', $reader->getIterator());
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testCreateFromFileObjectFailed()
{
Reader::createFromFileObject(new StdClass());
}
}
|