Skip to content

Commit

Permalink
Functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysiekpiasecki committed Oct 8, 2016
1 parent db8152d commit 21b6a3c
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 55 deletions.
2 changes: 1 addition & 1 deletion app/Resources/views/user/sidebar.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<li>
<a><i class="fa fa-book"></i> Documentation <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="http://symfony.com/doc/current/index.html">Symfony</a></li>
<li><a class="symfony-docs" href="http://symfony.com/doc/current/index.html">Symfony</a></li>
</ul>
</li>
<li><a href="{{ path('app_gentelella', {page: 'index'}) }}"><i class="fa fa-search"></i> Template</a></li>
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
stopOnFailure="true"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>
<php>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
/**
* GentelellaController.
*/
class GentelellaController extends Controller
{
/**
* Render Gentelella page.
Expand Down Expand Up @@ -41,16 +44,4 @@ public function gentelellaAction(Request $request, $page)

return $this->render(sprintf('gentelella/%s.html.twig', $page));
}

/**
* @Route("/", name="app_dashboard")
*
* @param Request $request Request
*
* @return Response
*/
public function dashboardAction(Request $request)
{
return $this->render('user/pages/dashboard.html.twig', []);
}
}
31 changes: 31 additions & 0 deletions src/AppBundle/Controller/SidebarController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* SidebarController.
*/
class SidebarController extends Controller
{
/**
* @Route("/", name="app_dashboard")
*
* @param Request $request Request
*
* @return Response
*/
public function dashboardAction(Request $request)
{
return $this->render('user/pages/dashboard.html.twig', []);
}
}
2 changes: 1 addition & 1 deletion src/AppBundle/Controller/TopNavController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\HttpFoundation\Response;

/**
* Top navigation controller.
* TopNavController.
*/
class TopNavController extends Controller
{
Expand Down
92 changes: 92 additions & 0 deletions tests/AppBundle/Controller/SidebarControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* SidebarControllerTest.
*
* @coversDefaultClass \AppBundle\Controller\SidebarController
*/
class SidebarControllerTest extends WebTestCase
{
/**
* @Test
*/
public function testDashboard()
{
$authClient = (new \TestClient('test', 'test'))->auth();

$crawler = $authClient->request('GET', '/profile');
$this->assertSame('Profile', trim($crawler->filter('.title_left h3')->text()));

$link = $authClient->getCrawler()->filter('.site_title')->link();
$crawler = $authClient->click($link);
$this->assertSame('Network Activities Graph title sub-title', trim($crawler->filter('.right_col h3')->text()));
}

/**
* @Test
*/
public function testLogo()
{
$client = (new \TestClient('test', 'test'))->auth();

$link = $client->getCrawler()->selectLink('Dashboard')->link();
$crawler = $client->click($link);
$this->assertSame('Network Activities Graph title sub-title', trim($crawler->filter('.right_col h3')->text()));
}

/**
* @Test
*/
public function testGoTemplateReturnApp()
{
$client = (new \TestClient('test', 'test'))->auth();

// Go to the template
$link = $client->getCrawler()->selectLink('Template')->link();
$crawler = $client->click($link);

// Return to the application
$linkBack = $crawler->selectLink('Back to the application');
$this->assertSame('Back to the application', $linkBack->text());
$crawler = $client->click($linkBack->link());
$this->assertSame('Network Activities Graph title sub-title', trim($crawler->filter('.right_col h3')->text()));
}

/**
* @covers :: gentelellaAction
*/
public function testPageNotFound()
{
$client = (new \TestClient('test', 'test'));
$authClient = $client->auth();
$crawler = $authClient->request('GET', 'fake-page');
$this->assertSame('404', $crawler->filter('.error-number')->text());
$client->logout();
}

/**
* @Test
*/
public function testLogoutAction()
{
$client = new \TestClient('test', 'test');

$authClient = $client->auth();
$link = $authClient->getCrawler()->filter('div.sidebar-footer a')->last()->link();
$authClient->click($link);
$authClient->followRedirect();
$authClient->followRedirect(); // Logout makes 2 redirects
$crawler = $authClient->getCrawler();
$this->assertSame('Login Form', trim($crawler->filter('h1')->text()));
$client->logout();
}
}
28 changes: 24 additions & 4 deletions tests/AppBundle/Controller/TopNavControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,35 @@
/**
* TopNavControllerTest.
*
* @see TopNavController
* @coversDefaultClass \AppBundle\Controller\TopNavController
*/
class TopNavControllerTest extends WebTestCase
{
/**
* @Test
* @covers :: profileAction
*/
public function testProfile()
{
$client = new \WebTestClient();
$authClient = $client->logIn();
$authClient = (new \TestClient('test', 'test'))->auth();
$crawler = $authClient->request('GET', '/profile');
$this->assertEquals(200, $authClient->getResponse()->getStatusCode());
$this->assertSame('Profile', trim($crawler->filter('.title_left h3')->text()));
}

/**
* @Test
*/
public function testLogoutAction()
{
$client = new \TestClient('test', 'test');

$authClient = $client->auth();
$link = $authClient->getCrawler()->filter('a[href="/logout"]')->link();
$authClient->click($link);
$authClient->followRedirect();
$authClient->followRedirect(); // Logout makes 2 redirects
$crawler = $authClient->getCrawler();
$this->assertSame('Login Form', trim($crawler->filter('h1')->text()));
$client->logout();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,22 @@
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* DefaultControllerTest.
* Tests for dead links.
*
* @see DefaultController
*/
class DefaultControllerTest extends WebTestCase
class URLTest extends WebTestCase
{
/**
* @Test
* @dataProvider provideUrl
*
* @param string $url Relative page url
*/
public function testTemplatePages($url)
public function testResponseOK($url)
{
$client = new \WebTestClient('test', 'test');
$authClient = $client->logIn();
$crawler = $authClient->request('GET', $url);
$this->assertEquals(200, $authClient->getResponse()->getStatusCode());
}

public function testDashboard()
{
$client = new \WebTestClient('test', 'test');
$authClient = $client->logIn();
$crawler = $authClient->request('GET', '/');
$this->assertSame('Total Users', trim($crawler->filter('span.count_top')->text()));
$authClient = (new \TestClient('test', 'test'))->auth();
$authClient->request('GET', $url);
$this->assertEquals(200, $authClient->getResponse()->getStatusCode());
}

Expand All @@ -47,7 +38,7 @@ public function provideUrl()
['/'],
['/profile'],
['/gentelella'],
['/gentelella'],
['/gentelella/index'],
['/gentelella/calendar'],
['/gentelella/chartjs'],
['/gentelella/chartjs2'],
Expand Down
49 changes: 30 additions & 19 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
shell_exec('php bin/console fos:user:deactivate test');
});

class WebTestClient
class TestClient
{
private static $client;

private $username;
private $password;

/**
* WebTestClient constructor.
* TestClient constructor.
*
* @param $username
* @param $password
Expand All @@ -25,26 +27,35 @@ public function __construct($username = 'test', $password = 'test')
$this->password = $password;
}

public function logIn()
public function auth()
{
static $client = null;

if ($client === null) {
$kernel = new AppKernel('test', false);
$kernel->boot();
$client = $kernel->getContainer()->get('test.client');

$crawler = $client->request('GET', '/logout');
$crawler = $client->request('GET', '/login');

$form = $crawler->filter('form')->form(array(
'_username' => $this->username,
'_password' => $this->password,
));
$client->submit($form);
$client->followRedirect();
if (self::$client == null) {
self::$client = $this->client();
}

return self::$client;
}

private function client()
{
$kernel = new AppKernel('test', false);
$kernel->boot();
$client = $kernel->getContainer()->get('test.client');
$crawler = $client->request('GET', '/login');
$form = $crawler->filter('form')->form(array(
'_username' => $this->username,
'_password' => $this->password,
));
$client->submit($form);
$client->followRedirect();

return $client;
}

public function logout()
{
self::$client->request('GET', '/logout');
self::$client->followRedirect();
self::$client = null;
}
}

0 comments on commit 21b6a3c

Please sign in to comment.