From 5843dfbd128bd7adf1cc1ab5d3c3a960c72b079c Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Mon, 12 Dec 2016 03:22:02 +0300 Subject: [PATCH] MainControllerTest now tests AJAX user autocompletion. --- .../Controller/MainController.php | 7 +++ .../Tests/Controller/MainControllerTest.php | 59 ++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php index 119c4bf..6811185 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php @@ -49,6 +49,13 @@ class MainController extends Controller ]); } + /** + * Returns user search autocomplete data in JSON + * + * @param $login + * + * @return JsonResponse + */ public function searchUserAjaxAction($login) { $em = $this->getDoctrine()->getManager(); diff --git a/src/Skobkin/Bundle/PointToolsBundle/Tests/Controller/MainControllerTest.php b/src/Skobkin/Bundle/PointToolsBundle/Tests/Controller/MainControllerTest.php index a506c4a..5158314 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Tests/Controller/MainControllerTest.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Tests/Controller/MainControllerTest.php @@ -52,7 +52,6 @@ class MainControllerTest extends WebTestCase public function testUserStats() { $client = static::createClient(); - $crawler = $client->request('GET', '/'); $userStatsBlock = $crawler->filter('.container.service-stats'); @@ -73,4 +72,62 @@ class MainControllerTest extends WebTestCase 'Zero service subscribers shows on the main page' ); } + + /** + * Tests AJAX user search autocomplete and returns JSON response string + * + * @return string + */ + public function testAjaxUserAutoComplete() + { + $client = static::createClient(); + $crawler = $client->request('GET', '/ajax/users/search/testuser'); + + $this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"'); + + return $client->getResponse()->getContent(); + } + + /** + * @depends testAjaxUserAutoComplete + * + * @param $json + */ + public function testAjaxUserAutoCompleteHasOptions($json) + { + $data = json_decode($json); + + $this->assertNotNull($data, 'JSON data successfully decoded and not empty'); + $this->assertTrue(is_array($data), 'JSON data is array'); + $this->assertGreaterThan(0, count($data), 'Array has at least one element'); + + return $data; + } + + /** + * @depends testAjaxUserAutoCompleteHasOptions + * + * @param array $users + */ + public function testAjaxUserAutoCompleteHasValidUserObjects(array $users) + { + foreach ($users as $key => $user) { + $this->assertTrue(array_key_exists('login', $user), sprintf('%d row of result has \'login\' field', $key)); + $this->assertTrue(array_key_exists('name', $user), sprintf('%d row of result has \'name\' field', $key)); + } + } + + public function testAjaxUserAutoCompleteForNonExistingUser() + { + $client = static::createClient(); + $crawler = $client->request('GET', '/ajax/users/search/aksdjhaskdjhqwhdgqkjwhdgkjah'); + + $this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"'); + + $data = json_decode($client->getResponse()->getContent()); + + $this->assertNotNull($data, 'JSON data successfully decoded and not empty'); + $this->assertTrue(is_array($data), 'JSON data is array'); + $this->assertEquals(0, count($data), 'Array has no elements'); + } }