-
Notifications
You must be signed in to change notification settings - Fork 0
/
Csv.php
166 lines (150 loc) · 7.92 KB
/
Csv.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Test\Base;
use Quid\Base;
// csv
// class for testing Quid\Base\Csv
class Csv extends Base\Test
{
// trigger
final public static function trigger(array $data):bool
{
// prepare
$currentFile = Base\Finder::path('[assertCommon]/class.php');
$mediaCsv = '[assertMedia]/csv.csv';
$storage = '[assertCurrent]';
$csvRes = Base\Csv::open($mediaCsv);
$fileRes = Base\File::open($currentFile);
$temp = Base\File::prefix('[assertCurrent]');
$res = Base\File::resource($temp);
$_file_ = Base\Finder::normalize('[assertCommon]/class.php');
$_dir_ = dirname($_file_);
assert(Base\Dir::reset($storage));
assert(Base\File::set('[assertCurrent]/test.php','WHAT'));
assert(Base\Csv::open($currentFile) === null);
// getFormat
assert(count(Base\Csv::getFormat()) === 3);
// same
$get = Base\Csv::getLines($csvRes);
assert(Base\Csv::same($get));
assert(!Base\Csv::same([[1],[2,3]]));
assert(count($get) === 15);
// clean
assert(count(Base\Csv::clean($get,true)) === 10);
// assoc
$assoc = Base\Csv::assoc($get);
assert($assoc[0]['Item Code'] === '15v3-01');
$splice = Base\Arr::splice(1,10,$assoc);
// list
assert(Base\Csv::list($assoc) === $get);
// strToArr
assert(Base\Csv::strToArr("test;test2é;\"test3\ntest4\";\"testqu;ote\"") === [['test','test2é',"test3\ntest4",'testqu;ote']]);
assert(Base\Csv::strToArr('') === null);
assert(count(Base\Csv::strToArr(["test;test2é;\"test3\ntest4\";\"testqu;ote\"",'',"test;test2é;\"test3\ntest4\";\"testqu;ote\""])) === 2);
// arrToStr
assert(strlen(Base\Csv::arrToStr($splice[0])) === 83);
assert(strlen(Base\Csv::arrToStr($splice)) > 100);
// prepareContent
assert(Base\Csv::prepareContent('ok') === [['ok']]);
assert(Base\Csv::prepareContent(['ok','what']) === [['ok','what']]);
$line = [['ok','what']];
// prepareContentPrepend
assert(Base\Csv::prepareContentPrepend($line,$csvRes)[0] === ['ok','what']);
assert(Base\Res::seekRewind($csvRes));
// resLine
assert(is_array(Base\Csv::resLine($csvRes)));
// resWrite
assert(Base\Csv::resWrite($line,$res) === false);
// file
assert(Base\Csv::is($csvRes));
assert(Base\Csv::isResource($csvRes));
assert(!Base\Csv::isResource($fileRes));
assert(!Base\Csv::isReadable($currentFile));
assert(!Base\Csv::isReadable($_dir_));
assert(Base\Csv::isReadable($mediaCsv));
assert(Base\Csv::isReadable($csvRes));
assert(!Base\Csv::isWritable($currentFile));
assert(!Base\Csv::isWritable($_dir_));
assert(Base\Csv::isWritable($mediaCsv));
assert(!Base\Csv::isEmpty($currentFile));
assert(!Base\Csv::isEmpty($_dir_));
assert(!Base\Csv::isEmpty($mediaCsv));
assert(!Base\Csv::isEmpty($csvRes));
assert(!Base\Csv::isNotEmpty($currentFile));
assert(!Base\Csv::isNotEmpty($_dir_));
assert(Base\Csv::isNotEmpty($mediaCsv));
assert(Base\Csv::isNotEmpty($csvRes));
assert(Base\Csv::option(['test'=>'deux']) === ['csv'=>true,'test'=>'deux','useIncludePath'=>true]);
assert(Base\Csv::stat($_dir_) === null);
assert(is_array(Base\Csv::stat($csvRes)));
assert(Base\Csv::stat($fileRes) === null);
assert(Base\Csv::stat($currentFile) === null);
assert(Base\Csv::mime($currentFile) === null);
assert(Base\Csv::info($currentFile) === null);
assert(Base\Csv::mime($mediaCsv) === 'text/csv');
assert(count(Base\Csv::info($mediaCsv)) === 13);
$prefix = Base\Csv::prefix();
assert(Base\Path::extension($prefix) === 'csv');
assert(Base\Finder::unlink($prefix));
assert(Base\Csv::getLoadPath('view/test.php') === '');
assert(Base\Csv::getLoadPath('view/test.csv') === 'view/test.csv');
assert(!empty(Base\Csv::path('[assertCurrent]/test.csv')));
assert(Base\Csv::path('[assertCurrent]/test.php') === null);
assert(count(Base\Csv::lines(0,3,$mediaCsv)) === 3);
assert(Base\Csv::getLines($mediaCsv,0,3) === Base\Csv::lines(0,3,$csvRes));
assert(count(Base\Csv::lineFirst($mediaCsv)) === 12);
assert(count(Base\Csv::lineLast($mediaCsv)) === 12);
assert(is_string(Base\Csv::get($mediaCsv)));
assert(strlen(Base\Csv::read(0,200,$mediaCsv)) === 200);
assert(Base\Csv::path($mediaCsv) === Base\Csv::shortcut($mediaCsv));
assert(Base\Csv::path($mediaCsv.'.php') === null);
assert(is_array(Base\Csv::line($csvRes)) && Base\Csv::line($csvRes) !== Base\Csv::line($csvRes));
assert(Base\Csv::lineCount($mediaCsv) < Base\File::lineCount($mediaCsv));
assert(Base\Csv::subCount('King',$csvRes) === Base\File::subCount('King',$csvRes));
assert(count(Base\Csv::lineChunk(10,$csvRes)) === 2);
assert(Base\Csv::set('[assertCurrent]/test.php') === null);
assert(Base\Csv::set('[assertCurrent]/test.csv',[['WHAT','ok'],['TE" ST','LOL']]));
assert(strlen(Base\Csv::get('[assertCurrent]/test.csv')) === 22);
assert(Base\File::set('[assertCurrent]/test.txt',[['WHAT','ok'],['TE" ST','LOL']]));
assert(strlen(Base\File::get('[assertCurrent]/test.txt')) === 33);
assert(Base\Path::isExtension('csv',Base\Csv::setFilenameExtension('[assertCurrent]','filenameext')));
assert(Base\Csv::write(['WRITE','OKzzzdasds'],'[assertCurrent]/test.csv'));
assert(strlen(Base\Csv::get('[assertCurrent]/test.csv')) === 22);
assert(Base\Csv::overwrite([['WRITEwqewe','OKzzzdasds'],['LOL','OUI']],'[assertCurrent]/test.csv'));
assert(strlen(Base\Csv::get('[assertCurrent]/test.csv')) === 30);
assert(Base\Csv::prepend(['pré','deux'],'[assertCurrent]/test.csv'));
assert(in_array(strlen(Base\Csv::get('[assertCurrent]/test.csv')),[40,43],true));
assert(Base\Csv::append(['appé','troi'],'[assertCurrent]/test.csv'));
assert(in_array(strlen(Base\Csv::get('[assertCurrent]/test.csv')),[51,54],true));
assert(Base\Csv::appendNewline(['appé','troi'],'[assertCurrent]/test.csv'));
assert(in_array(strlen(Base\Csv::get('[assertCurrent]/test.csv')),[62,65],true));
assert(Base\Csv::lineSplice(0,1,'[assertCurrent]/test.csv',['testzzz','test2zzz'])[0] === ['testzzz','test2zzz']);
assert(Base\Csv::lineSplice(1,3,'[assertCurrent]/test.csv',[['test','test2'],['test3','test4']],true)[1] === ['test','test2']);
assert(Base\Csv::getLines('[assertCurrent]/test.csv')[1] === ['test','test2']);
assert(count(Base\Csv::lineSpliceFirst('[assertCurrent]/test.csv',null,true)) === 3);
assert(count(Base\Csv::lineSpliceLast('[assertCurrent]/test.csv',null,true)) === 2);
assert(count(Base\Csv::getLines('[assertCurrent]/test.csv')) === 2);
assert(Base\Csv::lineInsert(0,[['what',2],['ok',['TEST']]],'[assertCurrent]/test.csv',false)[1][1] === ['TEST']);
assert(count(Base\Csv::lineFilter(function($v,$k) {
assert(is_array($v));
return true;
},'[assertCurrent]/test.csv')) === 2);
assert(Base\Csv::lineMap(function($v,$k) {
$v[2] = 'TEST';
return $v;
},'[assertCurrent]/test.csv')[0] === ['test','test2','TEST']);
assert(Base\Csv::changeBasename('test2.csv','[assertCurrent]/test.csv'));
assert(Base\Csv::empty('[assertCurrent]/test2.csv'));
assert(!Base\Csv::is($currentFile));
assert(!Base\Csv::is($_dir_));
assert(Base\Csv::is($mediaCsv));
assert(Base\Csv::is($csvRes));
return true;
}
}
?>