1
|
|
|
<?php |
2
|
|
|
namespace Tests\Werkspot\BingAdsApiBundle\Api\Helper; |
3
|
|
|
|
4
|
|
|
use PHPUnit_Framework_TestCase; |
5
|
|
|
use Werkspot\BingAdsApiBundle\Api\Helper\Csv; |
6
|
|
|
|
7
|
|
|
class CsvTest extends PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
public function testRemoveOneLastLine() |
10
|
|
|
{ |
11
|
|
|
$csvArray = $this->getCsvArray(); |
12
|
|
|
|
13
|
|
|
$csvHelper = new Csv(); |
14
|
|
|
$result = $csvHelper->removeLastLines($csvArray); |
15
|
|
|
|
16
|
|
|
$this->assertEquals((count($csvArray) - 1), count($result)); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testRemoveTwoLastLine() |
20
|
|
|
{ |
21
|
|
|
$csvArray = $this->getCsvArray(); |
22
|
|
|
|
23
|
|
|
$csvHelper = new Csv(); |
24
|
|
|
$result = $csvHelper->removeLastLines($csvArray, 2); |
25
|
|
|
|
26
|
|
|
$this->assertEquals((count($csvArray) - 2), count($result)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testFixDate() |
30
|
|
|
{ |
31
|
|
|
$csvArray = [ |
32
|
|
|
'"11/11/1988","account_1","1","group 1","1","0","EUR","0.00","Italy","","Bolzano","","Bolzano"', |
33
|
|
|
'"4/22/2007","account_1","2","group 2","4","0","EUR","0.00","Italy","","Milan","","Milan"', |
34
|
|
|
]; |
35
|
|
|
$csvHelper = new Csv(); |
36
|
|
|
$result = $csvHelper->convertDateMDYtoYMD($csvArray); |
37
|
|
|
|
38
|
|
|
$expectedResult = [ |
39
|
|
|
"\"1988/11/11\",\"account_1\",\"1\",\"group 1\",\"1\",\"0\",\"EUR\",\"0.00\",\"Italy\",\"\",\"Bolzano\",\"\",\"Bolzano\"\r\n", |
40
|
|
|
"\"2007/04/22\",\"account_1\",\"2\",\"group 2\",\"4\",\"0\",\"EUR\",\"0.00\",\"Italy\",\"\",\"Milan\",\"\",\"Milan\"\r\n", |
41
|
|
|
]; |
42
|
|
|
$this->assertEquals($expectedResult, $result); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testRemoveAllHeaders() |
46
|
|
|
{ |
47
|
|
|
$csvArray = $this->getCsvArray(); |
48
|
|
|
$csvHelper = new Csv(); |
49
|
|
|
$result = $csvHelper->removeHeaders($csvArray); |
50
|
|
|
|
51
|
|
|
$expectedResult = [ |
52
|
|
|
'"11/11/1988","account_1","1","group 1","1","0","EUR","0.00","Italy","","Bolzano","","Bolzano"', |
53
|
|
|
'"4/22/2007","account_1","2","group 2","4","0","EUR","0.00","Italy","","Milan","","Milan"', |
54
|
|
|
'', |
55
|
|
|
'"©2016 Microsoft Corporation. All rights reserved. "', |
56
|
|
|
]; |
57
|
|
|
$this->assertEquals($expectedResult, $result); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testRemoveFileHeaders() |
61
|
|
|
{ |
62
|
|
|
$csvArray = $this->getCsvArray(); |
63
|
|
|
$csvHelper = new Csv(); |
64
|
|
|
$result = $csvHelper->removeHeaders($csvArray, false); |
65
|
|
|
|
66
|
|
|
$expectedResult = [ |
67
|
|
|
'"GregorianDate","AccountName","AdGroupId","AdGroupName","Impressions","Clicks","CurrencyCode","Spend","CountryOrRegion","City","State","MetroArea","MostSpecificLocation"', |
68
|
|
|
'"11/11/1988","account_1","1","group 1","1","0","EUR","0.00","Italy","","Bolzano","","Bolzano"', |
69
|
|
|
'"4/22/2007","account_1","2","group 2","4","0","EUR","0.00","Italy","","Milan","","Milan"', |
70
|
|
|
'', |
71
|
|
|
'"©2016 Microsoft Corporation. All rights reserved. "', |
72
|
|
|
]; |
73
|
|
|
$this->assertEquals($expectedResult, $result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testRemoveAllHeadersInCount() |
77
|
|
|
{ |
78
|
|
|
$csvArray = $this->getCsvArray(); |
79
|
|
|
$csvHelper = new Csv(); |
80
|
|
|
$result = $csvHelper->removeHeaders($csvArray); |
81
|
|
|
|
82
|
|
|
$this->assertEquals((count($csvArray) - 11), count($result)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testRemoveFileHeadersAsCount() |
86
|
|
|
{ |
87
|
|
|
$csvArray = $this->getCsvArray(); |
88
|
|
|
$csvHelper = new Csv(); |
89
|
|
|
$result = $csvHelper->removeHeaders($csvArray, false); |
90
|
|
|
|
91
|
|
|
$this->assertEquals((count($csvArray) - 10), count($result)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testArrayToCsvLine() |
95
|
|
|
{ |
96
|
|
|
$array = [ |
97
|
|
|
'one', |
98
|
|
|
'two', |
99
|
|
|
'three', |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$csvHelper = new Csv(); |
103
|
|
|
$result = $csvHelper->arrayToCsvLine($array); |
104
|
|
|
$this->assertEquals("one,two,three\r\n", $result); |
105
|
|
|
|
106
|
|
|
$result = $csvHelper->arrayToCsvLine($array, '.', '"'); |
107
|
|
|
$this->assertEquals("\"one\".\"two\".\"three\"\r\n", $result); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function getCsvArray() |
111
|
|
|
{ |
112
|
|
|
return [ |
113
|
|
|
'"Report Name: GeoLocationPerformanceReportRequest"', |
114
|
|
|
'"Report Time: 2/28/2016"', |
115
|
|
|
'"Time Zone: (GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna"', |
116
|
|
|
'"Last Completed Available Day: 2/29/2016 9:00:00 AM (GMT)"', |
117
|
|
|
'"Last Completed Available Hour: 2/29/2016 9:00:00 AM (GMT)"', |
118
|
|
|
'"Report Aggregation: Daily"', |
119
|
|
|
'"Report Filter: "', |
120
|
|
|
'"Potential Incomplete Data: false"', |
121
|
|
|
'"Rows: 2"', |
122
|
|
|
'', |
123
|
|
|
'"GregorianDate","AccountName","AdGroupId","AdGroupName","Impressions","Clicks","CurrencyCode","Spend","CountryOrRegion","City","State","MetroArea","MostSpecificLocation"', |
124
|
|
|
'"11/11/1988","account_1","1","group 1","1","0","EUR","0.00","Italy","","Bolzano","","Bolzano"', |
125
|
|
|
'"4/22/2007","account_1","2","group 2","4","0","EUR","0.00","Italy","","Milan","","Milan"', |
126
|
|
|
'', |
127
|
|
|
'"©2016 Microsoft Corporation. All rights reserved. "', |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|