Skip to content

Instantly share code, notes, and snippets.

@clubdesarrolladores
Last active August 29, 2015 14:13
Show Gist options
  • Save clubdesarrolladores/d733d6aa4b5d70210489 to your computer and use it in GitHub Desktop.
Save clubdesarrolladores/d733d6aa4b5d70210489 to your computer and use it in GitHub Desktop.
pruebas rest
<?php
namespace Replagal\Bundle\SectionBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Class RESTControllerTest
* @package Replagal\Bundle\SectionBundle\Tests\Controller
*/
class RESTControllerTest extends WebTestCase
{
/**
* @param $response
* @param int $statusCode
*/
protected function assertJsonResponse($response, $statusCode = 200)
{
$this->assertEquals(
$statusCode, $response->getStatusCode(),
$response->getContent()
);
$this->assertTrue(
$response->headers->contains('Content-Type', 'application/json'),
$response->headers
);
}
/**
*
*/
public function testValidAuthAction()
{
$client = static::createClient();
$crawler = $client->request('POST', '/api/auth.json', ['username' => 'enabled']);
$response = $client->getResponse();
$this->assertJsonResponse($response, 200);
$result = json_decode($response->getContent(), true);
$expected = [
'status' => true,
'errors' => [],
'data' => []
];
$this->assertEquals($expected, $result);
}
/**
*
*/
public function testInvalidAuthAction()
{
$client = static::createClient();
$crawler = $client->request('POST', '/api/auth.json');
$response = $client->getResponse();
$this->assertJsonResponse($response, 200);
$result = json_decode($response->getContent(), true);
$expected = [
'status' => false,
'errors' => [
['type' => 'irrecoverable', 'identity' => null, 'message' => 'User not found']
],
'data' => []
];
$this->assertEquals($expected, $result);
}
/**
*
*/
public function testInvalidAuthAction2()
{
$client = static::createClient();
$crawler = $client->request('POST', '/api/auth.json', ['username' => 'inexistent']);
$response = $client->getResponse();
$this->assertJsonResponse($response, 200);
$result = json_decode($response->getContent(), true);
$expected = [
'status' => false,
'errors' => [
['type' => 'irrecoverable', 'identity' => 'inexistent', 'message' => 'User not found']
],
'data' => []
];
$this->assertEquals($expected, $result);
}
/**
*
*/
public function testInvalidAuthAction3()
{
$client = static::createClient();
$crawler = $client->request('POST', '/api/auth.json', ['username' => 'disabled']);
$response = $client->getResponse();
$this->assertJsonResponse($response, 200);
$result = json_decode($response->getContent(), true);
$expected = [
'status' => false,
'errors' => [
['type' => 'irrecoverable', 'identity' => 'disabled', 'message' => 'User not enabled']
],
'data' => []
];
$this->assertEquals($expected, $result);
}
/**
*
*/
public function testTrackViewAction()
{
$client = static::createClient();
$crawler = $client->request('POST', '/api/track/view.json', ['username' => 'enabled']);
$response = $client->getResponse();
$this->assertJsonResponse($response, 200);
$result = json_decode($response->getContent(), true);
$expected = [
'status' => false,
'errors' => [
['type' => 'recoverable', 'identity' => null, 'message' => 'Empty tracks']
],
'data' => []
];
$this->assertEquals($expected, $result);
}
/**
*
*/
public function testTrackViewAction2()
{
$tracks = [
[
'number' => '2.2',
'datetime' => '2015-01-14 17:45:43'
],
[
'number' => '2.3',
'datetime' => '2015-01-14 17:45:43'
]
];
$client = static::createClient();
$crawler = $client->request('POST', '/api/track/view.json', ['username' => 'enabled', 'tracks' => $tracks]);
$response = $client->getResponse();
$this->assertJsonResponse($response, 200);
$result = json_decode($response->getContent(), true);
$expected = [
'status' => true,
'errors' => [],
'data' => []
];
$this->assertEquals($expected, $result);
}
/**
*
*/
public function testTrackViewAction3()
{
$tracks = [
[
'number' => '2.2',
'datetime' => '2015-01-14 17:45:43'
],
[
'number' => 'invalid number',
'datetime' => '2015-01-14 17:45:43'
]
];
$client = static::createClient();
$crawler = $client->request('POST', '/api/track/view.json', ['username' => 'enabled', 'tracks' => $tracks]);
$response = $client->getResponse();
$this->assertJsonResponse($response, 200);
$result = json_decode($response->getContent(), true);
$expected = [
'status' => false,
'errors' => [
['type' => 'irrecoverable', 'identity' => 'invalid number', 'message' => 'The section dont exists']
],
'data' => []
];
$this->assertEquals($expected, $result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment