-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserTest.php
145 lines (125 loc) · 2.92 KB
/
ParserTest.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
<?php
namespace php_shell;
$GLOBALS['config'] = require_once 'config.php';
require_once 'Parser.php';
require_once 'helpers.php';
use PHPUnit\Framework\TestCase;
class ParserTest extends TestCase
{
/**
* Lines to add to the text file.
*
* @var int
*/
protected $lines;
/**
* File for testing.
*
* @var string
*/
protected $testCSVFileName;
/**
* Test output combination file.
*
* @var string
*/
protected $outputUniqueCombinationFileName;
/**
* Required fields if not found within file should throw an exception.
*
* This parameter is used to select for which columns
* to skip the value for testing required fields.
* @var null
*/
private $skipColumn = null;
public function __construct()
{
parent::__construct();
$this->lines = 100000;
$this->testCSVFileName = __DIR__ . '/' . time() . '-test-csv-file.csv';
$this->outputUniqueCombinationFileName = __DIR__ . '/' . time() . '-combination-count.csv';
}
/**
* File header names.
*
* @return string[]
*/
public function filHeadings()
{
return [
'brand_name',
'model_name',
'colour_name',
'gb_spec_name',
'network_name',
'grade_name',
'condition_name'
];
}
/**
* Here random numbers are created to get different strings with different values,
* and to get duplicate strings.
*
* @return array
*/
public function generateRandomValuesForFields()
{
$csvLine = [];
$randomNumber = rand(10, 30);
for ($i = 0; $i < count($this->filHeadings()); $i++) {
$csvLine[] = (!is_null($this->skipColumn) && $i === $this->skipColumn) ? '' : 'Random values for csv columns ' . $randomNumber;
}
return $csvLine;
}
/**
* Creating a text file that needs to be parsed.
*/
public function createTestFile()
{
$csv = fopen($this->testCSVFileName, 'w');
fputcsv($csv, $this->filHeadings());
for ($i = 0; $i < $this->lines; $i++) {
fputcsv($csv, $this->generateRandomValuesForFields());
}
fclose($csv);
}
/**
* Options from command line.
*
* @return array
*/
public function getCommandOptions()
{
return [
'file' => $this->testCSVFileName,
'unique-combinations' => $this->outputUniqueCombinationFileName
];
}
/**
* Testing required fields
*/
// public function testRequiredProperties()
// {
// // Given
// $this->skipColumn = 1;
// $this->createTestFile();
//
// // When
// (new Parser($this->getCommandOptions()))->parse();
//
// // Then
// $this->assertFileExists($this->outputUniqueCombinationFileName);
// }
/**
* Parsing file
*/
public function testParseFile()
{
// Given
$this->createTestFile();
// When
(new Parser($this->getCommandOptions()))->parse();
// Then
$this->assertFileExists($this->outputUniqueCombinationFileName);
}
}