From 1963855c64c57a06f4231299bc5add915ffc7d72 Mon Sep 17 00:00:00 2001 From: myrmidex Date: Tue, 2 Jun 2026 20:01:42 +0000 Subject: [PATCH] Add subject, show subjects --- composer.json | 1 + composer.lock | 71 ++++++++++++++++++- config/bundles.php | 1 + .../packages/dama_doctrine_test_bundle.yaml | 5 ++ config/reference.php | 7 ++ migrations/Version20260602195404.php | 34 +++++++++ phpunit.dist.xml | 1 + src/Controller/SubjectController.php | 42 ++++++++++- src/Entity/Restaurant.php | 11 +++ src/Entity/Subject.php | 39 ++++++++++ src/Repository/SubjectRepository.php | 43 +++++++++++ symfony.lock | 12 ++++ templates/index/page.html.twig | 8 +++ .../Controller/SubjectControllerTest.php | 16 ++++- 14 files changed, 285 insertions(+), 6 deletions(-) create mode 100644 config/packages/dama_doctrine_test_bundle.yaml create mode 100644 migrations/Version20260602195404.php create mode 100644 src/Entity/Restaurant.php create mode 100644 src/Entity/Subject.php create mode 100644 src/Repository/SubjectRepository.php diff --git a/composer.json b/composer.json index 34433b9..fdb5632 100644 --- a/composer.json +++ b/composer.json @@ -103,6 +103,7 @@ } }, "require-dev": { + "dama/doctrine-test-bundle": "^8.6", "phpstan/extension-installer": "^1.4", "phpstan/phpstan": "^2.2", "phpstan/phpstan-doctrine": "^2.0", diff --git a/composer.lock b/composer.lock index 33cc08a..bf4eb2b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6aa0596ff046b9df530a9c0757f8d54e", + "content-hash": "cd40b73a97f584ed609ab7a2dca91ce9", "packages": [ { "name": "composer/semver", @@ -7703,6 +7703,75 @@ } ], "packages-dev": [ + { + "name": "dama/doctrine-test-bundle", + "version": "v8.6.0", + "source": { + "type": "git", + "url": "https://github.com/dmaicher/doctrine-test-bundle.git", + "reference": "f7e3487643e685432f7e27c50cac64e9f8c515a4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dmaicher/doctrine-test-bundle/zipball/f7e3487643e685432f7e27c50cac64e9f8c515a4", + "reference": "f7e3487643e685432f7e27c50cac64e9f8c515a4", + "shasum": "" + }, + "require": { + "doctrine/dbal": "^3.3 || ^4.0", + "doctrine/doctrine-bundle": "^2.11.0 || ^3.0", + "php": ">= 8.2", + "psr/cache": "^2.0 || ^3.0", + "symfony/cache": "^6.4 || ^7.3 || ^8.0", + "symfony/framework-bundle": "^6.4 || ^7.3 || ^8.0" + }, + "conflict": { + "phpunit/phpunit": "<11.0" + }, + "require-dev": { + "behat/behat": "^3.0", + "friendsofphp/php-cs-fixer": "^3.27", + "phpstan/phpstan": "^2.0", + "phpunit/phpunit": "^11.5.41|| ^12.3.14", + "symfony/dotenv": "^6.4 || ^7.3 || ^8.0", + "symfony/process": "^6.4 || ^7.3 || ^8.0" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "8.x-dev" + } + }, + "autoload": { + "psr-4": { + "DAMA\\DoctrineTestBundle\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "David Maicher", + "email": "mail@dmaicher.de" + } + ], + "description": "Symfony bundle to isolate doctrine database tests and improve test performance", + "keywords": [ + "doctrine", + "isolation", + "performance", + "symfony", + "testing", + "tests" + ], + "support": { + "issues": "https://github.com/dmaicher/doctrine-test-bundle/issues", + "source": "https://github.com/dmaicher/doctrine-test-bundle/tree/v8.6.0" + }, + "time": "2026-01-21T07:39:44+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.13.4", diff --git a/config/bundles.php b/config/bundles.php index 4e3a560..be20bb7 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -13,4 +13,5 @@ return [ Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true], Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true], Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true], + DAMA\DoctrineTestBundle\DAMADoctrineTestBundle::class => ['test' => true], ]; diff --git a/config/packages/dama_doctrine_test_bundle.yaml b/config/packages/dama_doctrine_test_bundle.yaml new file mode 100644 index 0000000..3482cba --- /dev/null +++ b/config/packages/dama_doctrine_test_bundle.yaml @@ -0,0 +1,5 @@ +when@test: + dama_doctrine_test: + enable_static_connection: true + enable_static_meta_data_cache: true + enable_static_query_cache: true diff --git a/config/reference.php b/config/reference.php index e377270..82c752f 100644 --- a/config/reference.php +++ b/config/reference.php @@ -1474,6 +1474,12 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param; * generate_final_classes?: bool|Param, // Default: true * generate_final_entities?: bool|Param, // Default: false * } + * @psalm-type DamaDoctrineTestConfig = array{ + * enable_static_connection?: mixed, // Default: true + * enable_static_meta_data_cache?: bool|Param, // Default: true + * enable_static_query_cache?: bool|Param, // Default: true + * connection_keys?: list, + * } * @psalm-type ConfigType = array{ * imports?: ImportsConfig, * parameters?: ParametersConfig, @@ -1532,6 +1538,7 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param; * twig_extra?: TwigExtraConfig, * security?: SecurityConfig, * monolog?: MonologConfig, + * dama_doctrine_test?: DamaDoctrineTestConfig, * }, * ...addSql('CREATE TABLE subject (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY (id))'); + $this->addSql('CREATE TABLE messenger_messages (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, body TEXT NOT NULL, headers TEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, available_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, delivered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY (id))'); + $this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0E3BD61CE16BA31DBBF396750 ON messenger_messages (queue_name, available_at, delivered_at, id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE subject'); + $this->addSql('DROP TABLE messenger_messages'); + } +} diff --git a/phpunit.dist.xml b/phpunit.dist.xml index 22bd879..2686035 100644 --- a/phpunit.dist.xml +++ b/phpunit.dist.xml @@ -40,5 +40,6 @@ + diff --git a/src/Controller/SubjectController.php b/src/Controller/SubjectController.php index 8f155bb..90208a9 100644 --- a/src/Controller/SubjectController.php +++ b/src/Controller/SubjectController.php @@ -2,15 +2,53 @@ namespace App\Controller; +use App\Entity\Restaurant; +use App\Repository\SubjectRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class SubjectController extends AbstractController { #[Route('/')] - public function index(): Response + public function index( + Request $request, + EntityManagerInterface $em, + SubjectRepository $repo, + ): Response { + $subject = new Restaurant(); + $subject->setName("MyRestaurant"); + + $form = $this->createFormBuilder($subject) + ->add('name', TextType::class) + ->add('save', SubmitType::class) + ->getForm(); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em->persist($subject); + $em->flush(); + + return $this->redirectToRoute('app_subject_index'); + } + + $subjects = $repo->findAll(); + + return $this->render('index/page.html.twig', [ + 'form' => $form, + 'subjects' => $subjects, + ]); + } + + #[Route('/', 'subject_new', methods: ["POST"])] + public function save(): RedirectResponse { - return $this->render('index/page.html.twig'); + return $this->redirect('/'); } } diff --git a/src/Entity/Restaurant.php b/src/Entity/Restaurant.php new file mode 100644 index 0000000..d54ca17 --- /dev/null +++ b/src/Entity/Restaurant.php @@ -0,0 +1,11 @@ + Restaurant::class])] +class Subject +{ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column] + private ?int $id = null; + + #[ORM\Column(length: 255)] + private ?string $name = null; + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): static + { + $this->name = $name; + + return $this; + } +} diff --git a/src/Repository/SubjectRepository.php b/src/Repository/SubjectRepository.php new file mode 100644 index 0000000..3ed365b --- /dev/null +++ b/src/Repository/SubjectRepository.php @@ -0,0 +1,43 @@ + + */ +class SubjectRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Subject::class); + } + + // /** + // * @return Subject[] Returns an array of Subject objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('s') + // ->andWhere('s.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('s.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Subject + // { + // return $this->createQueryBuilder('s') + // ->andWhere('s.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/symfony.lock b/symfony.lock index d73642a..6a39fba 100644 --- a/symfony.lock +++ b/symfony.lock @@ -1,4 +1,16 @@ { + "dama/doctrine-test-bundle": { + "version": "8.6", + "recipe": { + "repo": "github.com/symfony/recipes-contrib", + "branch": "main", + "version": "8.3", + "ref": "dfc51177476fb39d014ed89944cde53dc3326d23" + }, + "files": [ + "config/packages/dama_doctrine_test_bundle.yaml" + ] + }, "doctrine/deprecations": { "version": "1.1", "recipe": { diff --git a/templates/index/page.html.twig b/templates/index/page.html.twig index 0a31e93..3b7a864 100644 --- a/templates/index/page.html.twig +++ b/templates/index/page.html.twig @@ -4,5 +4,13 @@

Rater

+ {{ form(form) }} + +

Existing

+
    + {% for subject in subjects %} +
  • {{ subject.name }}
  • + {% endfor %} +
diff --git a/tests/Integration/Controller/SubjectControllerTest.php b/tests/Integration/Controller/SubjectControllerTest.php index e05d145..11228fa 100644 --- a/tests/Integration/Controller/SubjectControllerTest.php +++ b/tests/Integration/Controller/SubjectControllerTest.php @@ -2,16 +2,26 @@ namespace App\Tests\Integration\Controller; +use App\Entity\Restaurant; +use App\Repository\SubjectRepository; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class SubjectControllerTest extends WebTestCase { - public function testSomething(): void + public function testAddingNewSubject(): void { $client = static::createClient(); $crawler = $client->request('GET', '/'); - $this->assertResponseIsSuccessful(); - $this->assertSelectorTextContains('h1', 'Rater'); + $client->submitForm('form_save', ['form[name]' => 'Riviera']); + + $this->assertResponseRedirects(); + + $subject = static::getContainer() + ->get(SubjectRepository::class) + ->findOneBy(['name' => 'Riviera']); + + $this->assertNotNull($subject); + $this->assertInstanceOf(Restaurant::class, $subject); } }