Add post and species forms
continuous-integration/drone/push Build is passing Details

Squashed commit of the following:
Author: Hugo PRADIER <Hugo.PRADIER2@etu.uca.fr>
Author: bastien ollier <bastien.ollier@etu.uca.fr>
Author: clfreville2 <clement.freville2@etu.uca.fr>
Reviewed on #7
pull/10/head
Clément FRÉVILLE 3 weeks ago
parent 82a3f69fa4
commit 49d60871c9

@ -109,7 +109,7 @@
"symfony/browser-kit": "7.0.*",
"symfony/css-selector": "7.0.*",
"symfony/debug-bundle": "7.0.*",
"symfony/maker-bundle": "^1.0",
"symfony/maker-bundle": "^1.59",
"symfony/phpunit-bridge": "^7.0",
"symfony/stopwatch": "7.0.*",
"symfony/web-profiler-bundle": "7.0.*"

29
composer.lock generated

@ -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": "cce9cbfaf4a49449e6431a8515f9d9eb",
"content-hash": "9df70d112adfdfa4fbcde08b504d0bb9",
"packages": [
{
"name": "api-platform/core",
@ -3757,16 +3757,16 @@
},
{
"name": "symfony/form",
"version": "v7.0.7",
"version": "v7.0.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/form.git",
"reference": "b4df6a399a2b03782a0163807239db342659f54f"
"reference": "1d0128e2f7e80c346ec51fa4d1ce4fec0d435eeb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/form/zipball/b4df6a399a2b03782a0163807239db342659f54f",
"reference": "b4df6a399a2b03782a0163807239db342659f54f",
"url": "https://api.github.com/repos/symfony/form/zipball/1d0128e2f7e80c346ec51fa4d1ce4fec0d435eeb",
"reference": "1d0128e2f7e80c346ec51fa4d1ce4fec0d435eeb",
"shasum": ""
},
"require": {
@ -3833,7 +3833,7 @@
"description": "Allows to easily create, process and reuse HTML forms",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/form/tree/v7.0.7"
"source": "https://github.com/symfony/form/tree/v7.0.8"
},
"funding": [
{
@ -3849,7 +3849,7 @@
"type": "tidelift"
}
],
"time": "2024-04-28T11:44:19+00:00"
"time": "2024-05-31T14:55:39+00:00"
},
{
"name": "symfony/framework-bundle",
@ -7137,16 +7137,16 @@
},
{
"name": "symfony/validator",
"version": "v7.0.7",
"version": "v7.0.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/validator.git",
"reference": "ab4e75b9d23ba70e78480aecbe4d8da15adf10eb"
"reference": "23af65dff1f4dfee9aab3a0123a243e40fa3d9cf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/validator/zipball/ab4e75b9d23ba70e78480aecbe4d8da15adf10eb",
"reference": "ab4e75b9d23ba70e78480aecbe4d8da15adf10eb",
"url": "https://api.github.com/repos/symfony/validator/zipball/23af65dff1f4dfee9aab3a0123a243e40fa3d9cf",
"reference": "23af65dff1f4dfee9aab3a0123a243e40fa3d9cf",
"shasum": ""
},
"require": {
@ -7191,7 +7191,8 @@
"Symfony\\Component\\Validator\\": ""
},
"exclude-from-classmap": [
"/Tests/"
"/Tests/",
"/Resources/bin/"
]
},
"notification-url": "https://packagist.org/downloads/",
@ -7211,7 +7212,7 @@
"description": "Provides tools to validate values",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/validator/tree/v7.0.7"
"source": "https://github.com/symfony/validator/tree/v7.0.8"
},
"funding": [
{
@ -7227,7 +7228,7 @@
"type": "tidelift"
}
],
"time": "2024-04-28T11:44:19+00:00"
"time": "2024-06-02T15:49:03+00:00"
},
{
"name": "symfony/var-dumper",

@ -7,4 +7,3 @@ parameters:
level: 8
paths:
- src
- tests

@ -2,19 +2,85 @@
namespace App\Controller;
use App\Entity\Post;
use App\Form\PostType;
use App\Repository\PostRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class PostController extends AbstractController
{
#[Route('/', name: 'app_posts')]
#[Route('/post', name: 'app_post_index', methods: ['GET'])]
public function index(PostRepository $repository): Response
{
$posts = $repository->findAll();
return $this->render('post/index.html.twig', [
return $this->render('post/table.html.twig', [
'posts' => $posts,
]);
}
#[Route('/post/new', name: 'app_post_new', methods: ['GET', 'POST'])]
#[IsGranted('ROLE_USER')]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$post = new Post();
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($post);
$entityManager->flush();
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
}
return $this->render('post/new.html.twig', [
'post' => $post,
'form' => $form,
]);
}
#[Route('/post/{id}', name: 'app_post_show', methods: ['GET'])]
public function show(Post $post): Response
{
return $this->render('post/show.html.twig', [
'post' => $post,
]);
}
#[Route('/post/{id}/edit', name: 'app_post_edit', methods: ['GET', 'POST'])]
#[IsGranted('ROLE_USER')]
public function edit(Request $request, Post $post, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
}
return $this->render('post/edit.html.twig', [
'post' => $post,
'form' => $form,
]);
}
#[Route('/post/{id}', name: 'app_post_delete', methods: ['POST'])]
#[IsGranted('ROLE_USER')]
public function delete(Request $request, Post $post, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$post->getId(), (string) $request->getPayload()->get('_token'))) {
$entityManager->remove($post);
$entityManager->flush();
}
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
}
}

@ -34,7 +34,11 @@ class RegistrationController extends AbstractController
// do anything else you need here, like send an email
return $this->redirectToRoute('_profiler_home');
return $this->redirectToRoute('app_login');
}
if ($this->getUser()) {
return $this->redirectToRoute('app_posts');
}
return $this->render('registration/register.html.twig', [

@ -3,27 +3,83 @@
namespace App\Controller;
use App\Entity\Species;
use App\Form\SpeciesType;
use App\Repository\SpeciesRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[Route('/species')]
class SpeciesController extends AbstractController
{
#[Route('/species', name: 'app_species')]
public function index(SpeciesRepository $repository): Response
#[Route('/', name: 'app_species_index', methods: ['GET'])]
public function table(SpeciesRepository $speciesRepository): Response
{
$species = $repository->findAll();
return $this->render('species/index.html.twig', [
return $this->render('species/table.html.twig', [
'species' => $speciesRepository->findAll(),
]);
}
#[Route('/new', name: 'app_species_new', methods: ['GET', 'POST'])]
#[IsGranted('ROLE_USER')]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$species = new Species();
$form = $this->createForm(SpeciesType::class, $species);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($species);
$entityManager->flush();
return $this->redirectToRoute('app_species_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('species/new.html.twig', [
'species' => $species,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_species_show', methods: ['GET'])]
public function show(Species $species): Response
{
return $this->render('species/show.html.twig', [
'species' => $species,
]);
}
#[Route('/species/{id}', name: 'app_species_detail')]
public function detail(Species $specie): Response
#[Route('/{id}/edit', name: 'app_species_edit', methods: ['GET', 'POST'])]
#[IsGranted('ROLE_USER')]
public function edit(Request $request, Species $species, EntityManagerInterface $entityManager): Response
{
return $this->render('species/detail.html.twig', [
'specie' => $specie,
$form = $this->createForm(SpeciesType::class, $species);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_species_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('species/edit.html.twig', [
'species' => $species,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_species_delete', methods: ['POST'])]
#[IsGranted('ROLE_USER')]
public function delete(Request $request, Species $species, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$species->getId(), (string) $request->getPayload()->get('_token'))) {
$entityManager->remove($species);
$entityManager->flush();
}
return $this->redirectToRoute('app_species_index', [], Response::HTTP_SEE_OTHER);
}
}

@ -4,17 +4,29 @@ namespace App\DataFixtures;
use App\Entity\Post;
use App\Entity\Species;
use App\Entity\User;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class AppFixtures extends Fixture
{
public function __construct(
private readonly UserPasswordHasherInterface $passwordHasher
)
{
}
public function load(ObjectManager $manager): void
{
$user = (new User())->setEmail('test@test.fr');
$user->setPassword($this->passwordHasher->hashPassword($user, 'password'));
$manager->persist($user);
$faker = \Faker\Factory::create();
for ($i = 0; $i < 20; ++$i) {
$name = $faker->text();
$name = $faker->name();
$species = (new Species())
->setScientificName($name)
->setVernacularName($name)

@ -11,6 +11,7 @@ use App\Repository\PostRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: PostRepository::class)]
#[ORM\HasLifecycleCallbacks]
@ -30,6 +31,7 @@ class Post
#[ORM\Column]
#[Groups(['post:collection:read'])]
#[Assert\NotBlank]
private ?\DateTimeImmutable $foundDate = null;
#[ORM\Column]
@ -51,6 +53,7 @@ class Post
#[ORM\Column(type: Types::TEXT)]
#[Groups(['post:read'])]
#[Assert\NotBlank]
private ?string $commentary = null;
#[ORM\ManyToOne(inversedBy: 'posts')]

@ -9,6 +9,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: SpeciesRepository::class)]
#[ApiResource(
@ -26,14 +27,17 @@ class Species
#[ORM\Column(length: 255)]
#[Groups(['species:collection:read'])]
#[Assert\NotBlank]
private ?string $scientific_name = null;
#[ORM\Column(length: 255)]
#[Groups(['species:collection:read'])]
#[Assert\NotBlank]
private ?string $vernacular_name = null;
#[ORM\Column(length: 255)]
#[Groups(['species:collection:read'])]
#[Assert\NotBlank]
private ?string $region = null;
/**

@ -0,0 +1,38 @@
<?php
namespace App\Form;
use App\Entity\Post;
use App\Entity\Species;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('foundDate', null, [
'widget' => 'single_text',
])
->add('latitude')
->add('longitude')
->add('altitude')
->add('commentary')
->add('species', EntityType::class, [
'class' => Species::class,
'choice_label' => 'scientific_name',
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}

@ -0,0 +1,27 @@
<?php
namespace App\Form;
use App\Entity\Species;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SpeciesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('scientific_name')
->add('vernacular_name')
->add('region')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Species::class,
]);
}
}

@ -33,6 +33,16 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader
$this->getEntityManager()->flush();
}
public function findOneByEmail(string $email): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.email = :email')
->setParameter('email', $email)
->getQuery()
->getOneOrNullResult()
;
}
// /**
// * @return User[] Returns an array of User objects
// */

@ -0,0 +1,4 @@
<form method="post" action="{{ path('app_post_delete', {'id': post.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ post.id) }}">
<button class="btn">Delete</button>
</form>

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Post{% endblock %}
{% block body %}
<h1>Edit Post</h1>
{{ include('post/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_post_index') }}">back to list</a>
{{ include('post/_delete_form.html.twig') }}
{% endblock %}

@ -1,11 +0,0 @@
{% extends 'base.html.twig' %}
{% block title %}Posts!{% endblock %}
{% block body %}
{% for post in posts %}
<div class="card">
#{{ post.id }} trouvé le {{ post.foundDate | date("d/m/Y \\à H \\h") }}
</div>
{% endfor %}
{% endblock %}

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New Post{% endblock %}
{% block body %}
<h1>Create new Post</h1>
{{ include('post/_form.html.twig') }}
<a href="{{ path('app_post_index') }}">back to list</a>
{% endblock %}

@ -0,0 +1,46 @@
{% extends 'base.html.twig' %}
{% block title %}Post{% endblock %}
{% block body %}
<h1>Post</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ post.id }}</td>
</tr>
<tr>
<th>FoundDate</th>
<td>{{ post.foundDate ? post.foundDate|date('Y-m-d H:i:s') : '' }}</td>
</tr>
<tr>
<th>PublicationDate</th>
<td>{{ post.publicationDate ? post.publicationDate|date('Y-m-d H:i:s') : '' }}</td>
</tr>
<tr>
<th>Latitude</th>
<td>{{ post.latitude }}</td>
</tr>
<tr>
<th>Longitude</th>
<td>{{ post.longitude }}</td>
</tr>
<tr>
<th>Altitude</th>
<td>{{ post.altitude }}</td>
</tr>
<tr>
<th>Commentary</th>
<td>{{ post.commentary }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('app_post_index') }}">back to list</a>
<a href="{{ path('app_post_edit', {'id': post.id}) }}">edit</a>
{{ include('post/_delete_form.html.twig') }}
{% endblock %}

@ -0,0 +1,45 @@
{% extends 'base.html.twig' %}
{% block title %}Post index{% endblock %}
{% block body %}
<h1>Post index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>FoundDate</th>
<th>PublicationDate</th>
<th>Latitude</th>
<th>Longitude</th>
<th>Altitude</th>
<th>Commentary</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for post in posts %}
<tr>
<td>{{ post.id }}</td>
<td>{{ post.foundDate ? post.foundDate|date('Y-m-d H:i:s') : '' }}</td>
<td>{{ post.publicationDate ? post.publicationDate|date('Y-m-d H:i:s') : '' }}</td>
<td>{{ post.latitude }}</td>
<td>{{ post.longitude }}</td>
<td>{{ post.altitude }}</td>
<td>{{ post.commentary }}</td>
<td>
<a href="{{ path('app_post_show', {'id': post.id}) }}">show</a>
<a href="{{ path('app_post_edit', {'id': post.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="8">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_post_new') }}">Create new</a>
{% endblock %}

@ -0,0 +1,4 @@
<form method="post" action="{{ path('app_species_delete', {'id': species.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ species.id) }}">
<button class="btn">Delete</button>
</form>

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

@ -1,43 +0,0 @@
{% extends 'base.html.twig' %}
{% block title %}Herbarium - Détail de l'espèces{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>{{ specie.vernacularName }}</h1>
<p>
🔬 Nom Scientifique : {{ specie.scientificName }}<br/>
📍 Region : {{ specie.region }}
</p>
<div>
<h2>Posts :</h2>
{% for post in specie.posts %}
<div>
<dt>
<h3>
<a href="">
📅 {{ post.publicationDate | date }}
</a>
</h3>
</dt>
<dd>
📍 géolocalisation :<br/>
- Longitude : {{ post.longitude }}<br/>
- Latitude : {{ post.latitude }}<br/>
- Altitude : {{ post.altitude }}<br/><br/>
💬 Commentaire :<br/>
- {{ post.getCommentary }}
</dd>
</div>
{% endfor %}
</div>
</div>
{% endblock %}

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Species{% endblock %}
{% block body %}
<h1>Edit Species</h1>
{{ include('species/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_species_index') }}">back to list</a>
{{ include('species/_delete_form.html.twig') }}
{% endblock %}

@ -1,6 +1,6 @@
{% extends 'base.html.twig' %}
{% block title %}Herbarium - Espèces{% endblock %}
{% block title %}Species{% endblock %}
{% block body %}
<style>
@ -14,16 +14,17 @@
<dl>
{% for specie in species %}
<dt>
<a href={{ url('app_species') ~ '/' ~ specie.getId }}>
🌿 {{ specie.getVernacularName }}
<a href={{ path('app_species_show', {'id': specie.id}) }}>
🌿 {{ specie.vernacularName }}
</a>
</dt>
<dd >
🔬 Nom Scientifique : {{ specie.getScientificName }}<br/>
📍 Region : {{ specie.getRegion }}
🔬 Nom Scientifique : {{ specie.scientificName }}<br/>
📍 Region : {{ specie.region }}
</dd><br/>
{% endfor %}
</dl>
<a href="{{ path('app_species_new') }}">Create new</a>
</div>
{% endblock %}

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New Species{% endblock %}
{% block body %}
<h1>Create new Species</h1>
{{ include('species/_form.html.twig') }}
<a href="{{ path('app_species_index') }}">back to list</a>
{% endblock %}

@ -0,0 +1,36 @@
{% extends 'base.html.twig' %}
{% block title %}Species{% endblock %}
{% block body %}
<h1>{{ species.vernacularName }}</h1>
<p>
🔬 Nom Scientifique : {{ species.scientificName }}<br/>
📍 Region : {{ species.region }}
</p>
<div>
<h2>Posts :</h2>
{% for post in species.posts %}
<div>
<dt>
<h3>{{ post.publicationDate | date }}</h3>
</dt>
<dl>
<dt>📍Géolocalisation</dt>
<dd>{{ post.longitude }} - {{ post.latitude }}</dd>
<dt>📍Commentaire</dt>
<dd>{{ post.getCommentary }}</dd>
</dl>
</div>
{% endfor %}
<a href="{{ path('app_species_index') }}">back to list</a>
<a href="{{ path('app_species_edit', {'id': species.id}) }}">edit</a>
{{ include('species/_delete_form.html.twig') }}
</div>
{% endblock %}

@ -0,0 +1,39 @@
{% extends 'base.html.twig' %}
{% block title %}Species index{% endblock %}
{% block body %}
<h1>Species index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Scientific_name</th>
<th>Vernacular_name</th>
<th>Region</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for species in species %}
<tr>
<td>{{ species.id }}</td>
<td>{{ species.scientificName }}</td>
<td>{{ species.vernacularName }}</td>
<td>{{ species.region }}</td>
<td>
<a href="{{ path('app_species_show', {'id': species.id}) }}">show</a>
<a href="{{ path('app_species_edit', {'id': species.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="5">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_species_new') }}">Create new</a>
{% endblock %}

@ -0,0 +1,128 @@
<?php
namespace App\Test\Controller;
use App\Entity\Post;
use App\Entity\User;
use App\Repository\PostRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PostControllerTest extends WebTestCase
{
private KernelBrowser $client;
private EntityManagerInterface $manager;
private PostRepository $repository;
private string $path = '/post/';
protected function setUp(): void
{
$this->client = static::createClient();
/** @var EntityManagerInterface $manager */
$manager = static::getContainer()->get(EntityManagerInterface::class);
$this->manager = $manager;
$this->repository = $this->manager->getRepository(Post::class);
foreach ($this->repository->findAll() as $object) {
$this->manager->remove($object);
}
$this->manager->flush();
$userRepository = $this->manager->getRepository(User::class);
/** @var User $user */
$user = $userRepository->findOneByEmail('test@test.fr');
$this->client->loginUser($user);
$this->client->request('GET', sprintf('%snew', $this->path));
}
public function testIndex(): void
{
$crawler = $this->client->request('GET', '/post');
self::assertResponseStatusCodeSame(200);
self::assertPageTitleContains('Post index');
// Use the $crawler to perform additional assertions e.g.
// self::assertSame('Some text on the page', $crawler->filter('.p')->first());
}
public function testNew(): void
{
self::assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'post[foundDate]' => '2024-01-01 00:00:00',
'post[latitude]' => '45.0',
'post[longitude]' => '45.0',
'post[altitude]' => '500.0',
'post[commentary]' => 'Testing',
]);
self::assertResponseRedirects('/');
self::assertSame(1, $this->repository->count());
}
public function testShow(): void
{
$fixture = new Post();
$fixture->setFoundDate(new \DateTimeImmutable('2024-01-01 00:00:00'));
$fixture->setCommentary('Cool stuff');
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
self::assertResponseStatusCodeSame(200);
self::assertSelectorTextContains('h1', 'Post');
}
public function testEdit(): void
{
$fixture = new Post();
$fixture->setFoundDate(new \DateTimeImmutable('2024-01-01 00:00:00'));
$fixture->setCommentary('Cool stuff');
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s/edit', $this->path, $fixture->getId()));
$this->client->submitForm('Update', [
'post[foundDate]' => '2024-03-25 00:00:00',
'post[latitude]' => '90',
'post[longitude]' => '90',
'post[altitude]' => '200',
'post[commentary]' => 'Something New',
]);
self::assertResponseRedirects('/');
$fixture = $this->repository->findAll();
self::assertEquals(new \DateTimeImmutable('2024-03-25 00:00:00'), $fixture[0]->getFoundDate());
self::assertSame(90., $fixture[0]->getLatitude());
self::assertSame(90., $fixture[0]->getLongitude());
self::assertSame(200., $fixture[0]->getAltitude());
self::assertSame('Something New', $fixture[0]->getCommentary());
}
public function testRemove(): void
{
$fixture = new Post();
$fixture->setFoundDate(new \DateTimeImmutable('2024-01-01 00:00:00'));
$fixture->setCommentary('Cool stuff');
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
$this->client->submitForm('Delete');
self::assertResponseRedirects('/');
self::assertSame(0, $this->repository->count());
}
}

@ -0,0 +1,129 @@
<?php
namespace App\Test\Controller;
use App\Entity\Species;
use App\Entity\User;
use App\Repository\SpeciesRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SpeciesControllerTest extends WebTestCase
{
private KernelBrowser $client;
private EntityManagerInterface $manager;
private SpeciesRepository $repository;
private string $path = '/species/';
protected function setUp(): void
{
$this->client = static::createClient();
/** @var EntityManagerInterface $manager */
$manager = static::getContainer()->get(EntityManagerInterface::class);
$this->manager = $manager;
$this->repository = $this->manager->getRepository(Species::class);
foreach ($this->repository->findAll() as $object) {
$this->manager->remove($object);
}
$this->manager->flush();
$userRepository = $this->manager->getRepository(User::class);
/** @var User $user */
$user = $userRepository->findOneByEmail('test@test.fr');
$this->client->loginUser($user);
$this->client->request('GET', sprintf('%snew', $this->path));
}
public function testIndex(): void
{
$crawler = $this->client->request('GET', $this->path);
self::assertResponseStatusCodeSame(200);
self::assertPageTitleContains('Species index');
// Use the $crawler to perform additional assertions e.g.
// self::assertSame('Some text on the page', $crawler->filter('.p')->first());
}
public function testNew(): void
{
$this->client->request('GET', sprintf('%snew', $this->path));
self::assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'species[scientific_name]' => 'Testing',
'species[vernacular_name]' => 'Testing',
'species[region]' => 'Testing',
]);
self::assertResponseRedirects($this->path);
self::assertSame(1, $this->repository->count([]));
}
public function testShow(): void
{
$fixture = new Species();
$fixture->setScientificName('My Title');
$fixture->setVernacularName('My Title');
$fixture->setRegion('My Title');
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
self::assertResponseStatusCodeSame(200);
self::assertPageTitleContains('Species');
// Use assertions to check that the properties are properly displayed.
}
public function testEdit(): void
{
$fixture = new Species();
$fixture->setScientificName('Value');
$fixture->setVernacularName('Value');
$fixture->setRegion('Value');
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s/edit', $this->path, $fixture->getId()));
$this->client->submitForm('Update', [
'species[scientific_name]' => 'Something New',
'species[vernacular_name]' => 'Something New',
'species[region]' => 'Something New',
]);
self::assertResponseRedirects('/species/');
$fixture = $this->repository->findAll();
self::assertSame('Something New', $fixture[0]->getScientificName());
self::assertSame('Something New', $fixture[0]->getVernacularName());
self::assertSame('Something New', $fixture[0]->getRegion());
}
public function testRemove(): void
{
$fixture = new Species();
$fixture->setScientificName('Value');
$fixture->setVernacularName('Value');
$fixture->setRegion('Value');
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
$this->client->submitForm('Delete');
self::assertResponseRedirects('/species/');
self::assertSame(0, $this->repository->count([]));
}
}
Loading…
Cancel
Save