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

Skip to content

Commit 18b0b24

Browse files
committed
improve fixtures of Catalog Promotion
1 parent ef04d64 commit 18b0b24

5 files changed

Lines changed: 101 additions & 23 deletions

File tree

src/Sylius/Behat/Context/Setup/CatalogPromotionContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,8 @@ private function createCatalogPromotion(
803803
$catalogPromotion = $this->catalogPromotionExampleFactory->create([
804804
'name' => $name,
805805
'code' => $code,
806-
'startDate' => $startDate,
807-
'endDate' => $endDate,
806+
'start_date' => $startDate,
807+
'end_date' => $endDate,
808808
'enabled' => $enabled,
809809
'channels' => $channels,
810810
'actions' => $actions,

src/Sylius/Bundle/CoreBundle/Fixture/CatalogPromotionFixture.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ protected function configureResourceNode(ArrayNodeDefinition $resourceNode): voi
3030
->scalarNode('name')->cannotBeEmpty()->end()
3131
->scalarNode('label')->cannotBeEmpty()->end()
3232
->scalarNode('description')->cannotBeEmpty()->end()
33+
->booleanNode('exclusive')->end()
34+
->integerNode('priority')->end()
35+
->scalarNode('start_date')->cannotBeEmpty()->end()
36+
->scalarNode('end_date')->cannotBeEmpty()->end()
3337
->arrayNode('channels')->scalarPrototype()->end()->end()
3438
->arrayNode('scopes')
3539
->requiresAtLeastOneElement()

src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionExampleFactory.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ public function create(array $options = []): CatalogPromotionInterface
5454
$catalogPromotion = $this->catalogPromotionFactory->createNew();
5555
$catalogPromotion->setCode($options['code']);
5656
$catalogPromotion->setName($options['name']);
57-
$catalogPromotion->setStartDate($options['startDate']);
58-
$catalogPromotion->setEndDate($options['endDate']);
57+
if (isset($options['start_date'])) {
58+
$catalogPromotion->setStartDate($this->getDate($options['start_date']));
59+
}
60+
if (isset($options['end_date'])) {
61+
$catalogPromotion->setEndDate($this->getDate($options['end_date']));
62+
}
63+
5964
$catalogPromotion->setEnabled($options['enabled']);
6065
$catalogPromotion->setPriority($options['priority'] ?? 0);
6166
$catalogPromotion->setExclusive($options['exclusive'] ?? false);
@@ -96,28 +101,17 @@ public function create(array $options = []): CatalogPromotionInterface
96101
protected function configureOptions(OptionsResolver $resolver): void
97102
{
98103
$resolver
99-
->setDefault('code', function (Options $options): string {
100-
return StringInflector::nameToCode($options['name']);
101-
})
104+
->setDefault('code', fn(Options $options): string => StringInflector::nameToCode($options['name']))
102105
->setNormalizer('code', static function (Options $options, ?string $code): string {
103106
if ($code === null) {
104107
return StringInflector::nameToCode($options['name']);
105108
}
106109

107110
return $code;
108111
})
109-
->setDefault('name', function (Options $options): string {
110-
/** @var string $words */
111-
$words = $this->faker->words(3, true);
112-
113-
return $words;
114-
})
115-
->setDefault('label', function (Options $options): string {
116-
return $options['name'];
117-
})
118-
->setDefault('description', function (Options $options): string {
119-
return $this->faker->sentence();
120-
})
112+
->setDefault('name', fn(Options $options): string => (string) $this->faker->words(3, true))
113+
->setDefault('label', fn(Options $options): string => $options['name'])
114+
->setDefault('description', fn(Options $options): string => $this->faker->sentence())
121115
->setDefault('channels', LazyOption::all($this->channelRepository))
122116
->setAllowedTypes('channels', 'array')
123117
->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))
@@ -127,10 +121,10 @@ protected function configureOptions(OptionsResolver $resolver): void
127121
->setAllowedTypes('priority', ['integer', 'null'])
128122
->setDefault('exclusive', false)
129123
->setAllowedTypes('exclusive', ['boolean', 'null'])
130-
->setDefault('startDate', null)
131-
->setAllowedTypes('startDate', [\DateTimeInterface::class, 'null'])
132-
->setDefault('endDate', null)
133-
->setAllowedTypes('endDate', [\DateTimeInterface::class, 'null'])
124+
->setDefault('start_date', null)
125+
->setAllowedTypes('start_date', [\DateTimeInterface::class, 'string', 'null'])
126+
->setDefault('end_date', null)
127+
->setAllowedTypes('end_date', [\DateTimeInterface::class, 'string', 'null'])
134128
->setDefault('enabled', true)
135129
->setAllowedTypes('enabled', 'boolean')
136130
;
@@ -144,4 +138,13 @@ private function getLocales(): iterable
144138
yield $locale->getCode();
145139
}
146140
}
141+
142+
private function getDate(\DateTimeInterface|string $date): \DateTimeInterface
143+
{
144+
if ($date instanceof \DateTimeInterface) {
145+
return $date;
146+
}
147+
148+
return new \DateTime($date);
149+
}
147150
}

src/Sylius/Bundle/CoreBundle/Resources/config/app/fixtures/promotion.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,53 @@ sylius_fixtures:
5656
- '000F_office_grey_jeans-variant-0'
5757
- '000F_office_grey_jeans-variant-1'
5858
- '000F_office_grey_jeans-variant-2'
59+
actions:
60+
- type: 'percentage_discount'
61+
configuration:
62+
amount: 0.5
63+
spring:
64+
code: 'spring'
65+
name: 'Spring sale'
66+
channels:
67+
- 'FASHION_WEB'
68+
priority: 100
69+
scopes:
70+
- type: 'for_taxons'
71+
configuration:
72+
taxons:
73+
- 'jeans'
74+
actions:
75+
- type: 'fixed_discount'
76+
configuration:
77+
amount: 3
78+
summer:
79+
code: 'summer'
80+
name: 'Summer sale'
81+
channels:
82+
- 'FASHION_WEB'
83+
exclusive: true
84+
priority: 30
85+
scopes:
86+
- type: 'for_variants'
87+
configuration:
88+
variants:
89+
- '000F_office_grey_jeans-variant-0'
90+
actions:
91+
- type: 'percentage_discount'
92+
configuration:
93+
amount: 0.5
94+
autumn:
95+
code: 'autumn'
96+
name: 'Autumn sale'
97+
start_date: '2 days'
98+
end_date: '10 days'
99+
channels:
100+
- 'FASHION_WEB'
101+
scopes:
102+
- type: 'for_products'
103+
configuration:
104+
products:
105+
- 'Knitted_wool_blend_green_cap'
59106
actions:
60107
- type: 'percentage_discount'
61108
configuration:

src/Sylius/Bundle/CoreBundle/Tests/Fixture/CatalogPromotionFixtureTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ public function catalog_promotion_name_can_be_set(): void
4848
$this->assertConfigurationIsValid([['custom' => [['name' => 'Name']]]], 'custom.*.name');
4949
}
5050

51+
/** @test */
52+
public function catalog_promotion_exclusiveness_can_be_set(): void
53+
{
54+
$this->assertConfigurationIsValid([['custom' => [['exclusive' => true]]]], 'custom.*.exclusive');
55+
}
56+
57+
/** @test */
58+
public function catalog_promotion_priority_can_be_set(): void
59+
{
60+
$this->assertConfigurationIsValid([['custom' => [['priority' => 4]]]], 'custom.*.priority');
61+
}
62+
63+
/** @test */
64+
public function catalog_promotion_start_date_can_be_set(): void
65+
{
66+
$this->assertConfigurationIsValid([['custom' => [['start_date' => '2020-01-02']]]], 'custom.*.start_date');
67+
}
68+
69+
/** @test */
70+
public function catalog_promotion_end_date_can_be_set(): void
71+
{
72+
$this->assertConfigurationIsValid([['custom' => [['end_date' => '2022-01-02']]]], 'custom.*.end_date');
73+
}
74+
5175
/** @test */
5276
public function catalog_promotion_description_can_be_set(): void
5377
{

0 commit comments

Comments
 (0)