MainControllerTest now tests AJAX user autocompletion.

This commit is contained in:
Alexey Skobkin 2016-12-12 03:22:02 +03:00
parent d988bac7b0
commit 5843dfbd12
2 changed files with 65 additions and 1 deletions

View file

@ -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();

View file

@ -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');
}
}