浏览代码

FD3-321 HostRESTControllerTest

Guillermo Espinoza 7 年之前
父节点
当前提交
cc53aa1937

+ 0 - 1
app/config/config.yml

@@ -35,7 +35,6 @@ framework:
         resource: '%kernel.project_dir%/app/config/routing.yml'
         strict_requirements: ~
     form: ~
-    csrf_protection: ~
     validation: { enable_annotations: true }
     #serializer: { enable_annotations: true }
     templating:

+ 7 - 2
src/IPv4Bundle/Form/HostType.php

@@ -13,9 +13,14 @@ class HostType extends AbstractType
      */
     public function buildForm(FormBuilderInterface $builder, array $options)
     {
-        $builder->add('mac')->add('options')->add('hostType');
+        $builder
+            ->add('mac')
+            ->add('options')
+            ->add('hostType', null, [
+                'required' => false
+            ]);
     }
-    
+
     /**
      * {@inheritdoc}
      */

+ 237 - 0
src/IPv4Bundle/tests/Controller/REST/HostRESTControllerTest.php

@@ -0,0 +1,237 @@
+<?php
+
+namespace IPv4Bundle\tests\Controller\REST;
+
+use WebserviceBundle\tests\WebTestCaseBase;
+
+/**
+ * Class HostRESTControllerTest
+ * @package IPv4Bundle\tests
+ * En caso de modificar las tablas solo se deberia modificar la funcion "obtainData" agregando los campos necesarios.
+ */
+class HostRESTControllerTest extends WebTestCaseBase
+{
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getUri()
+    {
+        return '/api/hosts.json';
+    }
+
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getPOSTUri()
+    {
+        return '/api/hosts.json';
+    }
+
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getUriPutDelete()
+    {
+        return '/api/hosts/';
+    }
+
+    /**
+     * Contiene los datos que debe retornar el webservice. La key es nombre del webservice y el
+     * value son los datos a retornar.
+     */
+    protected function obtainDataWebService()
+    {
+        $datos = [];
+
+        return $datos;
+    }
+
+    /**
+     * 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.
+     */
+    protected function obtainData($key = null)
+    {
+        $datos = [
+            'mac' => 'cafecafecafe',
+            'options' => 'cafecafecafe',
+            'hostType' => null,
+        ];
+
+        if ($key == null) {
+            return $datos;
+        } else {
+            if (isset($datos[$key])) {
+                return $datos[$key];
+            } else {
+                throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
+            }
+        }
+    }
+
+    /**
+     * Realiza una busqueda.
+     * get_hosts -> /api/hosts.{_format}
+     * controller: IPv4Bundle:HostREST:cget
+     * 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 = [
+                'mac' => $this->obtainData('mac'),
+            ];
+        }
+        $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.
+     * post_hosts -> /api/hosts.{_format}
+     * controller: IPv4Bundle:HostREST:post
+     * 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.
+     * get_hosts -> /api/hosts.{_format}
+     * controller: IPv4Bundle:HostREST:cget
+     * 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('mac'), $response->getContent(), "Error al buscar Host.");
+    }
+
+    /**
+     * Realiza una modificacion.
+     * put_hosts -> /api/hosts.{_format}
+     * controller: IPv4Bundle:HostREST:put
+     * 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(), [
+            'mac' => 'cafecafecaff',
+        ]);
+        // 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.
+     * get_hosts -> /api/hosts.{_format}
+     * controller: IPv4Bundle:HostREST:cget
+     * Method: GET
+     */
+    public function testGET_PUT()
+    {
+        $response = $this->generateGET(null, [
+            'mac' => 'cafecafecaff',
+        ]);
+
+        // 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('cafecafecaff', $response->getContent(), "Error al buscar Host modificado.");
+    }
+
+    /**
+     * Realiza una baja.
+     * delete_hosts -> /api/hosts.{_format}
+     * controller: IPv4Bundle:HostREST:delete
+     * Method: DELETE
+     */
+    public function testDELETE()
+    {
+        $data = [
+            'mac' => 'cafecafecaff',
+        ];
+        // 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.
+     * get_hosts -> /api/hosts.{_format}
+     * controller: IPv4Bundle:HostREST:cget
+     * 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.");
+    }
+
+}