-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from answear/search-with-image2
Add searchByFeature endpoint
- Loading branch information
Showing
12 changed files
with
443 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Answear\WideEyesBundle\Request; | ||
|
||
class SearchByFeatureRequest implements Request | ||
{ | ||
private string $featureId; | ||
private string $label; | ||
private ?string $gender; | ||
private ?string $filters; | ||
|
||
public function __construct(string $featureId, string $label, ?string $gender = null, ?string $filters = null) | ||
{ | ||
$this->featureId = $featureId; | ||
$this->label = $label; | ||
$this->gender = $gender; | ||
$this->filters = $filters; | ||
} | ||
|
||
public function toJson(): string | ||
{ | ||
return json_encode( | ||
array_filter( | ||
[ | ||
'featureId' => $this->featureId, | ||
'label' => $this->label, | ||
'gender' => $this->gender, | ||
'filters' => $this->filters, | ||
] | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Answear\WideEyesBundle\Response; | ||
|
||
use Answear\WideEyesBundle\Exception\MalformedResponse; | ||
use Webmozart\Assert\Assert; | ||
|
||
class SearchByFeatureResponse | ||
{ | ||
private array $uids; | ||
|
||
private function __construct(array $uids) | ||
{ | ||
Assert::allString($uids); | ||
$this->uids = $uids; | ||
} | ||
|
||
public static function fromArray(array $response): SearchByFeatureResponse | ||
{ | ||
try { | ||
$products = $response['products']; | ||
$responseUids = array_map( | ||
static function ($item) { | ||
Assert::keyExists($item, 'uid'); | ||
|
||
return $item['uid']; | ||
}, | ||
$products, | ||
); | ||
|
||
return new self($responseUids); | ||
} catch (\Throwable $e) { | ||
throw new MalformedResponse($e->getMessage(), $response, $e); | ||
} | ||
} | ||
|
||
public function getUids(): array | ||
{ | ||
return $this->uids; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Answear\WideEyesBundle\Tests\Unit\Request; | ||
|
||
use Answear\WideEyesBundle\Request\SearchByFeatureRequest; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SearchByFeatureRequestTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function requestWithoutGenderAndCountryIsCorrect(): void | ||
{ | ||
$request = new SearchByFeatureRequest('featureId', 'label'); | ||
|
||
self::assertSame( | ||
'{"featureId":"featureId","label":"label"}', | ||
$request->toJson() | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function requestWithGenderAndCountryIsCorrect(): void | ||
{ | ||
$request = new SearchByFeatureRequest('featureId', 'label', 'female', 'pl.inStock == true'); | ||
|
||
self::assertSame( | ||
'{"featureId":"featureId","label":"label","gender":"female","filters":"pl.inStock == true"}', | ||
$request->toJson() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Answear\WideEyesBundle\Tests\Unit\Response; | ||
|
||
use Answear\WideEyesBundle\Exception\MalformedResponse; | ||
use Answear\WideEyesBundle\Response\SearchByFeatureResponse; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SearchByFeatureResponseTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function correctlyReturnsUids(): void | ||
{ | ||
$responseData = [ | ||
'products' => [ | ||
[ | ||
'uid' => 'uid1', | ||
], | ||
[ | ||
'uid' => 'uid2', | ||
], | ||
[ | ||
'uid' => 'uid3', | ||
], | ||
], | ||
]; | ||
|
||
$response = SearchByFeatureResponse::fromArray($responseData); | ||
|
||
self::assertEquals( | ||
[ | ||
'uid1', | ||
'uid2', | ||
'uid3', | ||
], | ||
$response->getUids() | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function malformedResponseWithoutUidInOneItem(): void | ||
{ | ||
$responseData = [ | ||
'products' => [ | ||
[ | ||
'uid' => 'uid1', | ||
], | ||
[ | ||
'id' => 'uid2', | ||
], | ||
[ | ||
'uid' => 'uid3', | ||
], | ||
], | ||
]; | ||
|
||
$this->expectException(MalformedResponse::class); | ||
SearchByFeatureResponse::fromArray($responseData); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function malformedResponseWithoutUid(): void | ||
{ | ||
$responseData = [ | ||
'products' => [ | ||
[ | ||
'result' => 'uid1', | ||
], | ||
[ | ||
'result' => 'uid2', | ||
], | ||
], | ||
]; | ||
|
||
$this->expectException(MalformedResponse::class); | ||
SearchByFeatureResponse::fromArray($responseData); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function malformedResponseWithoutProducts(): void | ||
{ | ||
$responseData = [ | ||
[ | ||
'uid' => 'uid1', | ||
], | ||
[ | ||
'uid' => 'uid2', | ||
], | ||
[ | ||
'uid' => 'uid3', | ||
], | ||
]; | ||
|
||
$this->expectException(MalformedResponse::class); | ||
SearchByFeatureResponse::fromArray($responseData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.