-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #16269 Bump API Platform to 3.3 and refactor uploading avatar i…
…mages (GSadee) This PR was merged into the api-platform-3 branch. Discussion ---------- | Q | A |-----------------|----- | Branch? | api-platform-3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no<!-- don't forget to update the UPGRADE-*.md file --> | Related tickets | | License | MIT <!-- - Bug fixes must be submitted against the 1.12 or 1.13 branches - Features and deprecations must be submitted against the 1.14 branch - Features, removing deprecations and BC breaks must be submitted against the 2.0 branch - Make sure that the correct base branch is set To be sure you are not breaking any Backward Compatibilities, check the documentation: https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html --> Commits ------- Bump API Platform to 3.3 [API] Implement state processor for uploading avatar images instead of controller [API] Remove the old avatar image before creating a new one [API] Make type optional in ImageCreatorInterface [Core] Add spec for setting null as image in admin user
- Loading branch information
Showing
14 changed files
with
282 additions
and
40 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
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
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
43 changes: 43 additions & 0 deletions
43
src/Sylius/Bundle/ApiBundle/StateProcessor/Post/UploadAvatarImageProcessor.php
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 | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\StateProcessor\Post; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProcessorInterface; | ||
use Sylius\Bundle\ApiBundle\Creator\ImageCreatorInterface; | ||
use Sylius\Component\Core\Repository\AvatarImageRepositoryInterface; | ||
|
||
final readonly class UploadAvatarImageProcessor implements ProcessorInterface | ||
{ | ||
public function __construct( | ||
private ProcessorInterface $processor, | ||
private ImageCreatorInterface $avatarImageCreator, | ||
private AvatarImageRepositoryInterface $avatarImageRepository, | ||
) { | ||
} | ||
|
||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) | ||
{ | ||
if ($data !== null) { | ||
$this->avatarImageRepository->remove($data); | ||
} | ||
|
||
$image = $this->avatarImageCreator->create( | ||
$context['request']->attributes->getString('id'), | ||
$context['request']->files->get('file'), | ||
); | ||
|
||
return $this->processor->process($image, $operation, $uriVariables, $context); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/Sylius/Bundle/ApiBundle/spec/Creator/AvatarImageCreatorSpec.php
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,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* 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\Creator; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Bundle\ApiBundle\Exception\AdminUserNotFoundException; | ||
use Sylius\Bundle\ApiBundle\Exception\NoFileUploadedException; | ||
use Sylius\Component\Core\Model\AdminUserInterface; | ||
use Sylius\Component\Core\Model\AvatarImageInterface; | ||
use Sylius\Component\Core\Uploader\ImageUploaderInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
|
||
final class AvatarImageCreatorSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
FactoryInterface $avatarImageFactory, | ||
RepositoryInterface $adminUserRepository, | ||
ImageUploaderInterface $imageUploader, | ||
) { | ||
$this->beConstructedWith($avatarImageFactory, $adminUserRepository, $imageUploader); | ||
} | ||
|
||
function it_creates_an_avatar_image( | ||
FactoryInterface $avatarImageFactory, | ||
RepositoryInterface $adminUserRepository, | ||
ImageUploaderInterface $imageUploader, | ||
AdminUserInterface $adminUser, | ||
AvatarImageInterface $avatarImage, | ||
): void { | ||
$file = new \SplFileInfo(__FILE__); | ||
|
||
$adminUserRepository->find('1')->willReturn($adminUser); | ||
|
||
$avatarImageFactory->createNew()->willReturn($avatarImage); | ||
$avatarImage->setFile($file)->shouldBeCalled(); | ||
|
||
$adminUser->setImage($avatarImage)->shouldBeCalled(); | ||
|
||
$imageUploader->upload($avatarImage)->shouldBeCalled(); | ||
|
||
$this->create('1', $file, null)->shouldReturn($avatarImage); | ||
} | ||
|
||
function it_throws_an_exception_if_admin_user_is_not_found( | ||
FactoryInterface $avatarImageFactory, | ||
RepositoryInterface $adminUserRepository, | ||
ImageUploaderInterface $imageUploader, | ||
): void { | ||
$file = new \SplFileInfo(__FILE__); | ||
|
||
$adminUserRepository->find('1')->willReturn(null); | ||
|
||
$avatarImageFactory->createNew()->shouldNotBeCalled(); | ||
$imageUploader->upload(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this | ||
->shouldThrow(AdminUserNotFoundException::class) | ||
->during('create', ['1', $file, null]) | ||
; | ||
} | ||
|
||
function it_throws_an_exception_if_there_is_no_uploaded_file( | ||
FactoryInterface $avatarImageFactory, | ||
RepositoryInterface $adminUserRepository, | ||
ImageUploaderInterface $imageUploader, | ||
): void { | ||
$adminUserRepository->find('1')->shouldNotBeCalled(); | ||
$avatarImageFactory->createNew()->shouldNotBeCalled(); | ||
$imageUploader->upload(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this | ||
->shouldThrow(NoFileUploadedException::class) | ||
->during('create', ['1', null, null]) | ||
; | ||
} | ||
} |
Oops, something went wrong.