Skip to content

Commit ea2c2ef

Browse files
Merge branch 'develop' into SW-797_add_variants_logic
2 parents 470f9bd + bcf89e7 commit ea2c2ef

File tree

7 files changed

+143
-15
lines changed

7 files changed

+143
-15
lines changed

examples/simple_search.php

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ function fetchProducts()
4545
->setUserIp('127.0.0.1')
4646
->setReferer('http://google.com/')
4747
->setRevision('1.0.0')
48+
->setShopType('my-shop-system')
49+
->setShopVersion('2.1.3')
4850
->setOutputAdapter('JSON_1.0');
4951

5052
/** @var Json10Response $response */

src/Definitions/QueryParameter.php

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class QueryParameter extends Definition
1010
USER_IP = 'userip',
1111
REFERER = 'referer',
1212
REVISION = 'revision',
13+
SHOP_TYPE = 'shopType',
14+
SHOP_VERSION = 'shopVersion',
1315
QUERY = 'query',
1416
ATTRIB = 'attrib',
1517
ORDER = 'order',

src/Requests/SearchNavigation/SearchNavigationRequest.php

+38
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,44 @@ public function setRevision($value)
8888
return $this;
8989
}
9090

91+
/**
92+
* Sets the shop type param. It is used to identify the shop system of the plugin.
93+
*
94+
* @param string $value
95+
* @return $this
96+
*/
97+
public function setShopType($value)
98+
{
99+
$validator = new ParameterValidator([QueryParameter::SHOP_TYPE => $value]);
100+
$validator->rule('string', QueryParameter::SHOP_TYPE);
101+
102+
if (!$validator->validate()) {
103+
throw new InvalidParamException(QueryParameter::SHOP_TYPE);
104+
}
105+
106+
$this->addParam(QueryParameter::SHOP_TYPE, $value);
107+
return $this;
108+
}
109+
110+
/**
111+
* Sets the shop version param. It is used to identify the version of the shop system.
112+
*
113+
* @param string $value
114+
* @return $this
115+
*/
116+
public function setShopVersion($value)
117+
{
118+
$validator = new ParameterValidator([QueryParameter::SHOP_VERSION => $value]);
119+
$validator->rule('version', QueryParameter::SHOP_VERSION);
120+
121+
if (!$validator->validate()) {
122+
throw new InvalidParamException(QueryParameter::SHOP_VERSION);
123+
}
124+
125+
$this->addParam(QueryParameter::SHOP_VERSION, $value);
126+
return $this;
127+
}
128+
91129
/**
92130
* Adds the attrib param. It is used to filter the search results.
93131
*

src/Validators/ParameterValidator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class ParameterValidator extends Validator
1212
/**
1313
* Semantic versioning regex.
1414
* @see https://semver.org/
15-
* @see https://regex101.com/r/Ly7O1x/3/
15+
* @see https://regex101.com/r/Ly7O1x/1900
1616
*/
17-
const SEMVER_VERSION_REGEX = '/^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/';
17+
const SEMVER_VERSION_REGEX = '/^(?P<additional>0|[1-9]\d*)?\.?(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/';
1818

1919
public function __construct(array $data = [], array $fields = [], $lang = null, $langDir = null)
2020
{

tests/Definitions/QueryParameterTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public function testAllQueryParametersAreAvailable()
2828
'FORCE_ORIGINAL_QUERY' => 'forceOriginalQuery',
2929
'OUTPUT_ATTRIB' => 'outputAttrib',
3030
'SELECTED' => 'selected',
31-
'OUTPUT_ADAPTER' => 'outputAdapter'
31+
'OUTPUT_ADAPTER' => 'outputAdapter',
32+
'SHOP_TYPE' => 'shopType',
33+
'SHOP_VERSION' => 'shopVersion',
3234
];
3335
$availableQueryParameters = QueryParameter::getConstants();
3436

tests/Requests/SearchNavigation/SearchNavigationRequestDataProvider.php

+30-10
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,44 @@ public function invalidRefererProvider()
159159
];
160160
}
161161

162-
public function revisionProvider()
162+
public function versionProvider()
163163
{
164164
return [
165-
'normal revision' => ['1.0.0'],
166-
'other revision' => ['5.1.3'],
167-
'more different revision' => ['55.3.11'],
165+
'normal version' => ['1.0.0'],
166+
'other version' => ['5.1.3'],
167+
'more different version' => ['55.3.11'],
168+
'longer version' => ['1.2.3.4'],
168169
'release candidate' => ['1.4.3-rc.1'],
170+
'longer release candidate' => ['1.1.4.3-rc.1'],
169171
];
170172
}
171173

172-
public function invalidRevisionProvider()
174+
public function invalidVersionProvider()
173175
{
174176
return [
175-
'revision is not a revision' => ['invalidRevision'],
176-
'revision is an integer' => [5],
177-
'revision is an array' => [['1.0.0']],
178-
'revision is an object' => [new \stdClass()],
179-
'revision with a dot after the number' => ['1.'],
177+
'version is not a version' => ['invalidRevision'],
178+
'version is an integer' => [5],
179+
'version is an array' => [['1.0.0']],
180+
'version is an object' => [new \stdClass()],
181+
'version with a dot after the number' => ['1.'],
182+
];
183+
}
184+
185+
public function shopTypeProvider()
186+
{
187+
return [
188+
'normal type' => ['plenty'],
189+
'other type' => ['plentyMarkets'],
190+
'more different type' => ['Shopware 6']
191+
];
192+
}
193+
194+
public function invalidShopTypeProvider()
195+
{
196+
return [
197+
'type is an integer' => [6],
198+
'type is an array' => [['bestType']],
199+
'type is an object' => [new \stdClass()],
180200
];
181201
}
182202

tests/Requests/SearchNavigation/SearchNavigationRequestTest.php

+66-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public function testInvalidRefererWillThrowAnException($invalidReferer)
305305
}
306306

307307
/**
308-
* @dataProvider revisionProvider
308+
* @dataProvider versionProvider
309309
* @param string $expectedRevision
310310
*/
311311
public function testRevisionCanBeSetAndIsFormattedAsQueryString($expectedRevision)
@@ -322,7 +322,7 @@ public function testRevisionCanBeSetAndIsFormattedAsQueryString($expectedRevisio
322322
}
323323

324324
/**
325-
* @dataProvider invalidRevisionProvider
325+
* @dataProvider invalidVersionProvider
326326
* @param mixed $invalidRevision
327327
*/
328328
public function testInvalidRevisionWillThrowAnException($invalidRevision)
@@ -336,6 +336,70 @@ public function testInvalidRevisionWillThrowAnException($invalidRevision)
336336
$searchRequest->setRevision($invalidRevision);
337337
}
338338

339+
/**
340+
* @dataProvider versionProvider
341+
* @param string $expectedVersion
342+
*/
343+
public function testShopVersionCanBeSetAndIsFormattedAsQueryString($expectedVersion)
344+
{
345+
$expectedParameter = 'shopVersion';
346+
347+
$searchRequest = new SearchRequest();
348+
$this->setRequiredParamsForSearchNavigationRequest($searchRequest);
349+
350+
$searchRequest->setShopVersion($expectedVersion);
351+
$params = $searchRequest->getParams();
352+
$this->assertArrayHasKey($expectedParameter, $params);
353+
$this->assertEquals($expectedVersion, $params[$expectedParameter]);
354+
}
355+
356+
/**
357+
* @dataProvider invalidVersionProvider
358+
* @param mixed $invalidVersion
359+
*/
360+
public function testInvalidShopVersionWillThrowAnException($invalidVersion)
361+
{
362+
$this->expectException(InvalidParamException::class);
363+
$this->expectExceptionMessage('Parameter shopVersion is not valid.');
364+
365+
$searchRequest = new SearchRequest();
366+
$this->setRequiredParamsForSearchNavigationRequest($searchRequest);
367+
368+
$searchRequest->setShopVersion($invalidVersion);
369+
}
370+
371+
/**
372+
* @dataProvider shopTypeProvider
373+
* @param string $expectedShopType
374+
*/
375+
public function testShopTypeWillBeSetItInAValidFormat($expectedShopType)
376+
{
377+
$expectedParameter = 'shopType';
378+
379+
$searchRequest = new SearchRequest();
380+
$this->setRequiredParamsForSearchNavigationRequest($searchRequest);
381+
382+
$searchRequest->setShopType($expectedShopType);
383+
$params = $searchRequest->getParams();
384+
$this->assertArrayHasKey($expectedParameter, $params);
385+
$this->assertEquals($expectedShopType, $params[$expectedParameter]);
386+
}
387+
388+
/**
389+
* @dataProvider invalidShopTypeProvider
390+
* @param mixed $invalidShopType
391+
*/
392+
public function testInvalidShopTypeWillThrowAnException($invalidShopType)
393+
{
394+
$this->expectException(InvalidParamException::class);
395+
$this->expectExceptionMessage('Parameter shopType is not valid.');
396+
397+
$searchRequest = new SearchRequest();
398+
$this->setRequiredParamsForSearchNavigationRequest($searchRequest);
399+
400+
$searchRequest->setShopType($invalidShopType);
401+
}
402+
339403
/**
340404
* @dataProvider attributeProvider
341405
* @param string $expectedAttributeName

0 commit comments

Comments
 (0)