Skip to content

Commit

Permalink
Allow to set enabled and forms on fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Feb 28, 2024
1 parent e3375e7 commit a631fe3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 38 deletions.
37 changes: 9 additions & 28 deletions src/Fixture/Factory/TermsExampleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Setono\SyliusTermsPlugin\Fixture\Factory;

use Closure;
use DateTime;
use DateTimeInterface;
use Faker\Factory as FakerFactory;
use Faker\Generator as FakerGenerator;
use Setono\SyliusTermsPlugin\Model\TermsInterface;
use Setono\SyliusTermsPlugin\Repository\TermsRepositoryInterface;
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
use Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Locale\Model\LocaleInterface;
Expand Down Expand Up @@ -57,6 +55,9 @@ public function create(array $options = []): TermsInterface
$terms->addChannel($channel);
}

$terms->setEnabled($options['enabled']);
$terms->setForms($options['forms']);

// add translation for each defined locales
foreach ($this->getLocales() as $localeCode) {
$this->createTranslation($terms, $localeCode, $options);
Expand All @@ -67,9 +68,6 @@ public function create(array $options = []): TermsInterface
$this->createTranslation($terms, $localeCode, $translationOptions);
}

$terms->setCreatedAt($options['created_at']);
$terms->setUpdatedAt($options['updated_at']);

return $terms;
}

Expand Down Expand Up @@ -101,6 +99,9 @@ protected function configureOptions(OptionsResolver $resolver): void

->setDefault('slug', null)

->setDefault('enabled', true)
->setAllowedTypes('enabled', ['bool'])

->setDefault('label', function (Options $options): string {
return $this->faker->text(60); // @todo add link to this text
})
Expand All @@ -110,13 +111,8 @@ protected function configureOptions(OptionsResolver $resolver): void
->setDefault('translations', [])
->setAllowedTypes('translations', ['array'])

->setDefault('created_at', null)
->setAllowedTypes('created_at', ['null', 'string', DateTimeInterface::class])
->setNormalizer('created_at', self::getDateTimeNormalizer())

->setDefault('updated_at', null)
->setAllowedTypes('updated_at', ['null', 'string', DateTimeInterface::class])
->setNormalizer('updated_at', self::getDateTimeNormalizer())
->setDefault('forms', [CompleteType::class])
->setAllowedTypes('forms', ['array'])
;
}

Expand All @@ -128,19 +124,4 @@ private function getLocales(): iterable
yield $locale->getCode();
}
}

private static function getDateTimeNormalizer(): Closure
{
return static function (Options $options, null|object|string $previousValue) {
if (null === $previousValue) {
return null;
}

if (is_object($previousValue)) {
return $previousValue;
}

return new DateTime($previousValue);
};
}
}
6 changes: 2 additions & 4 deletions src/Fixture/TermsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ protected function configureResourceNode(ArrayNodeDefinition $resourceNode): voi
->scalarNode('slug')->cannotBeEmpty()->end()
->scalarNode('label')->cannotBeEmpty()->end()
->scalarNode('content')->cannotBeEmpty()->end()

->booleanNode('enabled')->defaultTrue()->end()
->variableNode('forms')->end()
->variableNode('translations')->cannotBeEmpty()->defaultValue([])->end()
->variableNode('channels')->cannotBeEmpty()->defaultValue([])->end()

->scalarNode('created_at')->cannotBeEmpty()->end()
->scalarNode('updated_at')->cannotBeEmpty()->end()
;
}
}
9 changes: 9 additions & 0 deletions src/Model/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public function getForms(): array
return $this->forms ?? [];
}

public function setForms(array $forms): void
{
$this->forms = null;

foreach ($forms as $form) {
$this->addForm($form);
}
}

public function addForm(string $form): void
{
if (null === $this->forms) {
Expand Down
5 changes: 5 additions & 0 deletions src/Model/TermsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function setContent(string $content): void;
*/
public function getForms(): array;

/**
* @param list<class-string> $forms
*/
public function setForms(array $forms): void;

/**
* @param class-string $form
*/
Expand Down
6 changes: 0 additions & 6 deletions src/Resources/config/app/fixtures.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ sylius_fixtures:
name: EULA for United States
slug: eula-us
label: Accept US EULA terms
created_at: "-3 day"
updated_at: now
channels:
- FASHION_WEB
terms_eula_eu:
Expand All @@ -94,8 +92,6 @@ sylius_fixtures:
fr_FR:
name: CLUF
slug: cluf
created_at: "-3 day"
updated_at: now
channels:
- EU_WEB
terms_terms_and_conditions:
Expand All @@ -109,8 +105,6 @@ sylius_fixtures:
slug: termes-conditions
de_DE:
name: Geschäftsbedingungen
created_at: "-3 day"
updated_at: now
channels:
- FASHION_WEB
- EU_WEB

0 comments on commit a631fe3

Please sign in to comment.