Skip to content

Commit 5201e10

Browse files
committed
unit tests added
1 parent a5e326d commit 5201e10

38 files changed

Lines changed: 2078 additions & 164 deletions

src/Filter/AbstractFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(Input $input)
2424

2525
protected function get(string $parameter, $default = null)
2626
{
27-
return $this->input->query->get($parameter, $default);
27+
return $this->input->get($parameter, $default);
2828
}
2929

3030
public function hasCustomFilter(string $filterName): bool
@@ -37,8 +37,8 @@ public function setCustomFilter(string $filterName, $value): void
3737
$this->customFilter[$filterName] = $value;
3838
}
3939

40-
public function getCustomFilter(string $filterName)
40+
public function getCustomFilter(string $filterName, $default = null)
4141
{
42-
return $this->customFilter[$filterName];
42+
return $this->customFilter[$filterName] ?? $default;
4343
}
4444
}

src/Filter/ProductListFilter.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ public function getIds(): array
5555
{
5656
$list = (string) $this->get('filter_ids_list');
5757

58-
return explode(",", $list);
58+
return strlen($list) > 0 ? explode(",", $list) : [];
5959
}
6060

61-
6261
public function hasCategory(): bool
6362
{
6463
return '' !== $this->getCategory();
@@ -103,10 +102,10 @@ public function getQuantityFrom(): ?float
103102

104103
public function hasQuantityTo(): bool
105104
{
106-
return null === $this->hasQuantityTo();
105+
return null !== $this->getQuantityTo();
107106
}
108107

109-
public function getQuantityTo(): float
108+
public function getQuantityTo(): ?float
110109
{
111110
return $this->getNullOrFloat('filter_quantity_to');
112111
}
@@ -119,12 +118,12 @@ public function hasSort(): bool
119118
public function getSort(): array
120119
{
121120
$filter = (string) $this->get('filter_sort');
122-
return explode(" ", trim($filter));
121+
return strlen($filter) > 0 ? explode(" ", trim($filter)) : [];
123122
}
124123

125124
private function getNullOrFloat(string $parameter): ?float
126125
{
127126
$filter = $this->get($parameter);
128-
return null === $filter ? null : (float) $filter;
127+
return (null === $filter || '' === $filter ) ? null : (float) $filter;
129128
}
130129
}

src/Handler/Common/FileVersionActionHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* For the full copyright and license information, please view the LICENSE
66
* file that was distributed with this source code.
77
*/
8+
89
declare(strict_types=1);
910

1011
namespace Spinbits\BaselinkerSdk\Handler\Common;

src/Handler/Common/SupportedMethodsActionHandler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
use Spinbits\BaselinkerSdk\RequestHandler;
1515
use Spinbits\BaselinkerSdk\Rest\Input;
1616

17-
class SupportedMethodsAction implements HandlerInterface
17+
class SupportedMethodsActionHandler implements HandlerInterface
1818
{
19-
/** @var RequestHandler */
20-
private $requestHandler;
19+
private RequestHandler $requestHandler;
2120

2221
/**
2322
* @param RequestHandler $requestHandler
@@ -27,6 +26,9 @@ public function __construct(RequestHandler $requestHandler)
2726
$this->requestHandler = $requestHandler;
2827
}
2928

29+
/**
30+
* {@inheritDoc}
31+
*/
3032
public function handle(Input $input): array
3133
{
3234
return $this->requestHandler->supportedActions();

src/Handler/HandlerInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@
1313

1414
interface HandlerInterface
1515
{
16+
/**
17+
* @param Input $input
18+
* @return array
19+
*/
1620
public function handle(Input $input): array;
1721
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @author Marcin Hubert <>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Spinbits\BaselinkerSdk\Handler\Sylius\Mapper;
12+
13+
use Sylius\Component\Product\Model\ProductInterface;
14+
use Sylius\Component\Product\Model\ProductVariantInterface;
15+
use Sylius\Component\Core\Model\ChannelInterface;
16+
use Sylius\Component\Core\Model\ChannelPricingInterface;
17+
18+
class ListProductMapper
19+
{
20+
private ChannelInterface $channel;
21+
22+
public function __construct(ChannelInterface $channel)
23+
{
24+
$this->channel = $channel;
25+
}
26+
27+
public function map(ProductInterface $product): \Generator
28+
{
29+
foreach ($product->getVariants() as $variant) {
30+
yield [
31+
'name' => $product->getName(),
32+
'quantity' => $variant->getOnHand(),
33+
'price' => $this->getPrice($variant),
34+
'ean' => null, // not required
35+
'sku' => $product->getCode(), // not required
36+
];
37+
}
38+
}
39+
40+
private function getPrice(ProductVariantInterface $productVariant): float
41+
{
42+
$pricing = $productVariant->getChannelPricingForChannel($this->channel);
43+
if (!$pricing instanceof ChannelPricingInterface) {
44+
return 0;
45+
}
46+
return $pricing->getPrice() / 100;
47+
}
48+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* @author Marcin Hubert <>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Spinbits\BaselinkerSdk\Handler\Sylius\Mapper;
12+
13+
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
14+
use Sylius\Component\Attribute\Model\AttributeValueInterface;
15+
use Sylius\Component\Core\Model\ChannelInterface;
16+
use Sylius\Component\Core\Model\ChannelPricingInterface;
17+
use Sylius\Component\Core\Model\TaxonInterface;
18+
use Sylius\Component\Core\Model\ProductInterface;
19+
use Sylius\Component\Core\Model\ProducVariantInterface;
20+
use Sylius\Component\Core\Model\ProductImageInterface;
21+
22+
class ProductMapper
23+
{
24+
private ChannelInterface $channel;
25+
private CacheManager $cacheManager;
26+
27+
public function __construct(ChannelInterface $channel, CacheManager $cacheManager)
28+
{
29+
$this->channel = $channel;
30+
$this->cacheManager = $cacheManager;
31+
}
32+
33+
public function map(ProductInterface $product): \Generator
34+
{
35+
yield [
36+
'sku' => $product->getCode(),
37+
'name' => $product->getName(),
38+
'tax' => $this->getTax($product),
39+
'description' => $product->getDescription(),
40+
'categoryId' => $this->getTaxon($product),
41+
'images' => $this->getImages($product),
42+
'variants' => $this->getVariants($product),
43+
'features' => $this->getFeatures($product),
44+
// non standard fields
45+
'allCategories' => $this->getTaxonomies($product),
46+
'allCategoriesExpanded' => $this->getTaxonomiesExpanded($product),
47+
'shortDescription' => $product->getShortDescription(),
48+
'slug' => $product->getSlug(),
49+
'url' => sprintf('https://digigames.pl/pl_PL/products/%s', $product->getSlug()),
50+
];
51+
}
52+
53+
private function getTax(ProductInterface $product): int
54+
{
55+
return 23;
56+
}
57+
58+
private function getTaxon(ProductInterface $product): string
59+
{
60+
foreach ($product->getTaxons() as $taxon) {
61+
if (!$taxon->getParent() instanceof TaxonInterface) {
62+
continue;
63+
}
64+
if ($taxon->getParent()->getcode() === "genre") {
65+
return $taxon->getCode();
66+
}
67+
}
68+
return $product->getMainTaxon()->getCode();
69+
}
70+
71+
private function getImages(ProductInterface $product): array
72+
{
73+
$cache = $this->cacheManager;
74+
return $product->getImages()->map(function (ProductImageInterface $image) use ($cache) {
75+
return $cache->getBrowserPath(parse_url($image->getPath(), PHP_URL_PATH), 'sylius_admin_product_original');
76+
})->toArray();
77+
}
78+
79+
private function getVariants(ProductInterface $product): array
80+
{
81+
$return = [];
82+
foreach ($product->getVariants() as $variant) {
83+
$return[$variant->getId()] = [
84+
'full_name' => $variant->getName(),
85+
'name' => $variant->getName(),
86+
'price' => $this->getPrice($variant),
87+
'quantity' => $variant->getOnHand() - $variant->getOnHold(),
88+
'sku' => $variant->getCode(),
89+
];
90+
}
91+
return $return;
92+
}
93+
94+
private function getFeatures(ProductInterface $product)
95+
{
96+
return $product->getAttributes()->map(function (AttributeValueInterface $attribute) {
97+
return [$attribute->getAttribute()->getName(), $attribute->getValue()];
98+
})->toArray();
99+
}
100+
101+
private function getTaxonomies(ProductInterface $product): array
102+
{
103+
return $product->getTaxons()->map(function (TaxonInterface $taxon) {
104+
return $taxon->getName();
105+
})->toArray();
106+
}
107+
108+
private function getTaxonomiesExpanded(ProductInterface $product): array
109+
{
110+
return $product->getTaxons()->map(function (TaxonInterface $taxon) {
111+
return $taxon->getFullname();
112+
})->toArray();
113+
}
114+
115+
private function getPrice(ProductVariantInterface $productVariant): float
116+
{
117+
$pricing = $productVariant->getChannelPricingForChannel($this->channel);
118+
if (!$pricing instanceof ChannelPricingInterface) {
119+
return 0;
120+
}
121+
return $pricing->getPrice() / 100;
122+
}
123+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @author Marcin Hubert <>
4+
* @author Jakub Lech <info@smartbyte.pl>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Spinbits\BaselinkerSdk\Handler\Sylius;
13+
14+
use Spinbits\BaselinkerSdk\Model\OrderAddModel;
15+
use App\Integration\BaseLinker\Service\OrderCreateService;
16+
use Spinbits\BaselinkerSdk\Handler\HandlerInterface;
17+
use Spinbits\BaselinkerSdk\Rest\Exception\InvalidArgumentException;
18+
use Spinbits\BaselinkerSdk\Rest\Input;
19+
use Symfony\Component\Validator\ConstraintViolation;
20+
use Symfony\Component\Validator\ConstraintViolationListInterface;
21+
use Symfony\Component\Validator\Validator\ValidatorInterface;
22+
23+
class OrderAddAction implements HandlerInterface
24+
{
25+
private ValidatorInterface $validator;
26+
private OrderCreateService $orderCreateService;
27+
28+
public function __construct(ValidatorInterface $validator, OrderCreateService $orderCreateService)
29+
{
30+
$this->validator = $validator;
31+
$this->orderCreateService = $orderCreateService;
32+
}
33+
34+
/**
35+
* @param Input $input
36+
* @return array
37+
* @throws InvalidArgumentException
38+
*/
39+
public function handle(Input $input): array
40+
{
41+
$input = new OrderAddModel($input->body->all());
42+
43+
$result = $this->validator->validate($input);
44+
$this->assertIsValid($result);
45+
46+
$order = $this->orderCreateService->createOrder($input);
47+
48+
return ['order_id' => $order->getId()];
49+
}
50+
51+
/**
52+
* @param ConstraintViolationListInterface|ConstraintViolation[] $result
53+
*
54+
* @throws InvalidArgumentException
55+
*/
56+
private function assertIsValid(ConstraintViolationListInterface $result): void
57+
{
58+
if (count($result) < 1) {
59+
return;
60+
}
61+
62+
$errors = [];
63+
foreach ($result as $violation) {
64+
$errors[] = $violation->getPropertyPath() . ": " . $violation->getMessage();
65+
}
66+
67+
throw new InvalidArgumentException('validation failed: ' . implode("; ", $errors));
68+
}
69+
}

0 commit comments

Comments
 (0)