1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Bukashk0zzzYmlGenerator |
5
|
|
|
* |
6
|
|
|
* (c) Denis Golubovskiy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Bukashk0zzz\YmlGenerator\Tests; |
13
|
|
|
|
14
|
|
|
use Bukashk0zzz\YmlGenerator\Generator; |
15
|
|
|
use Bukashk0zzz\YmlGenerator\Model\Category; |
16
|
|
|
use Bukashk0zzz\YmlGenerator\Model\Currency; |
17
|
|
|
use Bukashk0zzz\YmlGenerator\Model\Delivery; |
18
|
|
|
use Bukashk0zzz\YmlGenerator\Model\ShopInfo; |
19
|
|
|
use Bukashk0zzz\YmlGenerator\Settings; |
20
|
|
|
use Faker\Factory as Faker; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Abstract Generator test |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractGeneratorTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \Faker\Generator |
29
|
|
|
*/ |
30
|
|
|
protected $faker; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Settings |
34
|
|
|
*/ |
35
|
|
|
protected $settings; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ShopInfo |
39
|
|
|
*/ |
40
|
|
|
protected $shopInfo; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $currencies; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $categories; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $deliveries; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $offerType; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test setup |
64
|
|
|
*/ |
65
|
|
|
public function setUp() |
66
|
|
|
{ |
67
|
|
|
$this->faker = Faker::create(); |
68
|
|
|
|
69
|
|
|
$this->settings = $this->createSettings(); |
70
|
|
|
$this->shopInfo = $this->createShopInfo(); |
71
|
|
|
$this->currencies = $this->createCurrencies(); |
72
|
|
|
$this->categories = $this->createCategories(); |
73
|
|
|
$this->deliveries = $this->createDeliveries(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return \Bukashk0zzz\YmlGenerator\Model\Offer\AbstractOffer |
78
|
|
|
*/ |
79
|
|
|
abstract protected function createOffer(); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Produces an XML file and writes to $this->settings->getOutputFile() |
83
|
|
|
*/ |
84
|
|
|
protected function generateFile() |
85
|
|
|
{ |
86
|
|
|
static::assertTrue((new Generator($this->settings))->generate( |
87
|
|
|
$this->shopInfo, |
88
|
|
|
$this->currencies, |
89
|
|
|
$this->categories, |
90
|
|
|
$this->createOffers(), |
91
|
|
|
$this->deliveries |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Test generation |
97
|
|
|
*/ |
98
|
|
|
protected function runGeneratorTest() |
99
|
|
|
{ |
100
|
|
|
$this->generateFile(); |
101
|
|
|
$this->validateFileWithDtd(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
protected function createOffers() |
108
|
|
|
{ |
109
|
|
|
$offers = []; |
110
|
|
|
foreach (\range(1, 2) as $id) { |
111
|
|
|
$offers[] = $this |
112
|
|
|
->createOffer() |
113
|
|
|
->setId($id) |
114
|
|
|
->setAvailable($this->faker->boolean) |
115
|
|
|
->setUrl($this->faker->url) |
116
|
|
|
->setPrice($this->faker->numberBetween(1, 9999)) |
117
|
|
|
->setOldPrice($this->faker->numberBetween(1, 9999)) |
118
|
|
|
->setWeight($this->faker->numberBetween(1, 9999)) |
119
|
|
|
->setCurrencyId('UAH') |
120
|
|
|
->setCategoryId($id) |
121
|
|
|
->setDelivery($this->faker->boolean) |
122
|
|
|
->setLocalDeliveryCost($this->faker->numberBetween(1, 9999)) |
123
|
|
|
->setDescription($this->faker->sentence) |
124
|
|
|
->setSalesNotes($this->faker->text(45)) |
125
|
|
|
->setManufacturerWarranty($this->faker->boolean) |
126
|
|
|
->setCountryOfOrigin('Украина') |
127
|
|
|
->setDownloadable($this->faker->boolean) |
128
|
|
|
->setAdult($this->faker->boolean) |
129
|
|
|
->setMarketCategory($this->faker->word) |
130
|
|
|
->setCpa($this->faker->numberBetween(0, 1)) |
131
|
|
|
->setBarcodes([$this->faker->ean13, $this->faker->ean13]) |
132
|
|
|
; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $offers; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Validate yml file using dtd |
140
|
|
|
*/ |
141
|
|
|
private function validateFileWithDtd() |
142
|
|
|
{ |
143
|
|
|
$systemId = 'data://text/plain;base64,'.\base64_encode(\file_get_contents(__DIR__.'/dtd/'.$this->offerType.'.dtd')); |
144
|
|
|
$root = 'yml_catalog'; |
145
|
|
|
|
146
|
|
|
$ymlFile = new \DOMDocument(); |
147
|
|
|
$ymlFile->loadXML(\file_get_contents($this->settings->getOutputFile())); |
148
|
|
|
|
149
|
|
|
$creator = new \DOMImplementation(); |
150
|
|
|
$ymlFileWithDtd = $creator->createDocument(null, null, $creator->createDocumentType($root, null, $systemId)); |
151
|
|
|
$ymlFileWithDtd->encoding = 'windows-1251'; |
152
|
|
|
|
153
|
|
|
$oldNode = $ymlFile->getElementsByTagName($root)->item(0); |
154
|
|
|
$newNode = $ymlFileWithDtd->importNode($oldNode, true); |
155
|
|
|
$ymlFileWithDtd->appendChild($newNode); |
156
|
|
|
|
157
|
|
|
try { |
158
|
|
|
static::assertTrue($ymlFileWithDtd->validate()); |
159
|
|
|
} catch (\Exception $exception) { |
160
|
|
|
echo $exception->getMessage(); |
161
|
|
|
static::fail('YML file not valid'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return Settings |
167
|
|
|
*/ |
168
|
|
|
private function createSettings() |
169
|
|
|
{ |
170
|
|
|
return (new Settings()) |
171
|
|
|
->setOutputFile(\tempnam(\sys_get_temp_dir(), 'YMLGeneratorTest')) |
172
|
|
|
->setEncoding('utf-8') |
173
|
|
|
->setIndentString("\t") |
174
|
|
|
; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return ShopInfo |
179
|
|
|
*/ |
180
|
|
|
private function createShopInfo() |
181
|
|
|
{ |
182
|
|
|
return (new ShopInfo()) |
183
|
|
|
->setName($this->faker->name) |
184
|
|
|
->setCompany($this->faker->company) |
185
|
|
|
->setUrl($this->faker->url) |
186
|
|
|
->setPlatform($this->faker->name) |
187
|
|
|
->setVersion($this->faker->numberBetween(1, 999)) |
188
|
|
|
->setAgency($this->faker->name) |
189
|
|
|
->setEmail($this->faker->email) |
190
|
|
|
; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
private function createCurrencies() |
197
|
|
|
{ |
198
|
|
|
$currencies = []; |
199
|
|
|
$currencies[] = (new Currency()) |
200
|
|
|
->setId('UAH') |
201
|
|
|
->setRate(1) |
202
|
|
|
; |
203
|
|
|
|
204
|
|
|
return $currencies; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
private function createCategories() |
211
|
|
|
{ |
212
|
|
|
$categories = []; |
213
|
|
|
$categories[] = (new Category()) |
214
|
|
|
->setId(1) |
215
|
|
|
->setName($this->faker->name) |
216
|
|
|
; |
217
|
|
|
|
218
|
|
|
$categories[] = (new Category()) |
219
|
|
|
->setId(2) |
220
|
|
|
->setParentId(1) |
221
|
|
|
->setName($this->faker->name) |
222
|
|
|
; |
223
|
|
|
|
224
|
|
|
return $categories; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
|
|
private function createDeliveries() |
231
|
|
|
{ |
232
|
|
|
$deliveries = []; |
233
|
|
|
$deliveries[] = (new Delivery()) |
234
|
|
|
->setCost(1) |
235
|
|
|
->setDays(2); |
236
|
|
|
|
237
|
|
|
$deliveries[] = (new Delivery()) |
238
|
|
|
->setCost(2) |
239
|
|
|
->setDays(1) |
240
|
|
|
->setOrderBefore(14); |
241
|
|
|
|
242
|
|
|
return $deliveries; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|