|
@@ -0,0 +1,197 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace IPv4Bundle\tests\Controller\REST;
|
|
|
+
|
|
|
+use WebserviceBundle\tests\WebTestCaseBase;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Class BaseRESTControllerTest
|
|
|
+ *
|
|
|
+ * @package IPv4Bundle\tests\Controller\REST
|
|
|
+ */
|
|
|
+abstract class BaseRESTControllerTest extends WebTestCaseBase
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return string Retorna la uri a consultar.
|
|
|
+ */
|
|
|
+ abstract protected function getUri();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return string Retorna la uri a consultar.
|
|
|
+ */
|
|
|
+ abstract protected function getPOSTUri();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return string Retorna la uri a consultar.
|
|
|
+ */
|
|
|
+ abstract protected function getUriPutDelete();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Contiene los datos que debe retornar el webservice. La key es nombre del webservice y el
|
|
|
+ * value son los datos a retornar.
|
|
|
+ */
|
|
|
+ abstract protected function obtainDataWebService();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Genera los datos a manipular.
|
|
|
+ *
|
|
|
+ * @param string $key Contiene la key a buscar en los datos.
|
|
|
+ *
|
|
|
+ * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
|
|
|
+ *
|
|
|
+ * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
|
|
|
+ */
|
|
|
+ abstract protected function obtainData($key = null);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Realiza una busqueda.
|
|
|
+ * Method: GET
|
|
|
+ *
|
|
|
+ * @param string $uri Contiene la direccion.
|
|
|
+ * @param array $data Contiene los filtros a utilizar en la busqueda.
|
|
|
+ *
|
|
|
+ * @return null|Response Retorna el response.
|
|
|
+ */
|
|
|
+ private function generateGET($uri = null, $data = null)
|
|
|
+ {
|
|
|
+ $this->initDefault();
|
|
|
+ if ($uri == null) {
|
|
|
+ $uri = $this->getUri();
|
|
|
+ }
|
|
|
+ if ($data == null) {
|
|
|
+ $data = [
|
|
|
+ $this->field => $this->obtainData($this->field),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $this->getClient()->request('GET', $uri . $this->generateFilters($data));
|
|
|
+
|
|
|
+ return $this->getClient()->getResponse();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sobreescribe el device.device_listener
|
|
|
+ */
|
|
|
+ private function setListener()
|
|
|
+ {
|
|
|
+ $webservicemock = $this->getContainerObject('webservice');
|
|
|
+ $listener = $this->getContainerObject('device.device_listener');
|
|
|
+ $listener->setWebservice($webservicemock);
|
|
|
+
|
|
|
+ $validator = $this->getContainerObject('device.device_validator');
|
|
|
+ $validator->setWebservice($webservicemock);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Realiza el alta.
|
|
|
+ * Method: POST
|
|
|
+ */
|
|
|
+ public function testPOST()
|
|
|
+ {
|
|
|
+ // inicializo con los datos del webservicemock
|
|
|
+ $this->initDefault($this->obtainDataWebService());
|
|
|
+ // seteo los datos del listener
|
|
|
+ $this->setListener();
|
|
|
+ // hago la inserccion llamando al servicio por post
|
|
|
+ $this->getClient()->request('POST', $this->getPOSTUri(), $this->obtainData());
|
|
|
+ // obtengo la respuesta
|
|
|
+ $response = $this->getClient()->getResponse();
|
|
|
+
|
|
|
+ $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Realiza una busqueda.
|
|
|
+ * Method: GET
|
|
|
+ */
|
|
|
+ public function testGET_POST()
|
|
|
+ {
|
|
|
+ // obtengo la respuesta
|
|
|
+ $response = $this->generateGET();
|
|
|
+ // verifco el resultado
|
|
|
+ $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
|
|
|
+ $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
|
|
|
+ $this->assertContains($this->obtainData($this->field), $response->getContent(), "Error al buscar {$this->entity}.");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Realiza una modificacion.
|
|
|
+ * Method: PUT
|
|
|
+ */
|
|
|
+ public function testPUT()
|
|
|
+ {
|
|
|
+ // realizo la consulta
|
|
|
+ $response = $this->generateGET();
|
|
|
+ // busco el id
|
|
|
+ $id = $this->getProperty($response, 'id');
|
|
|
+ // inicializo con los datos del webservicemock
|
|
|
+ $this->initDefault($this->obtainDataWebService());
|
|
|
+ // seteo los datos del listener
|
|
|
+ $this->setListener();
|
|
|
+ // creo el nuevo set de datos a enviar.
|
|
|
+ $data = $this->obtainDataChange(
|
|
|
+ $this->obtainData(), [
|
|
|
+ $this->field => $this->field_value_edited,
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ // hago la modificacion llamando al servicio por put
|
|
|
+ $this->getClient()->request('PUT', $this->getUriPutDelete() . $id, $data);
|
|
|
+ // obtengo la respuesta
|
|
|
+ $response = $this->getClient()->getResponse();
|
|
|
+
|
|
|
+ $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Realiza una busqueda.
|
|
|
+ * Method: GET
|
|
|
+ */
|
|
|
+ public function testGET_PUT()
|
|
|
+ {
|
|
|
+ $response = $this->generateGET(
|
|
|
+ null, [
|
|
|
+ $this->field => $this->field_value_edited,
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ // verifco el resultado
|
|
|
+ $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
|
|
|
+ $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
|
|
|
+ $this->assertContains($this->field_value_edited, $response->getContent(), "Error al buscar {$this->entity} modificado.");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Realiza una baja.
|
|
|
+ * Method: DELETE
|
|
|
+ */
|
|
|
+ public function testDELETE()
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ $this->field => $this->field_value_edited,
|
|
|
+ ];
|
|
|
+ // realizo la consulta
|
|
|
+ $response = $this->generateGET(null, $data);
|
|
|
+ // obtengo el id de la respuesta de la busqueda
|
|
|
+ $id = $this->getProperty($response, 'id');
|
|
|
+ $this->initDefault();
|
|
|
+ // realizo la consulta
|
|
|
+ $this->getClient()->request('DELETE', $this->getUriPutDelete() . $id, $data);
|
|
|
+ // obtengo la respuesta
|
|
|
+ $response = $this->getClient()->getResponse();
|
|
|
+
|
|
|
+ $this->assertEquals(204, $response->getStatusCode(), "Error en la respuesta http.");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Realiza una busqueda.
|
|
|
+ * Method: GET
|
|
|
+ */
|
|
|
+ public function testGET_DELETE()
|
|
|
+ {
|
|
|
+ $response = $this->generateGET();
|
|
|
+ // verifco el resultado
|
|
|
+ $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
|
|
|
+ $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|