First take on unit tests.

Creates an event and compares the the input data with the database.

Ticket #28
This commit is contained in:
Tim Schumacher 2016-06-26 13:48:47 +02:00
parent fc138fb910
commit e44e7b6423
10 changed files with 1491 additions and 82 deletions

View file

@ -176,5 +176,5 @@
</div>
</div>
<input type="submit" class="ui button green" value="Speichern"/>
<input type="submit" class="ui button green" name="save" value="Speichern"/>
</form>

View file

@ -1,17 +0,0 @@
<?php
namespace Hackspace\Bundle\CalciferBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/hello/Fabien');
$this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0);
}
}

View file

@ -2,54 +2,80 @@
namespace Hackspace\Bundle\CalciferBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\ORM\EntityRepository;
use Hackspace\Bundle\CalciferBundle\Entity\Event;
use Liip\FunctionalTestBundle\Test\WebTestCase;
class EventControllerTest extends WebTestCase
{
/*
public function testCompleteScenario()
{
// Create a new client to browse the application
$client = static::createClient();
// Create a new entry in the database
$crawler = $client->request('GET', '//');
$this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET //");
$crawler = $client->click($crawler->selectLink('Create a new entry')->link());
private function initClient() {
$this->loadFixtures([]);
// Fill in the form and submit it
$form = $crawler->selectButton('Create')->form(array(
'hackspace_bundle_calciferbundle_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(
'hackspace_bundle_calciferbundle_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());
$client = static::makeClient();
return $client;
}
*/
public function testEmptyListing() {
$client = $this->initClient();
$crawler = $client->request('GET', '/');
$this->assertStatusCode(200, $client);
}
public function testPostEventForm()
{
$client = $this->initClient();
$url = $client->getContainer()->get('router')->generate('_new');
$crawler = $client->request('GET', $url);
$this->assertStatusCode(200, $client);
$form = $crawler->selectButton('save')->form();
$now = new \DateTime();
$now->setTime(0,0,0);
$dateformat = "Y-m-d H:i";
$startdate = clone $now;
$startdate->add(new \DateInterval("P1D"));
$enddate = clone $now;
$enddate->add(new \DateInterval("P1DT2H"));
$form['startdate'] = $startdate->format("Y-m-d H:i");
$form['enddate'] = $enddate->format("Y-m-d H:i");
$form['summary'] = "Testevent";
$form['url'] = "https://calcifer.datenknoten.me";
$form["location"] = "Krautspace";
$form["location_lat"] = 1;
$form["location_lon"] = 2;
$form["tags"] = "foo,bar,krautspace";
$form["description"] = "Testdescription";
$crawler = $client->submit($form);
$this->assertStatusCode(302, $client);
$target = $client->getResponse()->headers->get('location');
$slug = explode("/",$target)[2];
$this->assertGreaterThan(0,strlen($slug));
$em = $this->getContainer()->get('doctrine')->getManager();
/** @var EntityRepository $repo */
$repo = $em->getRepository('CalciferBundle:Event');
/** @var Event $entity */
$entity = $repo->findOneBy(['slug' => $slug]);
$this->assertInstanceOf('Hackspace\Bundle\CalciferBundle\Entity\Event', $entity);
$this->assertTrue($startdate == $entity->startdate, "Startdate equal");
$this->assertTrue($enddate == $entity->enddate, "Enddate equal");
$this->assertTrue($form["summary"]->getValue() == $entity->summary, "Summary equal");
$this->assertTrue($form["url"]->getValue() == $entity->url, "URL equal");
$this->assertTrue($form["description"]->getValue() == $entity->description, "Description equal");
}
}