浏览代码

FD3-294 Test OLTModelRESTController

Guillermo Espinoza 7 年之前
父节点
当前提交
9a82568853
共有 1 个文件被更改,包括 207 次插入0 次删除
  1. 207 0
      src/FTTHBundle/tests/Controller/REST/OLTModelRESTControllerTest.php

+ 207 - 0
src/FTTHBundle/tests/Controller/REST/OLTModelRESTControllerTest.php

@@ -0,0 +1,207 @@
+<?php
+
+namespace FTTHBundle\tests\Controller\REST;
+
+use WebserviceBundle\tests\WebTestCaseBase;
+use Symfony\Component\HttpFoundation\Response;
+
+/**
+ * @package FTTHBundle\tests\Controller\REST
+ * En caso de modificar las tablas solo se deberia modificar la funcion "obtainData" agregando los campos necesarios.
+ */
+class OLTModelRESTControllerTest extends WebTestCaseBase
+{
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getUri()
+    {
+        return '/api/oltmodels.json';
+    }
+
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getUriPutDelete()
+    {
+        return '/api/oltmodels/';
+    }
+
+    /**
+     * 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' => 'OLT Model',
+            'props' => 'Test',
+            'mark' => 'Test',
+            'library' => 'Test',
+            'extraData' => '',
+            'tenancyId' => '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);
+            }
+        }
+    }
+
+    /**
+     * @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();
+        }
+        // realizo la consulta
+        if ($data == null) {
+            $data = [   
+                'name' => $this->obtainData('name'), 
+                'tenancyId' => $this->obtainData('tenancyId'),
+            ];
+        }
+        $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);
+        //$this->setContainerObject('device.device_listener', $listener);
+        $validator = $this->getContainerObject('device.device_validator');
+        $validator->setWebservice($webservicemock);
+        //$this->setContainerObject('device.device_validator', $validator);
+    }
+
+    /**
+     * Realiza el alta.
+     */
+    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->getUri(), $this->obtainData());
+        // obtengo la respuesta
+        $response = $this->getClient()->getResponse();
+        $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
+    }
+
+    /**
+     * Realiza una busqueda.
+     */
+    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('name'), $response->getContent(), "Error al buscar");
+    }
+
+    /**
+     * Realiza una modificacion
+     */
+    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(), [   
+            'name' => 'OLT Model edit', 
+            'id' => $id,
+        ]);
+        // 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
+     */
+    public function testGET_PUT()
+    {
+        $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('OLT Model edit', $response->getContent(), "Error al buscar olt model modificado.");
+    }
+    
+    /**
+     * Realiza una baja
+     */
+    public function testDELETE()
+    {
+        // realizo la consulta
+        $response = $this->generateGET();
+        // obtengo el id de la respuesta de la busqueda
+        $id = $this->getProperty($response, 'id');
+        $this->initDefault();
+        // realizo la consulta
+        $data = [    
+            'tenancy' => 1,
+        ];
+        $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
+     */
+    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.");
+    }
+
+}