From f93a2e5e19c889c448af67e25a2859f0486efabd Mon Sep 17 00:00:00 2001 From: myrmidex Date: Tue, 2 Jun 2026 22:42:51 +0000 Subject: [PATCH] Add createdBy on subject --- migrations/Version20260602224032.php | 35 +++++++++++++ src/Controller/SubjectController.php | 1 + src/Entity/Subject.php | 16 ++++++ src/Factory/UserFactory.php | 55 ++++++++++++++++++++ tests/Integration/Subject/AddSubjectTest.php | 9 +++- 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 migrations/Version20260602224032.php create mode 100644 src/Factory/UserFactory.php diff --git a/migrations/Version20260602224032.php b/migrations/Version20260602224032.php new file mode 100644 index 0000000..94aaab4 --- /dev/null +++ b/migrations/Version20260602224032.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE subject ADD created_by_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE subject ADD CONSTRAINT FK_FBCE3E7AB03A8386 FOREIGN KEY (created_by_id) REFERENCES "user" (id) NOT DEFERRABLE'); + $this->addSql('CREATE INDEX IDX_FBCE3E7AB03A8386 ON subject (created_by_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE subject DROP CONSTRAINT FK_FBCE3E7AB03A8386'); + $this->addSql('DROP INDEX IDX_FBCE3E7AB03A8386'); + $this->addSql('ALTER TABLE subject DROP created_by_id'); + } +} diff --git a/src/Controller/SubjectController.php b/src/Controller/SubjectController.php index 90208a9..459b246 100644 --- a/src/Controller/SubjectController.php +++ b/src/Controller/SubjectController.php @@ -32,6 +32,7 @@ class SubjectController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $subject->setCreatedBy($this->getUser()); $em->persist($subject); $em->flush(); diff --git a/src/Entity/Subject.php b/src/Entity/Subject.php index 9184497..48167c6 100644 --- a/src/Entity/Subject.php +++ b/src/Entity/Subject.php @@ -20,6 +20,10 @@ class Subject #[ORM\Column(length: 255)] private ?string $name = null; + #[ORM\ManyToOne] + #[ORM\JoinColumn(nullable: true)] + private ?User $createdBy = null; + public function getId(): ?int { return $this->id; @@ -36,4 +40,16 @@ class Subject return $this; } + + public function getCreatedBy(): ?User + { + return $this->createdBy; + } + + public function setCreatedBy(?User $createdBy): static + { + $this->createdBy = $createdBy; + + return $this; + } } diff --git a/src/Factory/UserFactory.php b/src/Factory/UserFactory.php new file mode 100644 index 0000000..ff13e30 --- /dev/null +++ b/src/Factory/UserFactory.php @@ -0,0 +1,55 @@ + + */ +final class UserFactory extends PersistentObjectFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() {} + + #[\Override] + public static function class(): string + { + return User::class; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + #[\Override] + protected function defaults(): array|callable + { + return [ + 'email' => self::faker()->text(180), + 'password' => self::faker()->text(), + 'roles' => [], + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + #[\Override] + protected function initialize(): static + { + return $this + // ->afterInstantiate(function(User $user): void {}) + ; + } +} diff --git a/tests/Integration/Subject/AddSubjectTest.php b/tests/Integration/Subject/AddSubjectTest.php index 17a8de7..dc3b663 100644 --- a/tests/Integration/Subject/AddSubjectTest.php +++ b/tests/Integration/Subject/AddSubjectTest.php @@ -3,6 +3,7 @@ namespace App\Tests\Integration\Subject; use App\Entity\Restaurant; +use App\Factory\UserFactory; use App\Repository\SubjectRepository; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; @@ -11,8 +12,12 @@ class AddSubjectTest extends WebTestCase public function testAddingNewSubject(): void { $client = static::createClient(); - $crawler = $client->request('GET', '/'); + $user = UserFactory::createOne(); + + $client->loginUser($user); + + $client->request('GET', '/'); $client->submitForm('form_save', ['form[name]' => 'Riviera']); $this->assertResponseRedirects(); @@ -25,6 +30,8 @@ class AddSubjectTest extends WebTestCase $this->assertNotNull($subject); $this->assertInstanceOf(Restaurant::class, $subject); + $this->assertNotNull($subject->getCreatedBy()); + $this->assertSame($user->getId(), $subject->getCreatedBy()->getId()); $this->assertSelectorTextContains('ul li', 'Riviera'); }