|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use League\Flysystem\Filesystem; |
|
4
|
|
|
use League\Flysystem\Memory\MemoryAdapter; |
|
5
|
|
|
use Wambo\Catalog\Error\CatalogException; |
|
6
|
|
|
use Wambo\Catalog\JSONCatalogProvider; |
|
7
|
|
|
use Wambo\Catalog\Mapper\CatalogMapper; |
|
8
|
|
|
use Wambo\Catalog\Mapper\ContentMapper; |
|
9
|
|
|
use Wambo\Catalog\Mapper\ProductMapper; |
|
10
|
|
|
use Wambo\Catalog\Model\Catalog; |
|
11
|
|
|
use Wambo\Catalog\Model\Product; |
|
12
|
|
|
use Wambo\Catalog\Validation\SKUValidator; |
|
13
|
|
|
use Wambo\Catalog\Validation\SlugValidator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class JSONCatalogTest tests the JSONCatalog class. |
|
17
|
|
|
* @package Wambo\Catalog\Tests |
|
18
|
|
|
*/ |
|
19
|
|
|
class JSONCatalogTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* If the JSONCatalog is empty no products should be returned. |
|
23
|
|
|
* |
|
24
|
|
|
* @test |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getCatalog_JSONIsEmpty_NoProductsAreReturned() |
|
27
|
|
|
{ |
|
28
|
|
|
// arrange |
|
29
|
|
|
$filesystem = new Filesystem(new MemoryAdapter()); |
|
30
|
|
|
$catalogMapperMock = $this->getMockBuilder(CatalogMapper::class)->disableOriginalConstructor()->getMock(); |
|
31
|
|
|
/** @var $catalogMapperMock CatalogMapper A mock for the CatalogMapper class */ |
|
32
|
|
|
$jsonCatalog = new JSONCatalogProvider($filesystem, "catalog.json", $catalogMapperMock); |
|
33
|
|
|
|
|
34
|
|
|
// act |
|
35
|
|
|
$catalog = $jsonCatalog->getCatalog(); |
|
36
|
|
|
|
|
37
|
|
|
// assert |
|
38
|
|
|
$this->assertEmpty($catalog, "getCatalog should not have returned a catalog if the catalog JSON is empty"); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* If the CatalogMapper returns a catalog, the provider should return that catalog. |
|
43
|
|
|
* |
|
44
|
|
|
* @test |
|
45
|
|
|
* @expectedException \Wambo\Catalog\Error\CatalogException |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function getCatalog_JSONIsInvalid_CatalogExceptionIsThrown() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
// arrange |
|
50
|
|
|
$filesystem = new Filesystem(new MemoryAdapter()); |
|
51
|
|
|
$catalogMapperMock = $this->getMockBuilder(CatalogMapper::class)->disableOriginalConstructor()->getMock(); |
|
52
|
|
|
$catalogMapperMock->method("getCatalog")->willReturn(new Catalog(array())); |
|
53
|
|
|
/** @var $catalogMapperMock CatalogMapper A mock for the CatalogMapper class */ |
|
54
|
|
|
|
|
55
|
|
|
$catalogJSON = <<<JSON |
|
56
|
|
|
[ |
|
57
|
|
|
{},,],, |
|
58
|
|
|
] |
|
59
|
|
|
JSON; |
|
60
|
|
|
$filesystem->write("catalog.json", $catalogJSON); |
|
61
|
|
|
$jsonCatalogProvider = new JSONCatalogProvider($filesystem, "catalog.json", $catalogMapperMock); |
|
62
|
|
|
|
|
63
|
|
|
// act |
|
64
|
|
|
$jsonCatalogProvider->getCatalog(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* If the CatalogMapper throws an exception, the provider should throw a CatalogException. |
|
69
|
|
|
* |
|
70
|
|
|
* @test |
|
71
|
|
|
* @expectedException Wambo\Catalog\Error\CatalogException |
|
72
|
|
|
*/ |
|
73
|
|
View Code Duplication |
public function getCatalog_JSONIsValid_MapperThrowsException_CatalogExceptionIsThrown() |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
// arrange |
|
76
|
|
|
$filesystem = new Filesystem(new MemoryAdapter()); |
|
77
|
|
|
$catalogMapperMock = $this->getMockBuilder(CatalogMapper::class)->disableOriginalConstructor()->getMock(); |
|
78
|
|
|
$catalogMapperMock->method("getCatalog")->willThrowException(new Exception("Some mapping error")); |
|
79
|
|
|
/** @var $catalogMapperMock CatalogMapper A mock for the CatalogMapper class */ |
|
80
|
|
|
|
|
81
|
|
|
$catalogJSON = <<<JSON |
|
82
|
|
|
[] |
|
83
|
|
|
JSON; |
|
84
|
|
|
$filesystem->write("catalog.json", $catalogJSON); |
|
85
|
|
|
$jsonCatalogProvider = new JSONCatalogProvider($filesystem, "catalog.json", $catalogMapperMock); |
|
86
|
|
|
|
|
87
|
|
|
// act |
|
88
|
|
|
$jsonCatalogProvider->getCatalog(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* If the CatalogMapper returns a catalog, the provider should return that catalog. |
|
93
|
|
|
* |
|
94
|
|
|
* @test |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public function getCatalog_JSONIsValid_MapperReturnsCatalog_CatalogIsReturned() |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
// arrange |
|
99
|
|
|
$filesystem = new Filesystem(new MemoryAdapter()); |
|
100
|
|
|
$catalogMapperMock = $this->getMockBuilder(CatalogMapper::class)->disableOriginalConstructor()->getMock(); |
|
101
|
|
|
$catalogMapperMock->method("getCatalog")->willReturn(new Catalog(array())); |
|
102
|
|
|
/** @var $catalogMapperMock CatalogMapper A mock for the CatalogMapper class */ |
|
103
|
|
|
|
|
104
|
|
|
$catalogJSON = <<<JSON |
|
105
|
|
|
[ |
|
106
|
|
|
{} |
|
107
|
|
|
] |
|
108
|
|
|
JSON; |
|
109
|
|
|
$filesystem->write("catalog.json", $catalogJSON); |
|
110
|
|
|
$jsonCatalogProvider = new JSONCatalogProvider($filesystem, "catalog.json", $catalogMapperMock); |
|
111
|
|
|
|
|
112
|
|
|
// act |
|
113
|
|
|
$catalog = $jsonCatalogProvider->getCatalog(); |
|
114
|
|
|
|
|
115
|
|
|
// assert |
|
116
|
|
|
$this->assertNotNull($catalog, "getCatalog should return a catalog if the mapper returned one"); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Integration test |
|
121
|
|
|
* @test |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getCatalog_IntegrationTest_CatalogWithProductsIsReturned() |
|
124
|
|
|
{ |
|
125
|
|
|
// arrange |
|
126
|
|
|
$filesystem = new Filesystem(new MemoryAdapter()); |
|
127
|
|
|
$contentMapper = new ContentMapper(); |
|
128
|
|
|
$productMapper = new ProductMapper($contentMapper); |
|
129
|
|
|
$catalogMapper = new CatalogMapper($productMapper); |
|
130
|
|
|
|
|
131
|
|
|
$catalogJSON = <<<JSON |
|
132
|
|
|
[ |
|
133
|
|
|
{ |
|
134
|
|
|
"sku": "t-shirt-no-1", |
|
135
|
|
|
"slug": "t-shirt-no-1", |
|
136
|
|
|
"title": "T-Shirt No. 1", |
|
137
|
|
|
"summary": "Our T-Shirt No. 1", |
|
138
|
|
|
"description": "Our fancy T-Shirt No. 1 is ..." |
|
139
|
|
|
}, |
|
140
|
|
|
{ |
|
141
|
|
|
"sku": "t-shirt-no-2", |
|
142
|
|
|
"slug": "t-shirt-no-2", |
|
143
|
|
|
"title": "T-Shirt No. 2", |
|
144
|
|
|
"summary": "Our T-Shirt No. 2", |
|
145
|
|
|
"description": "Our fancy T-Shirt No. 2 is ..." |
|
146
|
|
|
}, |
|
147
|
|
|
{ |
|
148
|
|
|
"sku": "t-shirt-no-3", |
|
149
|
|
|
"slug": "t-shirt-no-3", |
|
150
|
|
|
"title": "T-Shirt No. 3", |
|
151
|
|
|
"summary": "Our T-Shirt No. 3", |
|
152
|
|
|
"description": "Our fancy T-Shirt No. 3 is ..." |
|
153
|
|
|
} |
|
154
|
|
|
] |
|
155
|
|
|
JSON; |
|
156
|
|
|
$filesystem->write("catalog.json", $catalogJSON); |
|
157
|
|
|
$jsonCatalogProvider = new JSONCatalogProvider($filesystem, "catalog.json", $catalogMapper); |
|
158
|
|
|
|
|
159
|
|
|
// act |
|
160
|
|
|
$catalog = $jsonCatalogProvider->getCatalog(); |
|
161
|
|
|
|
|
162
|
|
|
// assert |
|
163
|
|
|
$this->assertCount(3, $catalog, "getCatalog should return a catalog with 3 products"); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Integration test |
|
168
|
|
|
* @test |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getCatalog_IntegrationTest_AllProductAttributesAreSet() |
|
171
|
|
|
{ |
|
172
|
|
|
// arrange |
|
173
|
|
|
$filesystem = new Filesystem(new MemoryAdapter()); |
|
174
|
|
|
$contentMapper = new ContentMapper(); |
|
175
|
|
|
$productMapper = new ProductMapper($contentMapper); |
|
176
|
|
|
$catalogMapper = new CatalogMapper($productMapper); |
|
177
|
|
|
|
|
178
|
|
|
$catalogJSON = <<<JSON |
|
179
|
|
|
[ |
|
180
|
|
|
{ |
|
181
|
|
|
"sku": "t-shirt-no-1", |
|
182
|
|
|
"slug": "t-shirt-no-1-slug", |
|
183
|
|
|
"title": "T-Shirt No. 1", |
|
184
|
|
|
"summary": "Our T-Shirt No. 1", |
|
185
|
|
|
"description": "Our fancy T-Shirt No. 1 is ..." |
|
186
|
|
|
} |
|
187
|
|
|
] |
|
188
|
|
|
JSON; |
|
189
|
|
|
$filesystem->write("catalog.json", $catalogJSON); |
|
190
|
|
|
$jsonCatalogProvider = new JSONCatalogProvider($filesystem, "catalog.json", $catalogMapper); |
|
191
|
|
|
|
|
192
|
|
|
// act |
|
193
|
|
|
$catalog = $jsonCatalogProvider->getCatalog(); |
|
194
|
|
|
|
|
195
|
|
|
// assert |
|
196
|
|
|
$products = $catalog->getProducts(); |
|
197
|
|
|
$this->assertCount(1, $products, "getCatalog should return a catalog with one product"); |
|
198
|
|
|
|
|
199
|
|
|
/** @var Product $firstProduct */ |
|
200
|
|
|
$firstProduct = $products[0]; |
|
201
|
|
|
$this->assertEquals("t-shirt-no-1", $firstProduct->getSku(), "Wrong SKU"); |
|
202
|
|
|
$this->assertEquals("t-shirt-no-1-slug", $firstProduct->getSlug(), "Wrong slug"); |
|
203
|
|
|
$this->assertEquals("T-Shirt No. 1", $firstProduct->getTitle(), "Wrong title"); |
|
204
|
|
|
$this->assertEquals("Our T-Shirt No. 1", $firstProduct->getSummaryText(), "Wrong summary"); |
|
205
|
|
|
$this->assertEquals("Our fancy T-Shirt No. 1 is ...", $firstProduct->getProductDescription(), "Wrong description"); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.