Skip to content

Commit

Permalink
Add changes to tests
Browse files Browse the repository at this point in the history
Add doc
  • Loading branch information
arti0090 committed Jul 28, 2021
1 parent 0b20ad8 commit 975c850
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 4 deletions.
21 changes: 21 additions & 0 deletions UPGRADE-API-1.11.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# UPGRADE FROM `v1.10.X` TO `v1.11.0`

1. The product images should have a proper prefix added to the path, so the images could be resolved.
This is now done and response of `Product Image` resource has been extended with parameter `media_path`.

```diff
{
"@context": "/api/v2/contexts/ProductImage",
"@id": "/api/v2/shop/product-images/123",
"@type": "ProductImage",
"id": "123",
"type": "thumbnail",
"path": "uo/product.jpg",
+ "media_path": "media/image/uo/product.jpg"
}
```
To change the prefix you need to set parameter in ``app/config/packages/_sylius.yaml``:

```yaml
sylius_api:
product_image_prefix: 'media/image'
```
1. `Sylius\Bundle\ApiBundle\Doctrine\Filters\ExchangeRateFilter` and `Sylius\Bundle\ApiBundle\Doctrine\Filters\TranslationOrderNameAndLocaleFilter` has been moved to `Sylius\Bundle\ApiBundle\Doctrine\Filter\ExchangeRateFilter` and `Sylius\Bundle\ApiBundle\Doctrine\Filter\TranslationOrderNameAndLocaleFilter` respectively.

1. `Sylius\Bundle\ApiBundle\View\CartShippingMethodInterface` and `Sylius\Bundle\ApiBundle\View\CartShippingMethod` have been removed.
Expand Down
Empty file added docker/xdebug/docker-xdebug.ini
Empty file.
70 changes: 70 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Serializer/ProductImageNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\ApiBundle\Serializer;

use Sylius\Component\Core\Model\ProductImageInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Webmozart\Assert\Assert;

/** @experimental */
class ProductImageNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

private const ALREADY_CALLED = 'product_image_normalizer_already_called';

/** @var string */
private $prefix;

public function __construct(string $prefix)
{
$this->prefix = $this->validatePrefix($prefix);
}

public function normalize($object, string $format = null, array $context = [])
{
Assert::isInstanceOf($object, ProductImageInterface::class);
Assert::keyNotExists($context, self::ALREADY_CALLED);

$context[self::ALREADY_CALLED] = true;

$data = $this->normalizer->normalize($object, $format, $context);

$data['media_path'] = $this->prefix . $data['path'];

return $data;
}

public function supportsNormalization($data, string $format = null, array $context = []): bool
{
if (isset($context[self::ALREADY_CALLED])) {
return false;
}

return $data instanceof ProductImageInterface;
}

private function validatePrefix(string $prefix): string
{
if ('/' !== substr($prefix, 0)) {
$prefix = '/'.$prefix;
}

if ('/' === substr($prefix, -1)) {
return $prefix;
}

return $prefix . '/';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\Serializer;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ProductImageInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class ProductImageNormalizerSpec extends ObjectBehavior
{
function let(): void {
$this->beConstructedWith('prefix');
}

function it_implements_context_aware_normalizer_interface(): void
{
$this->shouldImplement(ContextAwareNormalizerInterface::class);
}

function it_supports_only_product_image_interface(ProductImageInterface $productImage, OrderInterface $order): void
{
$this->supportsNormalization($productImage)->shouldReturn(true);
$this->supportsNormalization($order)->shouldReturn(false);
}

function it_serializes_product_image_with_proper_prefix(
NormalizerInterface $normalizer,
ProductImageInterface $productImage
): void {
$this->setNormalizer($normalizer);

$normalizer->normalize($productImage, null, ['product_image_normalizer_already_called' => true])->willReturn(['path' => 'some_path']);

$this->normalize($productImage, null, [])->shouldReturn(['path' => 'some_path', 'media_path' => '/prefix/some_path']);
}
}
2 changes: 1 addition & 1 deletion tests/Api/DataFixtures/ORM/product_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Sylius\Component\Core\Model\Product:
Sylius\Component\Core\Model\ProductImage:
product_thumbnail:
type: "thumbnail"
path: "/uo/product.jpg"
path: "uo/product.jpg"
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"@type": "ProductImage",
"id": "@integer@",
"type": "thumbnail",
"path": "\/uo\/product.jpg"
"path": "uo\/product.jpg",
"media_path": "\/media\/image\/uo\/product.jpg"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"@type": "ProductImage",
"id": "@integer@",
"type": "thumbnail",
"path": "\/uo\/product.jpg"
"path": "uo\/product.jpg",
"media_path": "\/media\/image\/uo\/product.jpg"
}
],
"hydra:totalItems": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"@type": "ProductImage",
"id": "@integer@",
"type": "thumbnail",
"path": "\/uo\/product.jpg"
"path": "uo\/product.jpg",
"media_path": "\/media\/image\/uo\/product.jpg"
}

0 comments on commit 975c850

Please sign in to comment.