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