Guillermo Espinoza 7 лет назад
Родитель
Сommit
566bb860d6
2 измененных файлов с 250 добавлено и 0 удалено
  1. 11 0
      app/config/config_test.yml
  2. 239 0
      src/CablemodemBundle/tests/CMTSRESTControllerTest.php

+ 11 - 0
app/config/config_test.yml

@@ -8,6 +8,8 @@ framework:
         storage_id: session.storage.mock_file
     profiler:
         collect: false
+    csrf_protection:
+        enabled: false
 
 web_profiler:
     toolbar: false
@@ -15,3 +17,12 @@ web_profiler:
 
 swiftmailer:
     disable_delivery: true
+
+services:
+    backtrace_formatter:
+      class:  Monolog\Formatter\LineFormatter
+      calls:
+        - [ includeStacktraces ]
+    webservice:
+      class: WebserviceBundle\Services\WebserviceMock
+      arguments: ["@service_container", null, null, null]

+ 239 - 0
src/CablemodemBundle/tests/CMTSRESTControllerTest.php

@@ -0,0 +1,239 @@
+<?php
+
+namespace CablemodemBundle\tests;
+
+use WebserviceBundle\tests\WebTestCaseBase;
+
+/**
+ * Class CMTSRESTControllerTest
+ * @package CablemodemBundle\tests
+ * En caso de modificar las tablas solo se deberia modificar la funcion "obtainData" agregando los campos necesarios.
+ */
+class CMTSRESTControllerTest extends WebTestCaseBase
+{
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getUri()
+    {
+        return '/api/cmtss.json';
+    }
+    
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getPOSTUri()
+    {
+        return '/api/cmts.json';
+    }
+
+    /**
+     * @return string Retorna la uri a consultar.
+     */
+    protected function getUriPutDelete()
+    {
+        return '/api/cmtss/';
+    }
+
+    /**
+     * 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' => 'Test',
+            'host' => '127.0.0.1',
+            'snmpComunity' => 'test',
+            'snmpVersion' => 1,
+            'executeSnmp' => true,
+            'extraData' => '',
+            'tenancyId' => '1',
+            'modelId' => '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);
+            }
+        }
+    }
+
+    /**
+     * Realiza una busqueda.
+     * get_cmtss -> /api/cmtss.{_format}
+     * controller: CablemodemBundle:CMTSREST: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 = [
+                '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);
+        
+        $validator = $this->getContainerObject('device.device_validator');
+        $validator->setWebservice($webservicemock);
+    }
+
+    /**
+     * Realiza el alta.
+     * post_cmtss -> /api/cmtss.{_format}
+     * controller: CablemodemBundle:CMTSREST: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_cmtss -> /api/cmtss.{_format}
+     * controller: CablemodemBundle:CMTSREST: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('name'), $response->getContent(), "Error al buscar CMTS.");
+    }
+
+    /**
+     * Realiza una modificacion.
+     * put_cmtss -> /api/cmtss.{_format}
+     * controller: CablemodemBundle:CMTSREST: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(), [
+            'name' => 'Test test', 
+            '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.
+     * get_cmtss -> /api/cmtss.{_format}
+     * controller: CablemodemBundle:CMTSREST:cget
+     * Method: GET
+     */
+    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('Test test', $response->getContent(), "Error al buscar CMTS modificado.");
+    }
+    
+    /**
+     * Realiza una baja.
+     * delete_cmtss -> /api/cmtss.{_format}
+     * controller: CablemodemBundle:CMTSREST:delete
+     * Method: DELETE
+     */
+    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.
+     * get_cmtss -> /api/cmtss.{_format}
+     * controller: CablemodemBundle:CMTSREST: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.");
+    }   
+    
+}