1
|
|
|
<?php |
2
|
|
|
use Wambo\Catalog\Mapper\ContentMapper; |
3
|
|
|
use Wambo\Catalog\Mapper\ProductMapper; |
4
|
|
|
use Wambo\Catalog\Model\Content; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class ProductMapperTest tests the Wambo\Catalog\Mapper\ProductMapper class. |
8
|
|
|
*/ |
9
|
|
|
class ProductMapperTest extends PHPUnit_Framework_TestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* If all required fields are given and the validation passes a product should be returned |
13
|
|
|
* |
14
|
|
|
* @test |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public function getProduct_AllRequiredFieldsPresent_ValidationPasses_ProductIsReturned() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
// arrange |
19
|
|
|
$contentMapperMock = $this->createMock(ContentMapper::class); |
20
|
|
|
$contentMapperMock->method("getContent")->willReturn(new Content("Summary", "...")); |
21
|
|
|
|
22
|
|
|
/** @var ContentMapper $contentMapperMock */ |
23
|
|
|
$productMapper = new ProductMapper($contentMapperMock); |
24
|
|
|
|
25
|
|
|
$productData = array( |
26
|
|
|
"sku" => "a-product", |
27
|
|
|
"slug" => "A-Product", |
28
|
|
|
"title" => "Super fancy product", |
29
|
|
|
"summary" => "A super fancy product", |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
// act |
33
|
|
|
$product = $productMapper->getProduct($productData); |
34
|
|
|
|
35
|
|
|
// assert |
36
|
|
|
$this->assertNotNull($product, "getProduct() should have returned a product"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* If given product data is missing one of the mandatory fields and exception should be thrown. |
41
|
|
|
* |
42
|
|
|
* @param array $productData Unstructured product data |
43
|
|
|
* |
44
|
|
|
* @test |
45
|
|
|
* |
46
|
|
|
* @expectedException \Wambo\Catalog\Error\ProductException |
47
|
|
|
* @expectedExceptionMessageRegExp /The field '\w+' is missing in the given product data/ |
48
|
|
|
* @dataProvider getProductDataWithMissingAttribute |
49
|
|
|
*/ |
50
|
|
|
public function getProduct_RequiredFieldMissing_ProductExceptionIsThrown(array $productData) |
51
|
|
|
{ |
52
|
|
|
// arrange |
53
|
|
|
$contentMapperMock = $this->createMock(ContentMapper::class); |
54
|
|
|
|
55
|
|
|
/** @var ContentMapper $contentMapperMock */ |
56
|
|
|
$productMapper = new ProductMapper($contentMapperMock); |
57
|
|
|
|
58
|
|
|
// act |
59
|
|
|
$productMapper->getProduct($productData); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* If the SKU validation fails and exception should be thrown |
64
|
|
|
* |
65
|
|
|
* @test |
66
|
|
|
* @expectedException Wambo\Catalog\Error\ProductException |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function getProduct_AllRequiredFieldsPresent_SkuValidationFails_ProductIsReturned() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
// arrange |
71
|
|
|
$contentMapperMock = $this->createMock(ContentMapper::class); |
72
|
|
|
$contentMapperMock->method("getContent")->willReturn(new Content("Summary", "...")); |
73
|
|
|
|
74
|
|
|
/** @var ContentMapper $contentMapperMock */ |
75
|
|
|
$productMapper = new ProductMapper($contentMapperMock); |
76
|
|
|
|
77
|
|
|
$productData = array( |
78
|
|
|
"sku" => "a", |
79
|
|
|
"slug" => "A-Product", |
80
|
|
|
"title" => "Super fancy product", |
81
|
|
|
"summary" => "A super fancy product", |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
// act |
85
|
|
|
$productMapper->getProduct($productData); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* If the Slug validation fails and exception should be thrown |
90
|
|
|
* |
91
|
|
|
* @test |
92
|
|
|
* @expectedException Wambo\Catalog\Error\ProductException |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function getProduct_AllRequiredFieldsPresent_SlugValidationFails_ProductIsReturned() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
// arrange |
97
|
|
|
$contentMapperMock = $this->createMock(ContentMapper::class); |
98
|
|
|
$contentMapperMock->method("getContent")->willReturn(new Content("A super fancy product", "...")); |
99
|
|
|
|
100
|
|
|
/** @var ContentMapper $contentMapperMock */ |
101
|
|
|
$productMapper = new ProductMapper($contentMapperMock); |
102
|
|
|
|
103
|
|
|
$productData = array( |
104
|
|
|
"sku" => "a-product", |
105
|
|
|
"slug" => "A/Product", |
106
|
|
|
"title" => "Super fancy product", |
107
|
|
|
"summary" => "A super fancy product", |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
// act |
111
|
|
|
$productMapper->getProduct($productData); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get product data with one or more missing attributes. |
116
|
|
|
*/ |
117
|
|
|
public static function getProductDataWithMissingAttribute() |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
|
121
|
|
|
// empty |
122
|
|
|
[ |
123
|
|
|
[] |
124
|
|
|
], |
125
|
|
|
|
126
|
|
|
// wrong casing |
127
|
|
|
[ |
128
|
|
|
[ |
129
|
|
|
"SKU" => "a-product", |
130
|
|
|
"SLUG" => "A-Product", |
131
|
|
|
"TITLE" => "Super fancy product", |
132
|
|
|
] |
133
|
|
|
], |
134
|
|
|
|
135
|
|
|
// title missing |
136
|
|
|
[ |
137
|
|
|
[ |
138
|
|
|
"sku" => "a-product", |
139
|
|
|
"slug" => "A-Product", |
140
|
|
|
] |
141
|
|
|
], |
142
|
|
|
|
143
|
|
|
// slug missing |
144
|
|
|
[ |
145
|
|
|
[ |
146
|
|
|
"sku" => "a-product", |
147
|
|
|
"title" => "Super fancy product", |
148
|
|
|
] |
149
|
|
|
], |
150
|
|
|
|
151
|
|
|
// sku missing |
152
|
|
|
[ |
153
|
|
|
[ |
154
|
|
|
"slug" => "A-Product", |
155
|
|
|
"title" => "Super fancy product", |
156
|
|
|
] |
157
|
|
|
] |
158
|
|
|
|
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.