diff --git a/app/Resources/views/user/sidebar.html.twig b/app/Resources/views/user/sidebar.html.twig index cbc3702..dbfebad 100644 --- a/app/Resources/views/user/sidebar.html.twig +++ b/app/Resources/views/user/sidebar.html.twig @@ -34,7 +34,7 @@
  • Documentation
  • Template
  • diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ab46010..7270d34 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -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" > diff --git a/src/AppBundle/Controller/DefaultController.php b/src/AppBundle/Controller/GentelellaController.php similarity index 80% rename from src/AppBundle/Controller/DefaultController.php rename to src/AppBundle/Controller/GentelellaController.php index ccb5738..99e900c 100644 --- a/src/AppBundle/Controller/DefaultController.php +++ b/src/AppBundle/Controller/GentelellaController.php @@ -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. @@ -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', []); - } } diff --git a/src/AppBundle/Controller/SidebarController.php b/src/AppBundle/Controller/SidebarController.php new file mode 100644 index 0000000..a10546e --- /dev/null +++ b/src/AppBundle/Controller/SidebarController.php @@ -0,0 +1,31 @@ +render('user/pages/dashboard.html.twig', []); + } +} diff --git a/src/AppBundle/Controller/TopNavController.php b/src/AppBundle/Controller/TopNavController.php index 0f2450a..0300614 100644 --- a/src/AppBundle/Controller/TopNavController.php +++ b/src/AppBundle/Controller/TopNavController.php @@ -13,7 +13,7 @@ use Symfony\Component\HttpFoundation\Response; /** - * Top navigation controller. + * TopNavController. */ class TopNavController extends Controller { diff --git a/tests/AppBundle/Controller/SidebarControllerTest.php b/tests/AppBundle/Controller/SidebarControllerTest.php new file mode 100644 index 0000000..e6b5929 --- /dev/null +++ b/tests/AppBundle/Controller/SidebarControllerTest.php @@ -0,0 +1,92 @@ +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(); + } +} diff --git a/tests/AppBundle/Controller/TopNavControllerTest.php b/tests/AppBundle/Controller/TopNavControllerTest.php index dc30e9f..0e948b0 100644 --- a/tests/AppBundle/Controller/TopNavControllerTest.php +++ b/tests/AppBundle/Controller/TopNavControllerTest.php @@ -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(); } } diff --git a/tests/AppBundle/Controller/DefaultControllerTest.php b/tests/AppBundle/URLTest.php similarity index 77% rename from tests/AppBundle/Controller/DefaultControllerTest.php rename to tests/AppBundle/URLTest.php index b3f0ddf..863b4d7 100644 --- a/tests/AppBundle/Controller/DefaultControllerTest.php +++ b/tests/AppBundle/URLTest.php @@ -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()); } @@ -47,7 +38,7 @@ public function provideUrl() ['/'], ['/profile'], ['/gentelella'], - ['/gentelella'], + ['/gentelella/index'], ['/gentelella/calendar'], ['/gentelella/chartjs'], ['/gentelella/chartjs2'], diff --git a/tests/bootstrap.php b/tests/bootstrap.php index acc4942..3e724ba 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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 @@ -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; + } }