|
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
|
|
|
* Test generation |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function runGeneratorTest() |
|
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
|
|
|
$this->validateFileWithDtd(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Validate yml file using dtd |
|
99
|
|
|
*/ |
|
100
|
|
|
private function validateFileWithDtd() |
|
101
|
|
|
{ |
|
102
|
|
|
$systemId = 'data://text/plain;base64,'.\base64_encode(\file_get_contents(__DIR__.'/dtd/'.$this->offerType.'.dtd')); |
|
103
|
|
|
$root = 'yml_catalog'; |
|
104
|
|
|
|
|
105
|
|
|
$ymlFile = new \DOMDocument(); |
|
106
|
|
|
$ymlFile->loadXML(\file_get_contents($this->settings->getOutputFile())); |
|
107
|
|
|
|
|
108
|
|
|
$creator = new \DOMImplementation(); |
|
109
|
|
|
$ymlFileWithDtd = $creator->createDocument(null, null, $creator->createDocumentType($root, null, $systemId)); |
|
110
|
|
|
$ymlFileWithDtd->encoding = 'windows-1251'; |
|
111
|
|
|
|
|
112
|
|
|
$oldNode = $ymlFile->getElementsByTagName($root)->item(0); |
|
113
|
|
|
$newNode = $ymlFileWithDtd->importNode($oldNode, true); |
|
114
|
|
|
$ymlFileWithDtd->appendChild($newNode); |
|
115
|
|
|
|
|
116
|
|
|
try { |
|
117
|
|
|
static::assertTrue($ymlFileWithDtd->validate()); |
|
118
|
|
|
} catch (\Exception $exception) { |
|
119
|
|
|
echo $exception->getMessage(); |
|
120
|
|
|
static::fail('YML file not valid'); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return Settings |
|
126
|
|
|
*/ |
|
127
|
|
|
private function createSettings() |
|
128
|
|
|
{ |
|
129
|
|
|
return (new Settings()) |
|
130
|
|
|
->setOutputFile(\tempnam(\sys_get_temp_dir(), 'YMLGeneratorTest')) |
|
131
|
|
|
->setEncoding('utf-8') |
|
132
|
|
|
->setIndentString("\t") |
|
133
|
|
|
; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return ShopInfo |
|
138
|
|
|
*/ |
|
139
|
|
|
private function createShopInfo() |
|
140
|
|
|
{ |
|
141
|
|
|
return (new ShopInfo()) |
|
142
|
|
|
->setName($this->faker->name) |
|
143
|
|
|
->setCompany($this->faker->company) |
|
144
|
|
|
->setUrl($this->faker->url) |
|
145
|
|
|
->setPlatform($this->faker->name) |
|
146
|
|
|
->setVersion($this->faker->numberBetween(1, 999)) |
|
147
|
|
|
->setAgency($this->faker->name) |
|
148
|
|
|
->setEmail($this->faker->email) |
|
149
|
|
|
; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
private function createCurrencies() |
|
156
|
|
|
{ |
|
157
|
|
|
$currencies = []; |
|
158
|
|
|
$currencies[] = (new Currency()) |
|
159
|
|
|
->setId('UAH') |
|
160
|
|
|
->setRate(1) |
|
161
|
|
|
; |
|
162
|
|
|
|
|
163
|
|
|
return $currencies; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
private function createCategories() |
|
170
|
|
|
{ |
|
171
|
|
|
$categories = []; |
|
172
|
|
|
$categories[] = (new Category()) |
|
173
|
|
|
->setId(1) |
|
174
|
|
|
->setName($this->faker->name) |
|
175
|
|
|
; |
|
176
|
|
|
|
|
177
|
|
|
$categories[] = (new Category()) |
|
178
|
|
|
->setId(2) |
|
179
|
|
|
->setParentId(1) |
|
180
|
|
|
->setName($this->faker->name) |
|
181
|
|
|
; |
|
182
|
|
|
|
|
183
|
|
|
return $categories; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return array |
|
188
|
|
|
*/ |
|
189
|
|
|
private function createDeliveries() |
|
190
|
|
|
{ |
|
191
|
|
|
$deliveries = []; |
|
192
|
|
|
$deliveries[] = (new Delivery()) |
|
193
|
|
|
->setCost(1) |
|
194
|
|
|
->setDays(2); |
|
195
|
|
|
|
|
196
|
|
|
$deliveries[] = (new Delivery()) |
|
197
|
|
|
->setCost(2) |
|
198
|
|
|
->setDays(1) |
|
199
|
|
|
->setOrderBefore(14); |
|
200
|
|
|
|
|
201
|
|
|
return $deliveries; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return array |
|
206
|
|
|
*/ |
|
207
|
|
|
private function createOffers() |
|
208
|
|
|
{ |
|
209
|
|
|
$offers = []; |
|
210
|
|
|
foreach (\range(1, 2) as $id) { |
|
211
|
|
|
$offers[] = $this |
|
212
|
|
|
->createOffer() |
|
213
|
|
|
->setId($id) |
|
214
|
|
|
->setAvailable($this->faker->boolean) |
|
215
|
|
|
->setUrl($this->faker->url) |
|
216
|
|
|
->setPrice($this->faker->numberBetween(1, 9999)) |
|
217
|
|
|
->setCurrencyId('UAH') |
|
218
|
|
|
->setCategoryId($id) |
|
219
|
|
|
->setDelivery($this->faker->boolean) |
|
220
|
|
|
->setLocalDeliveryCost($this->faker->numberBetween(1, 9999)) |
|
221
|
|
|
->setDescription($this->faker->sentence) |
|
222
|
|
|
->setSalesNotes($this->faker->text(45)) |
|
223
|
|
|
->setManufacturerWarranty($this->faker->boolean) |
|
224
|
|
|
->setCountryOfOrigin('Украина') |
|
225
|
|
|
->setDownloadable($this->faker->boolean) |
|
226
|
|
|
->setAdult($this->faker->boolean) |
|
227
|
|
|
; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $offers; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|