Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Sylius/Bundle/CoreBundle/Fixture/AdminUserFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected function configureResourceNode(ArrayNodeDefinition $resourceNode): voi
->scalarNode('locale_code')->cannotBeEmpty()->end()
->scalarNode('first_name')->cannotBeEmpty()->end()
->scalarNode('last_name')->cannotBeEmpty()->end()
->scalarNode('avatar')->cannotBeEmpty()->end()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
namespace Sylius\Bundle\CoreBundle\Fixture\Factory;

use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\Core\Model\AvatarImage;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -32,15 +36,32 @@ class AdminUserExampleFactory extends AbstractExampleFactory implements ExampleF
/** @var string */
private $localeCode;

public function __construct(FactoryInterface $userFactory, string $localeCode)
{
/** @var FileLocatorInterface */
private $fileLocator;

/** @var ImageUploaderInterface */
private $imageUploader;

public function __construct(
FactoryInterface $userFactory,
string $localeCode,
?FileLocatorInterface $fileLocator = null,
?ImageUploaderInterface $imageUploader = null
) {
$this->userFactory = $userFactory;
$this->localeCode = $localeCode;

$this->faker = \Faker\Factory::create();
$this->optionsResolver = new OptionsResolver();

$this->configureOptions($this->optionsResolver);

$this->fileLocator = $fileLocator;
$this->imageUploader = $imageUploader;

if ($this->fileLocator === null || $this->imageUploader === null) {
@trigger_error(sprintf('Not passing a $fileLocator or/and $imageUploader to %s constructor is deprecated since Sylius 1.6 and will be removed in Sylius 2.0.', self::class), \E_USER_DEPRECATED);
}
}

/**
Expand Down Expand Up @@ -70,6 +91,10 @@ public function create(array $options = []): AdminUserInterface
$user->addRole('ROLE_API_ACCESS');
}

if (!($options['avatar'] === '')) {
$this->createAvatar($user, $options);
}

return $user;
}

Expand All @@ -92,6 +117,27 @@ protected function configureOptions(OptionsResolver $resolver): void
->setDefault('api', false)
->setDefined('first_name')
->setDefined('last_name')
->setDefault('avatar', '')
->setAllowedTypes('avatar', 'string')
;
}

private function createAvatar(AdminUserInterface $adminUser, array $options): void
{
if ($this->fileLocator === null || $this->imageUploader === null) {
throw new \RuntimeException('You must configure a $fileLocator or/and $imageUploader');
}

$imagePath = $options['avatar'];

$imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath);
$uploadedImage = new UploadedFile($imagePath, basename($imagePath));

$avatarImage = new AvatarImage();
$avatarImage->setFile($uploadedImage);

$this->imageUploader->upload($avatarImage);

$adminUser->setAvatar($avatarImage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ sylius_fixtures:
locale_code: '%locale%'
first_name: 'John'
last_name: 'Doe'
avatar: '@SyliusCoreBundle/Resources/fixtures/adminAvatars/john.jpg'

- email: '[email protected]'
username: 'api'
Expand All @@ -120,6 +121,7 @@ sylius_fixtures:
first_name: 'Luke'
last_name: 'Brushwood'
api: true
avatar: '@SyliusCoreBundle/Resources/fixtures/adminAvatars/luke.jpg'

tax_category:
options:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<service id="sylius.fixture.example_factory.admin_user" class="Sylius\Bundle\CoreBundle\Fixture\Factory\AdminUserExampleFactory">
<argument type="service" id="sylius.factory.admin_user" />
<argument>%locale%</argument>
<argument type="service" id="file_locator" />
<argument type="service" id="sylius.image_uploader" />
</service>

<service id="sylius.fixture.example_factory.promotion" class="Sylius\Bundle\CoreBundle\Fixture\Factory\PromotionExampleFactory">
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.