From 4d02314770096ce492e23df83f1c4370275f69f1 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Tue, 22 Mar 2016 17:16:19 +0100 Subject: [PATCH] Event CRUD generated --- Controller/EventController.php | 224 +++++++++++++++++++++++ Form/EventType.php | 43 +++++ Resources/config/routing.yml | 4 + Resources/config/routing/event.yml | 30 +++ Resources/views/Event/edit.html.twig | 16 ++ Resources/views/Event/index.html.twig | 43 +++++ Resources/views/Event/new.html.twig | 15 ++ Resources/views/Event/show.html.twig | 36 ++++ Tests/Controller/EventControllerTest.php | 55 ++++++ 9 files changed, 466 insertions(+) create mode 100644 Controller/EventController.php create mode 100644 Form/EventType.php create mode 100644 Resources/config/routing/event.yml create mode 100644 Resources/views/Event/edit.html.twig create mode 100644 Resources/views/Event/index.html.twig create mode 100644 Resources/views/Event/new.html.twig create mode 100644 Resources/views/Event/show.html.twig create mode 100644 Tests/Controller/EventControllerTest.php diff --git a/Controller/EventController.php b/Controller/EventController.php new file mode 100644 index 000000000..b084218df --- /dev/null +++ b/Controller/EventController.php @@ -0,0 +1,224 @@ +getDoctrine()->getManager(); + + $entities = $em->getRepository('ChillEventBundle:Event')->findAll(); + + return $this->render('ChillEventBundle:Event:index.html.twig', array( + 'entities' => $entities, + )); + } + /** + * Creates a new Event entity. + * + */ + public function createAction(Request $request) + { + $entity = new Event(); + $form = $this->createCreateForm($entity); + $form->handleRequest($request); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($entity); + $em->flush(); + + return $this->redirect($this->generateUrl('event_show', array('id' => $entity->getId()))); + } + + return $this->render('ChillEventBundle:Event:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + )); + } + + /** + * Creates a form to create a Event entity. + * + * @param Event $entity The entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createCreateForm(Event $entity) + { + $form = $this->createForm(new EventType(), $entity, array( + 'action' => $this->generateUrl('event_create'), + 'method' => 'POST', + )); + + $form->add('submit', 'submit', array('label' => 'Create')); + + return $form; + } + + /** + * Displays a form to create a new Event entity. + * + */ + public function newAction() + { + $entity = new Event(); + $form = $this->createCreateForm($entity); + + return $this->render('ChillEventBundle:Event:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + )); + } + + /** + * Finds and displays a Event entity. + * + */ + public function showAction($id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('ChillEventBundle:Event')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find Event entity.'); + } + + $deleteForm = $this->createDeleteForm($id); + + return $this->render('ChillEventBundle:Event:show.html.twig', array( + 'entity' => $entity, + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Displays a form to edit an existing Event entity. + * + */ + public function editAction($id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('ChillEventBundle:Event')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find Event entity.'); + } + + $editForm = $this->createEditForm($entity); + $deleteForm = $this->createDeleteForm($id); + + return $this->render('ChillEventBundle:Event:edit.html.twig', array( + 'entity' => $entity, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Creates a form to edit a Event entity. + * + * @param Event $entity The entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createEditForm(Event $entity) + { + $form = $this->createForm(new EventType(), $entity, array( + 'action' => $this->generateUrl('event_update', array('id' => $entity->getId())), + 'method' => 'PUT', + )); + + $form->add('submit', 'submit', array('label' => 'Update')); + + return $form; + } + /** + * Edits an existing Event entity. + * + */ + public function updateAction(Request $request, $id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('ChillEventBundle:Event')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find Event entity.'); + } + + $deleteForm = $this->createDeleteForm($id); + $editForm = $this->createEditForm($entity); + $editForm->handleRequest($request); + + if ($editForm->isValid()) { + $em->flush(); + + return $this->redirect($this->generateUrl('event_edit', array('id' => $id))); + } + + return $this->render('ChillEventBundle:Event:edit.html.twig', array( + 'entity' => $entity, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + /** + * Deletes a Event entity. + * + */ + public function deleteAction(Request $request, $id) + { + $form = $this->createDeleteForm($id); + $form->handleRequest($request); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $entity = $em->getRepository('ChillEventBundle:Event')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find Event entity.'); + } + + $em->remove($entity); + $em->flush(); + } + + return $this->redirect($this->generateUrl('event')); + } + + /** + * Creates a form to delete a Event entity by id. + * + * @param mixed $id The entity id + * + * @return \Symfony\Component\Form\Form The form + */ + private function createDeleteForm($id) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('event_delete', array('id' => $id))) + ->setMethod('DELETE') + ->add('submit', 'submit', array('label' => 'Delete')) + ->getForm() + ; + } +} diff --git a/Form/EventType.php b/Form/EventType.php new file mode 100644 index 000000000..5434681a7 --- /dev/null +++ b/Form/EventType.php @@ -0,0 +1,43 @@ +add('label') + ->add('date') + ->add('center') + ->add('type') + ->add('circle') + ; + } + + /** + * @param OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'Chill\EventBundle\Entity\Event' + )); + } + + /** + * @return string + */ + public function getName() + { + return 'chill_eventbundle_event'; + } +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index f6262b049..83a6a4093 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -1,3 +1,7 @@ +chill_event_event: + resource: "@ChillEventBundle/Resources/config/routing/event.yml" + prefix: /event + chill_event_fr_admin_event_status: resource: "@ChillEventBundle/Resources/config/routing/status.yml" prefix: /{_locale}/admin/event/status diff --git a/Resources/config/routing/event.yml b/Resources/config/routing/event.yml new file mode 100644 index 000000000..cd3573f4e --- /dev/null +++ b/Resources/config/routing/event.yml @@ -0,0 +1,30 @@ +event: + path: / + defaults: { _controller: "ChillEventBundle:Event:index" } + +event_show: + path: /{id}/show + defaults: { _controller: "ChillEventBundle:Event:show" } + +event_new: + path: /new + defaults: { _controller: "ChillEventBundle:Event:new" } + +event_create: + path: /create + defaults: { _controller: "ChillEventBundle:Event:create" } + methods: POST + +event_edit: + path: /{id}/edit + defaults: { _controller: "ChillEventBundle:Event:edit" } + +event_update: + path: /{id}/update + defaults: { _controller: "ChillEventBundle:Event:update" } + methods: [POST, PUT] + +event_delete: + path: /{id}/delete + defaults: { _controller: "ChillEventBundle:Event:delete" } + methods: [POST, DELETE] diff --git a/Resources/views/Event/edit.html.twig b/Resources/views/Event/edit.html.twig new file mode 100644 index 000000000..733f7332c --- /dev/null +++ b/Resources/views/Event/edit.html.twig @@ -0,0 +1,16 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

Event edit

+ + {{ form(edit_form) }} + + +{% endblock %} diff --git a/Resources/views/Event/index.html.twig b/Resources/views/Event/index.html.twig new file mode 100644 index 000000000..9ba265dda --- /dev/null +++ b/Resources/views/Event/index.html.twig @@ -0,0 +1,43 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

Event list

+ + + + + + + + + + + + {% for entity in entities %} + + + + + + + {% endfor %} + +
IdLabelDateActions
{{ entity.id }}{{ entity.label }}{% if entity.date %}{{ entity.date|date('Y-m-d H:i:s') }}{% endif %} + +
+ + + {% endblock %} diff --git a/Resources/views/Event/new.html.twig b/Resources/views/Event/new.html.twig new file mode 100644 index 000000000..25e6d2661 --- /dev/null +++ b/Resources/views/Event/new.html.twig @@ -0,0 +1,15 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

Event creation

+ + {{ form(form) }} + + +{% endblock %} diff --git a/Resources/views/Event/show.html.twig b/Resources/views/Event/show.html.twig new file mode 100644 index 000000000..a2e9f85cb --- /dev/null +++ b/Resources/views/Event/show.html.twig @@ -0,0 +1,36 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

Event

+ + + + + + + + + + + + + + + + +
Id{{ entity.id }}
Label{{ entity.label }}
Date{{ entity.date|date('Y-m-d H:i:s') }}
+ + +{% endblock %} diff --git a/Tests/Controller/EventControllerTest.php b/Tests/Controller/EventControllerTest.php new file mode 100644 index 000000000..6435a2712 --- /dev/null +++ b/Tests/Controller/EventControllerTest.php @@ -0,0 +1,55 @@ +request('GET', '/event/'); + $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /event/"); + $crawler = $client->click($crawler->selectLink('Create a new entry')->link()); + + // Fill in the form and submit it + $form = $crawler->selectButton('Create')->form(array( + 'chill_eventbundle_event[field_name]' => 'Test', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check data in the show view + $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")'); + + // Edit the entity + $crawler = $client->click($crawler->selectLink('Edit')->link()); + + $form = $crawler->selectButton('Update')->form(array( + 'chill_eventbundle_event[field_name]' => 'Foo', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check the element contains an attribute with value equals "Foo" + $this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]'); + + // Delete the entity + $client->submit($crawler->selectButton('Delete')->form()); + $crawler = $client->followRedirect(); + + // Check the entity has been delete on the list + $this->assertNotRegExp('/Foo/', $client->getResponse()->getContent()); + } + + */ +}