Bläddra i källkod

Merged in FD3-321 (pull request #1)

FD3-321
Guillermo Espinoza 7 år sedan
förälder
incheckning
eefef12eb2

+ 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}
      */

+ 197 - 0
src/IPv4Bundle/tests/Controller/REST/BaseRESTControllerTest.php

@@ -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.");
+    }
+
+}

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

@@ -0,0 +1,93 @@
+<?php
+
+namespace IPv4Bundle\tests\Controller\REST;
+
+use WebserviceBundle\tests\WebTestCaseBase;
+
+/**
+ * Class HostRESTControllerTest
+ * @package IPv4Bundle\tests\Controller\REST
+ * En caso de modificar las tablas solo se deberia modificar la funcion "obtainData" agregando los campos necesarios.
+ */
+class HostRESTControllerTest extends BaseRESTControllerTest
+{
+
+    /**
+    * @var string $entity
+    */
+    protected $entity = 'Host';
+
+    /**
+    * @var string $field
+    */
+    protected $field = 'mac';
+
+    /**
+    * @var string $field_value_edited
+    */
+    protected $field_value_edited = 'cafecafecaff';
+
+    /**
+     * @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);
+            }
+        }
+    }
+
+}

+ 93 - 0
src/IPv4Bundle/tests/Controller/REST/HostTypeRESTControllerTest.php

@@ -0,0 +1,93 @@
+<?php
+
+namespace IPv4Bundle\tests\Controller\REST;
+
+use WebserviceBundle\tests\WebTestCaseBase;
+
+/**
+ * Class HostRESTControllerTest
+ * @package IPv4Bundle\tests\Controller\REST
+ * En caso de modificar las tablas solo se deberia modificar la funcion "obtainData" agregando los campos necesarios.
+ */
+class HostTypeRESTControllerTest extends BaseRESTControllerTest
+{
+
+    /**
+    * @var string $entity
+    */
+    protected $entity = 'HostType';
+
+    /**
+    * @var string $field
+    */
+    protected $field = 'name';
+
+    /**
+    * @var string $field_value_edited
+    */
+    protected $field_value_edited = 'host_type';
+
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getUri()
+    {
+        return '/api/hosttypes.json';
+    }
+
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getPOSTUri()
+    {
+        return '/api/hosttypes.json';
+    }
+
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getUriPutDelete()
+    {
+        return '/api/hosttypes/';
+    }
+
+    /**
+     * 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 = [
+            'name' => 'HostType',
+            'shortname' => 'hosttype',
+            'opcode' => 1,
+        ];
+
+        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);
+            }
+        }
+    }
+
+}