Skip to content

Commit

Permalink
+ Added delete flow, + Ignore coverage from DataFixtures folder
Browse files Browse the repository at this point in the history
  • Loading branch information
kpicaza committed Feb 28, 2016
1 parent c1c8ced commit e103a87
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 24 deletions.
6 changes: 6 additions & 0 deletions app/Resources/views/joke/delete.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{# empty Twig template #}

<h1>Joke Delete Form</h1>
{{ form_start(form, { method: 'DELETE', action: path('app_jokes_delete_confirm', { 'id': joke.id } ) } ) }}
{{ form_rest(form) }}
{{ form_end(form) }}
5 changes: 5 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ fos_user:
registration:
confirmation:
enabled: true

sensio_framework_extra:
request:
converters: true
auto_convert: false
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<whitelist>
<directory>src</directory>
<exclude>
<directory>src/*Bundle/DataFixtures</directory>
<directory>src/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
Expand Down
103 changes: 91 additions & 12 deletions src/AppBundle/Controller/JokeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use AppBundle\Entity\Joke;

class JokeController extends Controller
{
Expand Down Expand Up @@ -65,13 +67,17 @@ public function createAction(Request $request)
/**
*
* @Route("/jokes/{id}/edit", name="app_jokes_edit")
* @ParamConverter("joke", class="AppBundle:Joke", options={
* "repository_method" = "findOneBy",
* "mapping": {
* "id": "id"
* },
* })
* @template("joke/edit.html.twig")
* @param integer $id
* @param Joke $joke
*/
public function editAction($id)
public function editAction(Joke $joke)
{
$joke = $this->get('app.joke_repository')->findOneBy(array('id' => $id));

$form = $this->createForm(\AppBundle\Form\Type\JokeFormType::class, $joke);

return [
Expand All @@ -83,13 +89,17 @@ public function editAction($id)
/**
*
* @Route("/jokes/{id}/update", name="app_jokes_update")
* @template("joke/edit.html.twig")
* @Method({"POST"})
*/
public function updateAction(Request $request, $id)
* @ParamConverter("joke", class="AppBundle:Joke", options={
* "repository_method" = "findOneBy",
* "mapping": {
* "id": "id"
* },
* })
* @template("joke/edit.html.twig")
*/
public function updateAction(Request $request, Joke $joke)
{
$joke = $this->get('app.joke_repository')->findOneBy(array('id' => $id));

$form = $this->createForm(\AppBundle\Form\Type\JokeFormType::class, $joke);
$form->handleRequest($request);

Expand All @@ -109,15 +119,84 @@ public function updateAction(Request $request, $id)
/**
*
* @Route("/jokes/{id}", name="app_jokes_show")
* @ParamConverter("joke", class="AppBundle:Joke", options={
* "repository_method" = "findOneBy",
* "mapping": {
* "id": "id"
* },
* })
* @template("joke/show.html.twig")
*/
public function showAction(Request $request, $id)
public function showAction(Request $request, Joke $joke)
{
$joke = $this->get('app.joke_repository')->findOneBy(array('id' => $id));
return [
'joke' => $joke
];
}

/**
*
* @Route("/jokes/{id}/delete", name="app_jokes_delete")
* @ParamConverter("joke", class="AppBundle:Joke", options={
* "repository_method" = "findOneBy",
* "mapping": {
* "id": "id"
* },
* })
* @template("joke/delete.html.twig")
* @param Joke $joke
*/
public function deleteController(Joke $joke)
{
return [
'joke' => $joke
'joke' => $joke,
'form' => $this->createDeleteForm($joke->getId())->createView()
];
}

/**
*
* @Route("/jokes/{id}/delete/confirm", name="app_jokes_delete_confirm")
* @Method({"DELETE"})
* @ParamConverter("joke", class="AppBundle:Joke", options={
* "repository_method" = "findOneBy",
* "mapping": {
* "id": "id"
* },
* })
* @param Request $request
* @param Joke $joke
* @return RedirectResponse
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function deleteConfirmController(Request $request, Joke $joke)
{
$form = $this->createDeleteForm($joke->getId());
$form->handleRequest($request);

if ($form->isValid()) {

$this->get('app.joke_repository')->remove($joke);

return $this->redirect($this->generateUrl('app_jokes'));
}

throw new \Symfony\Component\Form\Exception\InvalidConfigurationException('Invalid delete form submision.');
}

/**
*
* @param integer $id
* @return Form $form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('app_jokes_delete_confirm', array('id' => $id)))
->setMethod('DELETE')
->add('delete', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, array('label' => 'Delete'))
->getForm()
;
}

}
10 changes: 10 additions & 0 deletions src/AppBundle/Entity/JokeGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ public function update()
{
$this->_em->flush();
}

/**
*
* @param \AppBundle\Entity\Joke $joke
*/
public function remove(Joke $joke)
{
$this->_em->remove($joke);
$this->_em->flush();
}
}
17 changes: 13 additions & 4 deletions src/AppBundle/Entity/JokeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JokeRepository
* @var \AppBundle\Entity\JokeGateway
*/
private $gateway;

/**
* @var \AppBundle\Entity\JokeFactory
*/
Expand Down Expand Up @@ -83,11 +83,10 @@ public function findOneBy(array $criteria, array $orderBy = array())
public function insert(array $params)
{
$joke = Joke::fromArray($params);

$rawJoke = $this->gateway->insert($joke);

return $this->factory->makeOne($rawJoke);

}

/**
Expand All @@ -97,4 +96,14 @@ public function update()
{
$this->gateway->update();
}

/**
*
* @param \AppBundle\Entity\Joke $joke
*/
public function remove(Joke $joke)
{
$this->gateway->remove($joke);
}

}
99 changes: 91 additions & 8 deletions tests/AppBundle/Controller/JokeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ class JokeControllerTest extends WebTestCase
const JOKE = 'A bad joke';
const NEW_JOKE = 'My new Joke';

/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;

/**
* {@inheritDoc}
*/
protected function setUp()
{
self::bootKernel();

$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager();
}

public function testJokeIndex()
{
$client = $this->createClient();
Expand Down Expand Up @@ -68,7 +85,7 @@ public function testJokeCreateWithValidValues()

$this->assertTrue($client->getResponse()->isRedirect());
$client->followRedirect();

$this->assertContains(
self::NEW_JOKE, $client->getResponse()->getContent()
);
Expand All @@ -89,13 +106,12 @@ public function testJokeCreateWithInvalidValues()
$form['joke_form[_token]'] = 'sdafasdfasfdas';

$client->submit($form);

$this->assertTrue($client->getResponse()->isSuccessful(), 'Expect Succesfull HTTP request');
$this->assertEquals(1, $crawler->filter('#joke_form_joke')->count(), 'Expect Joke input');
$this->assertEquals(1, $crawler->filter('#joke_form_published')->count(), 'Expect publish input');
$this->assertEquals(1, $crawler->filter('#joke_form_submit')->count(), 'Expect submit button');
}


public function testJokeEdit()
{
Expand All @@ -109,7 +125,7 @@ public function testJokeEdit()
$this->assertEquals(1, $crawler->filter('#joke_form_published')->count(), 'Expect publish input');
$this->assertEquals(1, $crawler->filter('#joke_form_submit')->count(), 'Expect submit button');
}

public function testJokeUpdateWithValidValues()
{
$client = $this->createClient();
Expand All @@ -127,7 +143,7 @@ public function testJokeUpdateWithValidValues()

$this->assertTrue($client->getResponse()->isRedirect());
$client->followRedirect();

$this->assertContains(
self::JOKE, $client->getResponse()->getContent()
);
Expand All @@ -137,7 +153,7 @@ public function testJokeUpdateWithInvalidValues()
{
$client = $this->createClient();

$crawler = $client->request('GET', '/jokes/' . self::ID .'/edit');
$crawler = $client->request('GET', '/jokes/' . self::ID . '/edit');

$this->assertTrue($client->getResponse()->isSuccessful(), 'Expect Succesfull HTTP request');

Expand All @@ -148,11 +164,78 @@ public function testJokeUpdateWithInvalidValues()
$form['joke_form[_token]'] = 'sdafasdfasfdas';

$client->submit($form);

$this->assertTrue($client->getResponse()->isSuccessful(), 'Expect Succesfull HTTP request');
$this->assertEquals(1, $crawler->filter('#joke_form_joke')->count(), 'Expect Joke input');
$this->assertEquals(1, $crawler->filter('#joke_form_published')->count(), 'Expect publish input');
$this->assertEquals(1, $crawler->filter('#joke_form_submit')->count(), 'Expect submit button');
}


public function testJokeDelete()
{
$client = $this->createClient();

$joke = $this->createJoke();

$crawler = $client->request('GET', '/jokes/' . $joke->getId() . '/delete');

$this->assertTrue($client->getResponse()->isSuccessful(), 'Expect Succesfull HTTP request');
$this->assertEquals(1, $crawler->filter('form')->count(), 'Expect Joke delete form.');

$this->assertTrue($client->getResponse()->isSuccessful(), 'Expect Succesfull HTTP request');

$form = $crawler->selectButton('Delete')->form();

$client->submit($form);

$this->assertTrue($client->getResponse()->isRedirect());
$crawler = $client->followRedirect();

$this->assertEquals(5, $crawler->filter('.joke')->count(), 'Expect Right number of jokes');

$this->assertEquals(1, $crawler->filter('.joke:contains("' . self::JOKE . '")')->count(), 'Expected joke is in jokes');
}

public function testJokeDeleteWithInvalidParams()
{
$client = $this->createClient();

$joke = $this->createJoke();

$crawler = $client->request('GET', '/jokes/' . $joke->getId() . '/delete');

$this->assertTrue($client->getResponse()->isSuccessful(), 'Expect Succesfull HTTP request');
$this->assertEquals(1, $crawler->filter('form')->count(), 'Expect Joke delete form.');

$this->assertTrue($client->getResponse()->isSuccessful(), 'Expect Succesfull HTTP request');

$form = $crawler->selectButton('Delete')->form();
$form['form[_token]'] = 'sdafasdfasfdas';

$client->submit($form);

$this->assertTrue(true, $client->getResponse()->isServerError());

}

private function createJoke()
{
$joke = new \AppBundle\Entity\Joke('joke to be deleted', true);

$this->em->persist($joke);
$this->em->flush();

return $joke;
}

/**
* {@inheritDoc}
*/
protected function tearDown()
{
parent::tearDown();

$this->em->close();
}

}
7 changes: 7 additions & 0 deletions tests/AppBundle/Form/Type/JokeFormTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
class JokeFormTypeTest extends TypeTestCase
{

public function testFormOptions()
{
$formtype = new JokeFormType();

$this->assertEquals('app_joke_form', $formtype->getName());
}

public function testSubmitValidData()
{
$formData = array(
Expand Down

0 comments on commit e103a87

Please sign in to comment.