Skip to content

Commit dc5a76d

Browse files
authored
Merge pull request #6 from Locastic/fixture-service-bug
RDY: Fixture bug
2 parents fa8be5a + ad5cb2a commit dc5a76d

File tree

8 files changed

+74
-160
lines changed

8 files changed

+74
-160
lines changed

src/Entity/ShippingMethod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Sylius\Component\Core\Model\ShippingMethod as BaseShippingMethod;
66
use Sylius\Component\Shipping\Model\ShippingMethodTranslationInterface;
77

8-
class ShippingMethod extends BaseShippingMethod implements IsPickupAtStoreInterface
8+
class ShippingMethod extends BaseShippingMethod implements ShippingMethodInterface
99
{
1010
use IsPickupAtStoreTrait;
1111

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Locastic\SyliusStoreLocatorPlugin\Entity;
4+
5+
use Sylius\Component\Core\Model\ShippingMethodInterface as BaseShippingMethodInterface;
6+
7+
interface ShippingMethodInterface extends BaseShippingMethodInterface, IsPickupAtStoreInterface
8+
{
9+
10+
}

src/Entity/Store.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Store implements StoreInterface
1717
use TimestampableTrait;
1818
use TranslatableTrait {
1919
__construct as protected initializeTranslationsCollection;
20+
getTranslation as public translatableTraitGetTranslation;
2021
}
2122

2223
protected $id;
@@ -119,72 +120,72 @@ public function setAddress(?string $address): void
119120

120121
public function getSlug(): ?string
121122
{
122-
return $this->getTranslation()->getSlug();
123+
return $this->getStoreLocatorTranslation()->getSlug();
123124
}
124125

125126
public function setSlug(?string $slug): void
126127
{
127-
$this->getTranslation()->setSlug($slug);
128+
$this->getStoreLocatorTranslation()->setSlug($slug);
128129
}
129130

130131
public function getName(): ?string
131132
{
132-
return $this->getTranslation()->getName();
133+
return $this->getStoreLocatorTranslation()->getName();
133134
}
134135

135136
public function setName(?string $name): void
136137
{
137-
$this->getTranslation()->setName($name);
138+
$this->getStoreLocatorTranslation()->setName($name);
138139
}
139140

140141
public function getMetaTitle(): ?string
141142
{
142-
return $this->getTranslation()->getMetaTitle();
143+
return $this->getStoreLocatorTranslation()->getMetaTitle();
143144
}
144145

145146
public function setMetaTitle(?string $metaTitle): void
146147
{
147-
$this->getTranslation()->setMetaTitle($metaTitle);
148+
$this->getStoreLocatorTranslation()->setMetaTitle($metaTitle);
148149
}
149150

150151
public function getMetaKeywords(): ?string
151152
{
152-
return $this->getTranslation()->getMetaKeywords();
153+
return $this->getStoreLocatorTranslation()->getMetaKeywords();
153154
}
154155

155156
public function setMetaKeywords(?string $metaKeywords): void
156157
{
157-
$this->getTranslation()->setMetaKeywords($metaKeywords);
158+
$this->getStoreLocatorTranslation()->setMetaKeywords($metaKeywords);
158159
}
159160

160161
public function getMetaDescription(): ?string
161162
{
162-
return $this->getTranslation()->getMetaDescription();
163+
return $this->getStoreLocatorTranslation()->getMetaDescription();
163164
}
164165

165166
public function setMetaDescription(?string $metaDescription): void
166167
{
167-
$this->getTranslation()->setMetaDescription($metaDescription);
168+
$this->getStoreLocatorTranslation()->setMetaDescription($metaDescription);
168169
}
169170

170171
public function getContent(): ?string
171172
{
172-
return $this->getTranslation()->getContent();
173+
return $this->getStoreLocatorTranslation()->getContent();
173174
}
174175

175176
public function setContent(?string $content): void
176177
{
177-
$this->getTranslation()->setContent($content);
178+
$this->getStoreLocatorTranslation()->setContent($content);
178179
}
179180

180181
public function getOpeningHours(): ?string
181182
{
182-
return $this->getTranslation()->getOpeningHours();
183+
return $this->getStoreLocatorTranslation()->getOpeningHours();
183184
}
184185

185186
public function setOpeningHours(?string $openingHours): void
186187
{
187-
$this->getTranslation()->setOpeningHours($openingHours);
188+
$this->getStoreLocatorTranslation()->setOpeningHours($openingHours);
188189
}
189190

190191
public function setPickupAtStoreAvailable(bool $pickupAtStoreAvailable): void
@@ -199,7 +200,7 @@ public function isPickupAtStoreAvailable(): ?bool
199200

200201
protected function getStoreTranslation(): StoreTranslationInterface
201202
{
202-
return $this->getTranslation();
203+
return $this->getStoreLocatorTranslation();
203204
}
204205

205206
protected function createTranslation(): ?StoreTranslationInterface
@@ -217,12 +218,12 @@ public function hasImages(): bool
217218
return !$this->images->isEmpty();
218219
}
219220

220-
public function hasImage(?ImageInterface $image): bool
221+
public function hasImage(ImageInterface $image): bool
221222
{
222223
return $this->images->contains($image);
223224
}
224225

225-
public function addImage(?ImageInterface $image): void
226+
public function addImage(ImageInterface $image): void
226227
{
227228
if ($image->hasFile()) {
228229
$image->setOwner($this);
@@ -238,5 +239,11 @@ public function removeImage(ImageInterface $image)
238239
}
239240
}
240241

242+
public function getStoreLocatorTranslation(?string $locale = null): StoreTranslationInterface
243+
{
244+
/** @var StoreTranslationInterface $storeTranslation */
245+
$storeTranslation = $this->translatableTraitGetTranslation($locale);
241246

247+
return $storeTranslation;
248+
}
242249
}

src/Entity/StoreInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getImages(): Collection;
6161

6262
public function hasImages(): bool;
6363

64-
public function hasImage(?ImageInterface $image): bool;
64+
public function hasImage(ImageInterface $image): bool;
6565

6666
public function addImage(ImageInterface $image): void;
6767

src/Fixture/ShippingMethodFactory.php

Lines changed: 9 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,22 @@
44

55
namespace Locastic\SyliusStoreLocatorPlugin\Fixture;
66

7-
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
8-
use Sylius\Component\Addressing\Model\ZoneInterface;
7+
use Locastic\SyliusStoreLocatorPlugin\Entity\ShippingMethodInterface;
8+
use Sylius\Bundle\CoreBundle\Fixture\Factory\ShippingMethodExampleFactory;
99
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
10-
use Sylius\Component\Core\Formatter\StringInflector;
11-
use Sylius\Component\Core\Model\ChannelInterface;
12-
use Sylius\Component\Core\Model\ShippingMethodInterface;
13-
use Sylius\Component\Locale\Model\LocaleInterface;
10+
use Sylius\Component\Core\Model\ShippingMethodInterface as BaseShippingMethodInterface;
1411
use Sylius\Component\Resource\Factory\FactoryInterface;
1512
use Sylius\Component\Resource\Repository\RepositoryInterface;
16-
use Sylius\Component\Shipping\Calculator\DefaultCalculators;
17-
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
1813
use Symfony\Component\OptionsResolver\Options;
1914
use Symfony\Component\OptionsResolver\OptionsResolver;
20-
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
2115

22-
class ShippingMethodFactory extends AbstractExampleFactory
16+
class ShippingMethodFactory extends ShippingMethodExampleFactory
2317
{
24-
/**
25-
* @var FactoryInterface
26-
*/
27-
private $shippingMethodFactory;
28-
29-
/**
30-
* @var RepositoryInterface
31-
*/
32-
private $zoneRepository;
33-
34-
/**
35-
* @var RepositoryInterface
36-
*/
37-
private $shippingCategoryRepository;
38-
39-
/**
40-
* @var RepositoryInterface
41-
*/
42-
private $localeRepository;
43-
44-
/**
45-
* @var ChannelRepositoryInterface
46-
*/
47-
private $channelRepository;
48-
4918
/**
5019
* @var \Faker\Generator
5120
*/
5221
private $faker;
5322

54-
/**
55-
* @var OptionsResolver
56-
*/
57-
private $optionsResolver;
58-
5923
/**
6024
* @param FactoryInterface $shippingMethodFactory
6125
* @param RepositoryInterface $zoneRepository
@@ -70,55 +34,21 @@ public function __construct(
7034
RepositoryInterface $localeRepository,
7135
ChannelRepositoryInterface $channelRepository
7236
) {
73-
$this->shippingMethodFactory = $shippingMethodFactory;
74-
$this->zoneRepository = $zoneRepository;
75-
$this->shippingCategoryRepository = $shippingCategoryRepository;
76-
$this->localeRepository = $localeRepository;
77-
$this->channelRepository = $channelRepository;
78-
79-
$this->faker = \Faker\Factory::create();
80-
$this->optionsResolver = new OptionsResolver();
81-
82-
$this->configureOptions($this->optionsResolver);
37+
parent::__construct($shippingMethodFactory, $zoneRepository, $shippingCategoryRepository, $localeRepository, $channelRepository);
8338
}
8439

8540
/**
8641
* {@inheritdoc}
8742
*/
88-
public function create(array $options = []): ShippingMethodInterface
43+
public function create(array $options = []): BaseShippingMethodInterface
8944
{
90-
$options = $this->optionsResolver->resolve($options);
91-
92-
9345
/** @var ShippingMethodInterface $shippingMethod */
94-
$shippingMethod = $this->shippingMethodFactory->createNew();
95-
$shippingMethod->setCode($options['code']);
96-
$shippingMethod->setEnabled($options['enabled']);
97-
$shippingMethod->setZone($options['zone']);
98-
$shippingMethod->setCalculator($options['calculator']['type']);
99-
$shippingMethod->setConfiguration($options['calculator']['configuration']);
100-
$shippingMethod->setArchivedAt($options['archived_at']);
101-
102-
if (array_key_exists('shipping_category', $options)) {
103-
$shippingMethod->setCategory($options['shipping_category']);
104-
}
46+
$shippingMethod = parent::create($options);
10547

10648
if (array_key_exists('is_pickup_at_store', $options)) {
10749
$shippingMethod->setPickupAtStore($options['is_pickup_at_store']);
10850
}
10951

110-
foreach ($this->getLocales() as $localeCode) {
111-
$shippingMethod->setCurrentLocale($localeCode);
112-
$shippingMethod->setFallbackLocale($localeCode);
113-
114-
$shippingMethod->setName($options['name']);
115-
$shippingMethod->setDescription($options['description']);
116-
}
117-
118-
foreach ($options['channels'] as $channel) {
119-
$shippingMethod->addChannel($channel);
120-
}
121-
12252
return $shippingMethod;
12353
}
12454

@@ -127,58 +57,12 @@ public function create(array $options = []): ShippingMethodInterface
12757
*/
12858
protected function configureOptions(OptionsResolver $resolver): void
12959
{
60+
parent::configureOptions($resolver);
61+
13062
$resolver
131-
->setDefault('code', function (Options $options): string {
132-
return StringInflector::nameToCode($options['name']);
133-
})
134-
->setDefault('name', function (Options $options): string {
135-
return $this->faker->words(3, true);
136-
})
137-
->setDefault('description', function (Options $options): string {
138-
return $this->faker->sentence();
139-
})
140-
->setDefault('enabled', function (Options $options): bool {
141-
return $this->faker->boolean(90);
142-
})
14363
->setDefault('is_pickup_at_store', function (Options $options): bool {
14464
return $this->faker->boolean(90);
14565
})
146-
->setAllowedTypes('enabled', 'bool')
147-
->setDefault('zone', LazyOption::randomOne($this->zoneRepository))
148-
->setAllowedTypes('zone', ['null', 'string', ZoneInterface::class])
149-
->setNormalizer('zone', LazyOption::findOneBy($this->zoneRepository, 'code'))
150-
->setDefined('shipping_category')
151-
->setAllowedTypes('shipping_category', ['null', 'string', ShippingCategoryInterface::class])
152-
->setNormalizer('shipping_category', LazyOption::findOneBy($this->shippingCategoryRepository, 'code'))
153-
->setDefault('calculator', function (Options $options): array {
154-
$configuration = [];
155-
/** @var ChannelInterface $channel */
156-
foreach ($options['channels'] as $channel) {
157-
$configuration[$channel->getCode()] = ['amount' => $this->faker->randomNumber(4)];
158-
}
159-
160-
return [
161-
'type' => DefaultCalculators::FLAT_RATE,
162-
'configuration' => $configuration,
163-
];
164-
})
165-
->setDefault('channels', LazyOption::all($this->channelRepository))
166-
->setAllowedTypes('channels', 'array')
167-
->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))
168-
->setDefault('archived_at', null)
169-
->setAllowedTypes('archived_at', ['null', \DateTimeInterface::class])
17066
;
17167
}
172-
173-
/**
174-
* @return iterable
175-
*/
176-
private function getLocales(): iterable
177-
{
178-
/** @var LocaleInterface[] $locales */
179-
$locales = $this->localeRepository->findAll();
180-
foreach ($locales as $locale) {
181-
yield $locale->getCode();
182-
}
183-
}
18468
}

src/Fixture/ShippingMethodFixture.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,34 @@
44

55
namespace Locastic\SyliusStoreLocatorPlugin\Fixture;
66

7+
use Sylius\Bundle\CoreBundle\Fixture\AbstractResourceFixture;
78
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8-
use Sylius\Bundle\CoreBundle\Fixture\ShippingMethodFixture as BaseShippingMethodFixture;
99

10-
class ShippingMethodFixture extends BaseShippingMethodFixture
10+
class ShippingMethodFixture extends AbstractResourceFixture
1111
{
12-
protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
12+
public function getName(): string
1313
{
14-
parent::configureResourceNode($resourceNode);
14+
return 'app_shipping_method';
15+
}
1516

17+
protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
18+
{
1619
$resourceNode
1720
->children()
18-
->booleanNode('is_pickup_at_store')->end();
21+
->scalarNode('code')->cannotBeEmpty()->end()
22+
->scalarNode('name')->cannotBeEmpty()->end()
23+
->scalarNode('description')->cannotBeEmpty()->end()
24+
->scalarNode('zone')->cannotBeEmpty()->end()
25+
->booleanNode('enabled')->end()
26+
->scalarNode('category')->end()
27+
->arrayNode('channels')->scalarPrototype()->end()->end()
28+
->arrayNode('calculator')
29+
->children()
30+
->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
31+
->variableNode('configuration')->end()
32+
->end()
33+
->end()
34+
->booleanNode('is_pickup_at_store')->end()
35+
;
1936
}
2037
}

0 commit comments

Comments
 (0)