Skip to content

Commit

Permalink
Merge pull request #377 from loic425/feature/foundry
Browse files Browse the repository at this point in the history
Use Foundry for data fixtures
  • Loading branch information
loic425 committed Jan 3, 2022
2 parents 9b3b2aa + ee07c30 commit 07a8b0b
Show file tree
Hide file tree
Showing 41 changed files with 380 additions and 739 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ test-behat-with-javascript:
.PHONY: test-behat-with-javascript

test-fixtures:
bin/console sylius:fixtures:load default --no-interaction
bin/console doctrine:fixtures:load --no-interaction
.PHONY: test-fixtures

install-package:
(cd $(path) && composer update --no-interaction --prefer-dist --no-scripts --no-plugins)
.PHONY: test-fixtures
.PHONY: install-package

test-package-phpstan:
(cd $(path) && vendor/bin/phpstan analyse -c phpstan.neon -l ${PHPSTAN_LEVEL})
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"App\\Context\\": "src/Monofony/Pack/CorePack/.recipe/src/Context/",
"App\\Command\\": "src/Monofony/Pack/CorePack/.recipe/src/Command/",
"App\\Dashboard\\": "src/Monofony/Pack/AdminPack/.recipe/src/Dashboard/",
"App\\DataFixtures\\": "src/Monofony/Pack/CorePack/.recipe/src/DataFixtures/",
"App\\DataTransformer\\": "src/Monofony/Pack/ApiPack/.recipe/src/DataTransformer/",
"App\\Entity\\": "src/Monofony/Pack/CorePack/.recipe/src/Entity/",
"App\\EventSubscriber\\": "src/Monofony/Pack/CorePack/.recipe/src/EventSubscriber/",
"App\\Factory\\": "src/Monofony/Pack/CorePack/.recipe/src/Factory/",
"App\\Fixture\\": "src/Monofony/Pack/CorePack/.recipe/src/Fixture/",
"App\\Form\\": "src/Monofony/Pack/CorePack/.recipe/src/Form/",
"App\\Grid\\": "src/Monofony/Pack/AdminPack/.recipe/src/Grid/",
Expand All @@ -33,6 +35,7 @@
"App\\Provider\\": "src/Monofony/Pack/ApiPack/.recipe/src/Provider/",
"App\\Repository\\": "src/Monofony/Pack/CorePack/.recipe/src/Repository/",
"App\\Security\\": "src/Monofony/Pack/CorePack/.recipe/src/Security/",
"App\\Story\\": "src/Monofony/Pack/CorePack/.recipe/src/Story/",
"App\\Tests\\": "tests/",
"App\\Validator\\": "src/Monofony/Pack/CorePack/.recipe/src/Validator/",
"App\\Tests\\Behat\\": "src/Monofony/Pack/CorePack/.recipe/tests/Behat/",
Expand Down Expand Up @@ -65,6 +68,7 @@
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.2",
"doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/doctrine-fixtures-bundle": "^3.4",
"doctrine/orm": "^2.5",
"fakerphp/faker": "^1.14",
"friendsofsymfony/oauth-server-bundle": "^1.6 || >2.0.0-alpha.0 ^2.0@dev",
Expand All @@ -77,7 +81,6 @@
"sensio/framework-extra-bundle": "^6.2",
"sensiolabs/security-checker": "^6.0",
"sonata-project/block-bundle": "^4.2",
"sylius/fixtures-bundle": "^1.7",
"sylius/grid-bundle": "dev-master",
"sylius/mailer-bundle": "^1.6",
"sylius/resource-bundle": "^1.8",
Expand All @@ -100,7 +103,8 @@
"symfony/webpack-encore-bundle": "^1.6",
"twig/extensions": "^1.5",
"twig/twig": "^2.7",
"vich/uploader-bundle": "^1.8"
"vich/uploader-bundle": "^1.8",
"zenstruck/foundry": "^1.14"
},
"require-dev": {
"behat/behat": "^3.2",
Expand All @@ -127,6 +131,7 @@
"rector/rector": "^0.11",
"se/selenium-server-standalone": "^3.141",
"symfony/debug-bundle": "^5.3",
"symfony/maker-bundle": "^1.36",
"symfony/web-profiler-bundle": "^5.3",
"symplify/easy-coding-standard": "^9.0",
"symplify/monorepo-builder": "^8.0",
Expand Down
4 changes: 3 additions & 1 deletion config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Knp\Bundle\MenuBundle\KnpMenuBundle::class => ['all' => true],
FOS\OAuthServerBundle\FOSOAuthServerBundle::class => ['all' => true],
Sylius\Bundle\FixturesBundle\SyliusFixturesBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['dev' => true, 'test' => true],
Expand All @@ -38,4 +37,7 @@
Sonata\Form\Bridge\Symfony\Bundle\SonataFormBundle::class => ['all' => true],
Sonata\BlockBundle\SonataBlockBundle::class => ['all' => true],
BabDev\PagerfantaBundle\BabDevPagerfantaBundle::class => ['all' => true],
Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['all' => true],
];
4 changes: 4 additions & 0 deletions config/packages/dev/zenstruck_foundry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# See full configuration: https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#full-default-bundle-configuration
zenstruck_foundry:
# Whether to auto-refresh proxies by default (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#auto-refresh)
auto_refresh_proxies: true
2 changes: 0 additions & 2 deletions config/packages/monofony_fixtures.yaml

This file was deleted.

5 changes: 5 additions & 0 deletions config/packages/test/zenstruck_foundry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Unless you want different configuration for test/dev environments,
# add configuration to config/packages/dev/zenstruck_foundry.yml
# and this will be synced to your test environment.
imports:
- { resource: ../dev/zenstruck_foundry.yaml }
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Monofony\Bridge\Behat\Service\SharedStorageInterface;
use Monofony\Contracts\Core\Model\User\AdminUserInterface;
use Webmozart\Assert\Assert;
use Zenstruck\Foundry\Proxy;

final class ManagingAdministratorsContext implements Context
{
Expand Down Expand Up @@ -56,7 +57,7 @@ public function iWantToCreateANewAdministrator(): void
* @When /^I am editing (my) details$/
* @When /^I want to edit (this administrator)$/
*/
public function iWantToEditThisAdministrator(AdminUserInterface $adminUser): void
public function iWantToEditThisAdministrator(AdminUserInterface|Proxy $adminUser): void
{
$this->updatePage->open(['id' => $adminUser->getId()]);
}
Expand Down Expand Up @@ -173,7 +174,7 @@ public function iDeleteThem(): void
/**
* @When /^I (?:|upload|update) the "([^"]+)" image as (my) avatar$/
*/
public function iUploadTheImageAsMyAvatar(string $avatar, AdminUserInterface $administrator): void
public function iUploadTheImageAsMyAvatar(string $avatar, AdminUserInterface|Proxy $administrator): void
{
$path = $this->updateAvatar($avatar, $administrator);

Expand Down Expand Up @@ -276,9 +277,15 @@ public function iShouldBeNotifiedThatItCannotBeDeleted(): void
/**
* @Then /^I should see the "([^"]*)" image as (my) avatar$/
*/
public function iShouldSeeTheImageAsMyAvatar(string $avatar, AdminUserInterface $administrator): void
public function iShouldSeeTheImageAsMyAvatar(string $avatar, AdminUserInterface|Proxy $administrator): void
{
$this->objectManager->refresh($administrator);
if ($administrator instanceof AdminUserInterface) {
$this->objectManager->refresh($administrator);
}

if ($administrator instanceof Proxy) {
$administrator->refresh();
}

Assert::same($this->sharedStorage->get($avatar), $administrator->getAvatar()->getPath());
}
Expand All @@ -291,9 +298,15 @@ public function iShouldSeeTheAvatarImageInTheTopBarNextToMyName(string $avatar):
Assert::true($this->topBarElement->hasAvatarInMainBar($avatar));
}

private function getPath(AdminUserInterface $administrator): string
private function getPath(AdminUserInterface|Proxy $administrator): string
{
$this->objectManager->refresh($administrator);
if ($administrator instanceof AdminUserInterface) {
$this->objectManager->refresh($administrator);
}

if ($administrator instanceof Proxy) {
$administrator->refresh();
}

$avatar = $administrator->getAvatar();
if (null === $avatar) {
Expand All @@ -303,7 +316,7 @@ private function getPath(AdminUserInterface $administrator): string
return $avatar->getPath() ?? '';
}

private function updateAvatar(string $avatar, AdminUserInterface $administrator): string
private function updateAvatar(string $avatar, AdminUserInterface|Proxy $administrator): string
{
$this->updatePage->attachAvatar($avatar);
$this->updatePage->saveChanges();
Expand Down
3 changes: 2 additions & 1 deletion src/Monofony/Pack/AdminPack/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"symfony/routing": "^5.3",
"symfony/templating": "^5.3",
"twig/twig": "^2.7",
"webmozart/assert": "^1.9"
"webmozart/assert": "^1.9",
"zenstruck/foundry": "^1.14"
},
"require-dev": {
"behat/behat": "^3.2",
Expand Down

This file was deleted.

37 changes: 0 additions & 37 deletions src/Monofony/Pack/CorePack/.recipe/config/sylius/fixtures.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// Install Sample data command is available on monofony/fixtures-plugin
if (class_exists(InstallSampleDataCommand::class)) {
$commandExecutor = new CommandExecutor($input, $output, $this->getApplication());
$commandExecutor->runCommand('app:install:sample-data', [], $output);
$commandExecutor->runCommand(InstallSampleDataCommand::getDefaultName(), [], $output);
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Command\Installer;

use App\Command\Helper\CommandsRunner;
use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -61,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$commands = [
'sylius:fixtures:load' => ['--no-interaction' => true],
(string) LoadDataFixturesDoctrineCommand::getDefaultName() => ['--no-interaction' => true],
];

$this->commandsRunner->run($commands, $input, $output, $this->getApplication());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Story\DefaultAdministratorsStory;
use App\Story\DefaultAppUsersStory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Persistence\ObjectManager;

class DefaultFixtures extends Fixture implements FixtureGroupInterface
{
public function load(ObjectManager $manager): void
{
DefaultAdministratorsStory::load();
DefaultAppUsersStory::load();
}

public static function getGroups(): array
{
return ['default'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Story\RandomAppUsersStory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Persistence\ObjectManager;

class FakeFixtures extends Fixture implements FixtureGroupInterface
{
public function load(ObjectManager $manager): void
{
RandomAppUsersStory::load();
}

public static function getGroups(): array
{
return ['fake'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace App\Factory;

use App\Entity\User\AdminUser;
use Monofony\Contracts\Core\Model\User\AdminUserInterface;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;

/**
* @extends ModelFactory<AdminUser>
*
* @method static AdminUser|Proxy createOne(array $attributes = [])
* @method static AdminUser[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static AdminUser|Proxy find(object|array|mixed $criteria)
* @method static AdminUser|Proxy findOrCreate(array $attributes)
* @method static AdminUser|Proxy first(string $sortedField = 'id')
* @method static AdminUser|Proxy last(string $sortedField = 'id')
* @method static AdminUser|Proxy random(array $attributes = [])
* @method static AdminUser|Proxy randomOrCreate(array $attributes = [])
* @method static AdminUser[]|Proxy[] all()
* @method static AdminUser[]|Proxy[] findBy(array $attributes)
* @method static AdminUser[]|Proxy[] randomSet(int $number, array $attributes = [])
* @method static AdminUser[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method AdminUser|Proxy create(array|callable $attributes = [])
*/
final class AdminUserFactory extends ModelFactory
{
protected function getDefaults(): array
{
return [
'email' => self::faker()->email(),
'username' => self::faker()->userName(),
'enabled' => true,
'password' => 'password',
'first_name' => self::faker()->firstName(),
'last_name' => self::faker()->lastName(),
];
}

protected function initialize(): self
{
return $this
->afterInstantiate(function (AdminUserInterface $adminUser) {
$adminUser->setPlainPassword($adminUser->getPassword());
$adminUser->setPassword(null);
})
;
}

protected static function getClass(): string
{
return AdminUser::class;
}
}

0 comments on commit 07a8b0b

Please sign in to comment.