Skip to content

Commit

Permalink
Fixed datafixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtronics committed Jan 21, 2025
1 parent 5fb8143 commit 59d152a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 28 deletions.
40 changes: 40 additions & 0 deletions src/DataFixtures/ConfirmerFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);


namespace App\DataFixtures;

use App\Entity\Confirmer;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class ConfirmerFixture extends Fixture
{
public const HHV_1 = '[email protected]';
public const TREASURER_1 = '[email protected]';
public const TREASURER_2 = '[email protected]';

public function load(ObjectManager $manager): void
{
$confirmer = new Confirmer();
$confirmer->setEmail('[email protected]');
$confirmer->setName('HHV 1');
$this->addReference(self::HHV_1, $confirmer);
$manager->persist($confirmer);

$confirmer = new Confirmer();
$confirmer->setEmail('[email protected]');
$confirmer->setName('Treasurer 1');
$this->addReference(self::TREASURER_1, $confirmer);
$manager->persist($confirmer);

$confirmer = new Confirmer();
$confirmer->setEmail('[email protected]');
$confirmer->setName('Treasurer 2');
$this->addReference(self::TREASURER_2, $confirmer);
$manager->persist($confirmer);

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

use App\Entity\BankAccount;
use App\Entity\Confirmer;
use App\Entity\Department;
use App\Entity\DepartmentTypes;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;

final class DepartmentFixture extends Fixture
final class DepartmentFixture extends Fixture implements DependentFixtureInterface
{
public const DEPARTMENT1_REFERENCE = 'department1';
public const DEPARTMENT2_REFERENCE = 'department2';
Expand All @@ -35,8 +37,9 @@ public function load(ObjectManager $manager): void
$department->setBlocked(true);
$department->setSkipBlockedValidationTokens(['token1', 'token2']);
$department->setContactEmails(['[email protected]', '[email protected]']);
$department->setEmailHhv(['[email protected]']);
$department->setEmailTreasurer(['[email protected]', '[email protected]']);
$department->getConfirmers()->add($this->getReference(ConfirmerFixture::HHV_1, Confirmer::class));
$department->getConfirmers()->add($this->getReference(ConfirmerFixture::TREASURER_1, Confirmer::class));
$department->getConfirmers()->add($this->getReference(ConfirmerFixture::TREASURER_2, Confirmer::class));
$this->addReference(self::DEPARTMENT2_REFERENCE, $department);
$manager->persist($department);

Expand All @@ -46,8 +49,9 @@ public function load(ObjectManager $manager): void
$department->setBankAccount($this->getReference(BankAccountFixture::BANK_ACCOUNT1_REFERENCE, BankAccount::class));
$department->setComment('Test');
$department->setContactEmails(['[email protected]', '[email protected]']);
$department->setEmailHhv(['[email protected]']);
$department->setEmailTreasurer(['[email protected]', '[email protected]']);
$department->getConfirmers()->add($this->getReference(ConfirmerFixture::HHV_1, Confirmer::class));
$department->getConfirmers()->add($this->getReference(ConfirmerFixture::TREASURER_1, Confirmer::class));
$department->getConfirmers()->add($this->getReference(ConfirmerFixture::TREASURER_2, Confirmer::class));
$this->addReference(self::DEPARTMENT3_REFERENCE, $department);
$manager->persist($department);

Expand All @@ -63,10 +67,17 @@ public function load(ObjectManager $manager): void
$department->setName('Department 5');
$department->setType(DepartmentTypes::SECTION);
$department->setBankAccount($this->getReference(BankAccountFixture::BANK_ACCOUNT3_REFERENCE, BankAccount::class));
$department->setEmailHhv(['[email protected]']);
$department->getConfirmers()->add($this->getReference(ConfirmerFixture::HHV_1, Confirmer::class));
$this->addReference(self::DEPARTMENT5_REFERENCE, $department);
$manager->persist($department);

$manager->flush();
}

public function getDependencies(): array
{
return [
ConfirmerFixture::class
];
}
}
38 changes: 33 additions & 5 deletions src/DataFixtures/PaymentOrderFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

namespace App\DataFixtures;

use App\Entity\ConfirmationToken;
use App\Entity\Confirmer;
use App\Entity\Department;
use App\Entity\PaymentOrder;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;

final class PaymentOrderFixture extends Fixture
final class PaymentOrderFixture extends Fixture implements DependentFixtureInterface
{
public function __construct()
{
}

public function getDependencies(): array
{
return [
DepartmentFixture::class,
ConfirmerFixture::class,
];
}

public function load(ObjectManager $manager): void
{
$payment_order = new PaymentOrder();
Expand All @@ -26,8 +37,21 @@ public function load(ObjectManager $manager): void
$payment_order->setDepartment($this->getReference(DepartmentFixture::DEPARTMENT3_REFERENCE, Department::class));
$payment_order->setAmount(12340);
$payment_order->setComment('Test');
$payment_order->setConfirm1Token(password_hash('token1', PASSWORD_DEFAULT));
$payment_order->setConfirm2Token(password_hash('token2', PASSWORD_DEFAULT));

$token1 = new ConfirmationToken(
confirmer: $this->getReference(ConfirmerFixture::HHV_1, Confirmer::class),
paymentOrder: $payment_order,
hashedToken: password_hash('token1', PASSWORD_DEFAULT)
);
$payment_order->addConfirmationToken($token1);

$token2 = new ConfirmationToken(
confirmer: $this->getReference(ConfirmerFixture::TREASURER_1, Confirmer::class),
paymentOrder: $payment_order,
hashedToken: password_hash('token2', PASSWORD_DEFAULT)
);
$payment_order->addConfirmationToken($token2);

$payment_order->getBankInfo()
->setAccountOwner('John Doe');
$payment_order->getBankInfo()
Expand All @@ -51,8 +75,12 @@ public function load(ObjectManager $manager): void
$payment_order->setDepartment($this->getReference(DepartmentFixture::DEPARTMENT2_REFERENCE, Department::class));
$payment_order->setAmount(12340);
$payment_order->setComment('Test');
$payment_order->setConfirm1Token(password_hash('token1', PASSWORD_DEFAULT));
$payment_order->setConfirm2Token(null);
$token1 = new ConfirmationToken(
confirmer: $this->getReference(ConfirmerFixture::HHV_1, Confirmer::class),
paymentOrder: $payment_order,
hashedToken: password_hash('token1', PASSWORD_DEFAULT)
);
$payment_order->addConfirmationToken($token1);
$payment_order->getBankInfo()
->setAccountOwner('John Doe');
$payment_order->getBankInfo()
Expand Down
23 changes: 7 additions & 16 deletions src/Doctrine/Purger/ResetAutoIncrementORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;

use function array_reverse;
use function assert;
use function count;
use function is_callable;
use function method_exists;
use function preg_match;
use Doctrine\ORM\Mapping\ManyToManyOwningSideMapping;


/**
* Class responsible for purging databases of data before reloading data fixtures.
Expand Down Expand Up @@ -160,7 +156,7 @@ public function purge(): void

foreach ($orderedTables as $tbl) {
// If we have a filter expression, check it and skip if necessary
if (! $emptyFilterExpression && ! preg_match($filterExpr, (string) $tbl)) {
if (in_array($tbl, $this->excluded)) {
continue;
}

Expand Down Expand Up @@ -301,21 +297,16 @@ private function getTableName(ClassMetadata $class, AbstractPlatform $platform):
return $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
}

/** @param ManyToManyOwningSideMapping|mixed[] $assoc */
private function getJoinTableName(
array $assoc,
$assoc,
ClassMetadata $class,
AbstractPlatform $platform
AbstractPlatform $platform,
): string {
if (isset($assoc['joinTable']['schema']) && ! method_exists($class, 'getSchemaName')) {
return $assoc['joinTable']['schema'] . '.' .
$this->em->getConfiguration()
->getQuoteStrategy()
->getJoinTableName($assoc, $class, $platform);
}

return $this->em->getConfiguration()->getQuoteStrategy()->getJoinTableName($assoc, $class, $platform);
}


private function getDeleteFromTableSQL(string $tableName, AbstractPlatform $platform): string
{
$tableIdentifier = new Identifier($tableName);
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/PaymentOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class PaymentOrder implements DBElementInterface, TimestampedElementInterface, \
/**
* @var Collection<ConfirmationToken> The confirmation tokens that can be used to confirm this payment order
*/
#[ORM\OneToMany(targetEntity: ConfirmationToken::class, mappedBy: 'paymentOrder', orphanRemoval: true)]
#[ORM\OneToMany(targetEntity: ConfirmationToken::class, mappedBy: 'paymentOrder', orphanRemoval: true, cascade: ['persist', 'remove'])]
private Collection $confirmationTokens;

public function __construct()
Expand Down

0 comments on commit 59d152a

Please sign in to comment.