Compare commits
2 commits
cc213bfb15
...
f93a2e5e19
| Author | SHA1 | Date | |
|---|---|---|---|
| f93a2e5e19 | |||
| 63638c2236 |
6 changed files with 135 additions and 13 deletions
35
migrations/Version20260602224032.php
Normal file
35
migrations/Version20260602224032.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DoctrineMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version20260602224032 extends AbstractMigration
|
||||||
|
{
|
||||||
|
public function getDescription(): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this up() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -32,6 +32,7 @@ class SubjectController extends AbstractController
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$subject->setCreatedBy($this->getUser());
|
||||||
$em->persist($subject);
|
$em->persist($subject);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,10 @@ class Subject
|
||||||
#[ORM\Column(length: 255)]
|
#[ORM\Column(length: 255)]
|
||||||
private ?string $name = null;
|
private ?string $name = null;
|
||||||
|
|
||||||
|
#[ORM\ManyToOne]
|
||||||
|
#[ORM\JoinColumn(nullable: true)]
|
||||||
|
private ?User $createdBy = null;
|
||||||
|
|
||||||
public function getId(): ?int
|
public function getId(): ?int
|
||||||
{
|
{
|
||||||
return $this->id;
|
return $this->id;
|
||||||
|
|
@ -36,4 +40,16 @@ class Subject
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCreatedBy(): ?User
|
||||||
|
{
|
||||||
|
return $this->createdBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setCreatedBy(?User $createdBy): static
|
||||||
|
{
|
||||||
|
$this->createdBy = $createdBy;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
55
src/Factory/UserFactory.php
Normal file
55
src/Factory/UserFactory.php
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Factory;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
use App\Repository\UserRepository;
|
||||||
|
use Doctrine\ORM\EntityRepository;
|
||||||
|
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
|
||||||
|
use Zenstruck\Foundry\Persistence\Proxy;
|
||||||
|
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends PersistentObjectFactory<User>
|
||||||
|
*/
|
||||||
|
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 {})
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,28 +3,21 @@
|
||||||
namespace App\Tests\Integration\Subject;
|
namespace App\Tests\Integration\Subject;
|
||||||
|
|
||||||
use App\Entity\Restaurant;
|
use App\Entity\Restaurant;
|
||||||
use App\Factory\RestaurantFactory;
|
use App\Factory\UserFactory;
|
||||||
use App\Repository\SubjectRepository;
|
use App\Repository\SubjectRepository;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
class AddSubjectTest extends WebTestCase
|
class AddSubjectTest extends WebTestCase
|
||||||
{
|
{
|
||||||
public function testSubjectList(): void
|
|
||||||
{
|
|
||||||
$client = static::createClient();
|
|
||||||
|
|
||||||
$subject = RestaurantFactory::createOne();
|
|
||||||
|
|
||||||
$client->request('GET', '/');
|
|
||||||
|
|
||||||
$this->assertSelectorTextContains('ul li', $subject->getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testAddingNewSubject(): void
|
public function testAddingNewSubject(): void
|
||||||
{
|
{
|
||||||
$client = static::createClient();
|
$client = static::createClient();
|
||||||
$crawler = $client->request('GET', '/');
|
|
||||||
|
|
||||||
|
$user = UserFactory::createOne();
|
||||||
|
|
||||||
|
$client->loginUser($user);
|
||||||
|
|
||||||
|
$client->request('GET', '/');
|
||||||
$client->submitForm('form_save', ['form[name]' => 'Riviera']);
|
$client->submitForm('form_save', ['form[name]' => 'Riviera']);
|
||||||
|
|
||||||
$this->assertResponseRedirects();
|
$this->assertResponseRedirects();
|
||||||
|
|
@ -37,6 +30,8 @@ class AddSubjectTest extends WebTestCase
|
||||||
|
|
||||||
$this->assertNotNull($subject);
|
$this->assertNotNull($subject);
|
||||||
$this->assertInstanceOf(Restaurant::class, $subject);
|
$this->assertInstanceOf(Restaurant::class, $subject);
|
||||||
|
$this->assertNotNull($subject->getCreatedBy());
|
||||||
|
$this->assertSame($user->getId(), $subject->getCreatedBy()->getId());
|
||||||
|
|
||||||
$this->assertSelectorTextContains('ul li', 'Riviera');
|
$this->assertSelectorTextContains('ul li', 'Riviera');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
tests/Integration/Subject/ListSubjectsTest.php
Normal file
20
tests/Integration/Subject/ListSubjectsTest.php
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tests\Integration\Subject;
|
||||||
|
|
||||||
|
use App\Factory\RestaurantFactory;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
|
class ListSubjectsTest extends WebTestCase
|
||||||
|
{
|
||||||
|
public function testSubjectList(): void
|
||||||
|
{
|
||||||
|
$client = static::createClient();
|
||||||
|
|
||||||
|
$subject = RestaurantFactory::createOne();
|
||||||
|
|
||||||
|
$client->request('GET', '/');
|
||||||
|
|
||||||
|
$this->assertSelectorTextContains('ul li', $subject->getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue