Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fixture loading while increasing the number of fixtures #522

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
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Adjust Admin List Order
* Add __toString methods to Entities
* Improve fixture loading while increasing the number of fixtures

# 3.3.1 (2023.11.12)

Expand Down Expand Up @@ -60,7 +61,7 @@

# 2.7.17 (2021.12.30)

* Set creationTime and updatedTime in all entity constructors (Fixes #207)
* Set creationTime and updatedTime in all entity constructors (Fixes #207)
* Update to symfony 4.3.36
* Update dependencies

Expand Down Expand Up @@ -145,8 +146,8 @@
# 2.7.3 (2020.10.21)

* Improve OpenPGP key import filter:
- Keep UIDs with valid email address but without realname
- Drop UIDs with invalid email address that have the valid email
* Keep UIDs with valid email address but without realname
* Drop UIDs with invalid email address that have the valid email
address in realname

# 2.7.2 (2020.10.21)
Expand Down
9 changes: 9 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
ignore:
- "config"
- "features"
- "hugo"
- "public"
- "src/DataFixtures"
- "templates"
- "tests"
36 changes: 12 additions & 24 deletions src/DataFixtures/LoadAliasData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,14 @@

namespace App\DataFixtures;

use App\Entity\Domain;
use App\Entity\User;
use App\Factory\AliasFactory;
use App\Repository\DomainRepository;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadAliasData extends Fixture implements OrderedFixtureInterface, ContainerAwareInterface
class LoadAliasData extends Fixture implements OrderedFixtureInterface
{
private ContainerInterface $container;

/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null): void
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
Expand All @@ -32,23 +18,25 @@ public function load(ObjectManager $manager): void
$user = $manager->getRepository(User::class)->findByEmail('[email protected]');

for ($i = 1; $i < 5; ++$i) {
$alias = AliasFactory::create($user, 'alias'.$i);
$alias = AliasFactory::create($user, null);

$manager->persist($alias);
$manager->flush();
}

for ($i = 1; $i < 5; ++$i) {
$alias = AliasFactory::create($user, null);
$users = $manager->getRepository(User::class)->findAll();

for ($i = 1; $i < 500; ++$i) {
$alias = AliasFactory::create($users[random_int(0, count($users) - 1)], 'alias' . $i);

$manager->persist($alias);
$manager->flush();

if (($i % 100) === 0) {
$manager->flush();
}
}
}

private function getRepository(): DomainRepository
{
return $this->container->get('doctrine')->getRepository(Domain::class);
$manager->flush();
$manager->clear();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/DataFixtures/LoadDomainData.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public function load(ObjectManager $manager): void
$domain->setName($name);

$manager->persist($domain);
$manager->flush();
}

$manager->flush();
$manager->clear();
}

/**
Expand Down
39 changes: 23 additions & 16 deletions src/DataFixtures/LoadUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class LoadUserData extends Fixture implements OrderedFixtureInterface, Container
{
private const PASSWORD = 'password';

private string $passwordHash;

private array $users = [
['email' => '[email protected]', 'roles' => [Roles::ADMIN]],
['email' => '[email protected]', 'roles' => [Roles::USER]],
Expand All @@ -41,6 +43,11 @@ public function setContainer(ContainerInterface $container = null): void
*/
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setPlainPassword(self::PASSWORD);
$this->getPasswordUpdater()->updatePassword($user);
$this->passwordHash = $user->getPassword();

$this->loadStaticUsers($manager);
$this->loadRandomUsers($manager);
}
Expand All @@ -58,20 +65,13 @@ private function getPasswordUpdater(): PasswordUpdater
return $this->container->get(PasswordUpdater::class);
}

/**
* @param $domain
* @param $email
* @param $roles
*/
private function buildUser($domain, $email, $roles): User
private function buildUser(Domain $domain, string $email, array $roles): User
{
$user = new User();
$user->setDomain($domain);
$user->setEmail($email);
$user->setRoles($roles);
$user->setPlainPassword(self::PASSWORD);

$this->getPasswordUpdater()->updatePassword($user);
$user->setPassword($this->passwordHash);

return $user;
}
Expand All @@ -89,8 +89,10 @@ private function loadStaticUsers(ObjectManager $manager): void
$user = $this->buildUser($domain, $email, $roles);

$manager->persist($user);
$manager->flush();
}

$manager->flush();
$manager->clear();
}

/**
Expand All @@ -99,12 +101,11 @@ private function loadStaticUsers(ObjectManager $manager): void
private function loadRandomUsers(ObjectManager $manager): void
{
$domainRepository = $manager->getRepository(Domain::class);
$domain = $domainRepository->findOneBy(['name' => 'example.org']);
$roles = [Roles::USER];

for ($i = 0; $i < 500; ++$i) {
$email = sprintf('%[email protected]', uniqid('', true));
$splitted = explode('@', $email);
$roles = [Roles::USER];
$domain = $domainRepository->findOneBy(['name' => $splitted[1]]);
for ($i = 0; $i < 15000; ++$i) {
$email = sprintf('user-%d@%s', $i, $domain->getName());

$user = $this->buildUser($domain, $email, $roles);
$user->setCreationTime(new \DateTime(sprintf('-%s days', random_int(1, 25))));
Expand All @@ -118,7 +119,13 @@ private function loadRandomUsers(ObjectManager $manager): void
}

$manager->persist($user);
$manager->flush();

if (($i % 100) === 0) {
$manager->flush();
}
}

$manager->flush();
$manager->clear();
}
}
20 changes: 6 additions & 14 deletions src/DataFixtures/LoadVoucherData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,13 @@
namespace App\DataFixtures;

use App\Entity\User;
use App\Entity\Voucher;
use App\Factory\VoucherFactory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadVoucherData extends Fixture implements OrderedFixtureInterface, ContainerAwareInterface
class LoadVoucherData extends Fixture implements OrderedFixtureInterface
{
private ContainerInterface $container;

/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null): void
{
$this->container = $container;
}

/**
* {@inheritdoc}
*
Expand All @@ -46,6 +33,10 @@ public function load(ObjectManager $manager): void
}

$manager->persist($voucher);

if (($i % 100) === 0) {
$manager->flush();
}
}

// add redeemed voucher to a suspicious parent
Expand All @@ -58,6 +49,7 @@ public function load(ObjectManager $manager): void
$manager->persist($voucher);

$manager->flush();
$manager->clear();
}

/**
Expand Down
Loading