Commit 4fe6eb59875dc0cb87dc75abe3bd8dfe448c1dd8

Authored by Eduardo Santos
2 parents 0b2d1169 8c6e168c
Exists in master and in 1 other branch 3.1

Inclusão de estrutura para isolamento dos teste em banco separado e alterações n…

…o getUpdate para o novo agente.
app/AppKernel.php
... ... @@ -41,6 +41,10 @@ class AppKernel extends Kernel
41 41 $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
42 42 }
43 43  
  44 + if (in_array($this->getEnvironment(), array('test'))) {
  45 + $bundles[] = new Liip\FunctionalTestBundle\LiipFunctionalTestBundle();
  46 + }
  47 +
44 48 return $bundles;
45 49 }
46 50  
... ...
app/config/config_test.yml
... ... @@ -4,11 +4,23 @@ imports:
4 4 framework:
5 5 test: ~
6 6 session:
7   - storage_id: session.storage.mock_file
  7 + storage_id: session.storage.filesystem
  8 +
  9 +doctrine:
  10 + dbal:
  11 + default_connection: default
  12 + connections:
  13 + default:
  14 + driver: pdo_sqlite
  15 + path: %kernel.cache_dir%/test.db
  16 + charset: UTF8
  17 +
  18 +liip_functional_test:
  19 + cache_sqlite_db: true
8 20  
9 21 web_profiler:
10 22 toolbar: false
11 23 intercept_redirects: false
12 24  
13 25 swiftmailer:
14 26 - disable_delivery: true
  27 + disable_delivery: true
15 28 \ No newline at end of file
... ...
composer.json
... ... @@ -60,7 +60,8 @@
60 60 "ddeboer/data-import-bundle": "dev-master",
61 61 "twbs/bootstrap": "3.0.*",
62 62 "doctrine/migrations": "dev-master",
63   - "doctrine/doctrine-migrations-bundle": "dev-master"
  63 + "doctrine/doctrine-migrations-bundle": "dev-master",
  64 + "liip/functional-test-bundle": "~1.0"
64 65 },
65 66 "scripts": {
66 67 "post-install-cmd": [
... ...
composer.lock
... ... @@ -4,7 +4,7 @@
4 4 "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 5 "This file is @generated automatically"
6 6 ],
7   - "hash": "c5bdcf452360fdac257f1b3f2477a9f1",
  7 + "hash": "2bfd0da0469386a6510b8353b8fd1ce5",
8 8 "packages": [
9 9 {
10 10 "name": "braincrafted/bootstrap-bundle",
... ... @@ -2322,6 +2322,63 @@
2322 2322 "time": "2014-09-22 12:29:23"
2323 2323 },
2324 2324 {
  2325 + "name": "liip/functional-test-bundle",
  2326 + "version": "1.0.2",
  2327 + "target-dir": "Liip/FunctionalTestBundle",
  2328 + "source": {
  2329 + "type": "git",
  2330 + "url": "https://github.com/liip/LiipFunctionalTestBundle.git",
  2331 + "reference": "e665bb4fca6ca449e225db569db671348a2579e4"
  2332 + },
  2333 + "dist": {
  2334 + "type": "zip",
  2335 + "url": "https://api.github.com/repos/liip/LiipFunctionalTestBundle/zipball/e665bb4fca6ca449e225db569db671348a2579e4",
  2336 + "reference": "e665bb4fca6ca449e225db569db671348a2579e4",
  2337 + "shasum": ""
  2338 + },
  2339 + "require": {
  2340 + "doctrine/common": "2.*",
  2341 + "php": ">=5.3.2",
  2342 + "symfony/browser-kit": "~2.0",
  2343 + "symfony/framework-bundle": "~2.0"
  2344 + },
  2345 + "suggest": {
  2346 + "doctrine/dbal": "Required when using the fixture loading functionality with an ORM and SQLite",
  2347 + "doctrine/doctrine-fixtures-bundle": "Required when using the fixture loading functionality",
  2348 + "doctrine/orm": "Required when using the fixture loading functionality with an ORM and SQLite"
  2349 + },
  2350 + "type": "symfony-bundle",
  2351 + "extra": {
  2352 + "branch-alias": {
  2353 + "dev-master": "1.0.x-dev"
  2354 + }
  2355 + },
  2356 + "autoload": {
  2357 + "psr-0": {
  2358 + "Liip\\FunctionalTestBundle": ""
  2359 + }
  2360 + },
  2361 + "notification-url": "https://packagist.org/downloads/",
  2362 + "license": [
  2363 + "MIT"
  2364 + ],
  2365 + "authors": [
  2366 + {
  2367 + "name": "Liip AG",
  2368 + "homepage": "http://www.liip.ch/"
  2369 + },
  2370 + {
  2371 + "name": "Community contributions",
  2372 + "homepage": "https://github.com/liip/LiipFunctionalTestBundle/contributors"
  2373 + }
  2374 + ],
  2375 + "description": "This bundles provides additional functional test-cases for Symfony2 applications",
  2376 + "keywords": [
  2377 + "Symfony2"
  2378 + ],
  2379 + "time": "2014-06-27 14:00:35"
  2380 + },
  2381 + {
2325 2382 "name": "monolog/monolog",
2326 2383 "version": "1.10.0",
2327 2384 "source": {
... ...
src/Cacic/CommonBundle/DataFixtures/ORM/LoadUsuarioData.php
... ... @@ -40,6 +40,7 @@ class LoadUsuarioData extends AbstractFixture implements FixtureInterface, Conta
40 40 ->getEncoder($userAdmin)
41 41 ;
42 42 $userAdmin->setTeSenha($encoder->encodePassword('123456', $userAdmin->getSalt()));
  43 + $userAdmin->setApiKey('cacic123');
43 44  
44 45 //$userAdmin->setTeSenha('7c4a8d09ca3762af61e59520943dc26494f8941b');
45 46  
... ...
src/Cacic/CommonBundle/Entity/ComputadorRepository.php
... ... @@ -511,7 +511,7 @@ class ComputadorRepository extends EntityRepository
511 511 ->select('computador')
512 512 ->andwhere('computador.teIpComputador = :ip_computador')
513 513 ->andWhere('computador.idSo = :idSo')
514   - ->andWhere("computador.teIpRede = '0.0.0.0'")
  514 + ->innerJoin('CacicCommonBundle:Rede', 'rede', 'WITH', "computador.idRede = rede.idRede AND rede.teIpRede = '0.0.0.0'")
515 515 ->setMaxResults(1)
516 516 ->orderBy('computador.idComputador')
517 517 ->setParameter('ip_computador', $ip_computador)
... ... @@ -524,13 +524,12 @@ class ComputadorRepository extends EntityRepository
524 524 // Em último caso pega primeiro computador com menor Id
525 525 $qb = $this->createQueryBuilder('computador')
526 526 ->select('computador')
527   - ->andwhere('computador.teIpComputador = :ip_computador')
528 527 ->andWhere('computador.idSo = :idSo')
529 528 ->setMaxResults(1)
530 529 ->orderBy('computador.idComputador')
531 530 ->setParameter('idSo', $so->getIdSo());
532 531  
533   - $computador = $qb->getQuery()->getSingleResult();
  532 + $computador = $qb->getQuery()->getOneOrNullResult();
534 533 }
535 534  
536 535 }
... ...
src/Cacic/CommonBundle/Tests/BaseTestCase.php 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: eduardo
  5 + * Date: 07/10/14
  6 + * Time: 12:47
  7 + */
  8 +
  9 +namespace Cacic\CommonBundle\Tests;
  10 +
  11 +use Liip\FunctionalTestBundle\Test\WebTestCase;
  12 +
  13 +
  14 +class BaseTestCase extends WebTestCase {
  15 +
  16 + /**
  17 + * Método construtor
  18 + */
  19 + public function __construct() {
  20 + // Fixtures to be loaded on tests
  21 + $this->classes = array(
  22 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadLocalData',
  23 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadRedeData',
  24 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadClasseData',
  25 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadClassPropertyData',
  26 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadAcaoData',
  27 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadCollectDefClassData',
  28 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadConfiguracaoPadraoData',
  29 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadConfiguracaoLocalData',
  30 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadGrupoUsuarioData',
  31 + 'Cacic\CommonBundle\DataFixtures\ORM\LoadUsuarioData',
  32 + );
  33 + }
  34 +
  35 + /**
  36 + * Setup test data for all cases
  37 + */
  38 +
  39 + public function setUp() {
  40 + $this->loadFixtures($this->classes);
  41 + }
  42 +
  43 + /**
  44 + * Clear test data for all cases
  45 + */
  46 + public function tearDown() {
  47 +
  48 + }
  49 +}
0 50 \ No newline at end of file
... ...
src/Cacic/WSBundle/Controller/NeoController.php
... ... @@ -486,6 +486,22 @@ class NeoController extends Controller {
486 486  
487 487 $computador = $this->getComputadorSemMac($dados, $request);
488 488  
  489 + if (empty($computador)){
  490 + // Só vai retornar erro se não tiver nenhum computador cadastrado
  491 + $logger->error("Nenhum computador cadastrado!!! Erro no getUpdate");
  492 + // Retorna erro se o JSON for inválido
  493 + $error_msg = '{
  494 + "message": "Ainda não há computadores cadastrados",
  495 + "codigo": 3
  496 + }';
  497 +
  498 +
  499 + $response = new JsonResponse();
  500 + $response->setStatusCode('500');
  501 + $response->setContent($error_msg);
  502 + return $response;
  503 + }
  504 +
489 505 // 0 - Array de saída
490 506 $saida['agentcomputer'] = "";
491 507  
... ...
src/Cacic/WSBundle/Tests/Controller/NeoControllerTest.php
... ... @@ -8,19 +8,22 @@
8 8  
9 9 namespace Cacic\WSBundle\Tests\Controller;
10 10  
11   -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  11 +use Cacic\CommonBundle\Tests\BaseTestCase;
12 12 use Symfony\Component\HttpFoundation\Session;
13 13 use Symfony\Component\Serializer\Serializer;
14 14 use Symfony\Component\Serializer\Encoder\JsonEncoder;
15 15  
16 16  
17 17  
18   -class NeoControllerTest extends WebTestCase
  18 +class NeoControllerTest extends BaseTestCase
19 19 {
20 20 /**
21 21 * Método que cria dados comuns a todos os testes
22 22 */
23 23 public function setUp() {
  24 + // Load setup from BaseTestCase method
  25 + parent::setUp();
  26 +
24 27 $this->client = static::createClient();
25 28 $this->container = $this->client->getContainer();
26 29 $this->apiKey = $this->container->getParameter('test_api_key');
... ... @@ -246,7 +249,10 @@ class NeoControllerTest extends WebTestCase
246 249  
247 250 }
248 251  
249   - public function testUpdate() {
  252 + /**
  253 + * Essa função vai falhar se não tiver nenhum computador cadastrado
  254 + */
  255 + public function testSemMac() {
250 256 $logger = $this->container->get('logger');
251 257 $this->client->request(
252 258 'POST',
... ... @@ -292,10 +298,115 @@ class NeoControllerTest extends WebTestCase
292 298 $logger->debug("Response status: $status");
293 299 $logger->debug("JSON do getConfig: \n".$response->getContent());
294 300  
  301 + $this->assertEquals($status, 500);
  302 +
  303 + }
  304 +
  305 + /**
  306 + * Retorna o último computador cadastrado caso seja enviado um sem Mac
  307 + */
  308 + public function testUpdate() {
  309 + $logger = $this->container->get('logger');
  310 + // Primeiro insere um computador válido
  311 + $this->client->request(
  312 + 'POST',
  313 + '/ws/neo/getTest',
  314 + array(),
  315 + array(),
  316 + array(
  317 + 'CONTENT_TYPE' => 'application/json',
  318 + //'HTTPS' => true
  319 + ),
  320 + '{
  321 + "computador": {
  322 + "networkDevices": [
  323 + {
  324 + "ipv4": "10.1.0.56",
  325 + "ipv6": "fe80::295b:a8db:d433:ebe%4",
  326 + "mac": "9C:D2:1E:EA:E0:89",
  327 + "netmask_ipv4": "255.255.255.0",
  328 + "netmask_ipv6": "ffff:ffff:ffff:ffff::",
  329 + "nome": "Wi-Fi"
  330 + },
  331 + {
  332 + "ipv4": "192.168.56.1",
  333 + "ipv6": "fe80::19f2:4739:8a9e:45e4%16",
  334 + "mac": "08:00:27:00:14:2B",
  335 + "netmask_ipv4": "255.255.255.0",
  336 + "netmask_ipv6": "ffff:ffff:ffff:ffff::",
  337 + "nome": "VirtualBox Host-Only Network"
  338 + }
  339 + ],
  340 + "operatingSystem": {
  341 + "idOs": 176,
  342 + "nomeOs": "Windows_NT"
  343 + },
  344 + "usuario": "Eric Menezes",
  345 + "nmComputador": "Notebook-XPTO",
  346 + "versaoAgente": "2.8.0"
  347 + }
  348 + }'
  349 + );
  350 + $logger->debug("Dados JSON do computador enviados antes do getUpdate \n".$this->client->getRequest()->getcontent());
  351 +
  352 + $response = $this->client->getResponse();
  353 + $status = $response->getStatusCode();
  354 + $logger->debug("Response status: $status");
  355 +
  356 + $this->assertEquals($status, 200);
  357 +
  358 + // Agora tenta inserir um sem MAC
  359 + $this->client->request(
  360 + 'POST',
  361 + '/ws/neo/update',
  362 + array(),
  363 + array(),
  364 + array(
  365 + 'CONTENT_TYPE' => 'application/json',
  366 + //'HTTPS' => true
  367 + ),
  368 + '{
  369 + "computador": {
  370 + "networkDevices": [
  371 + {
  372 + "ipv4": "10.1.0.56",
  373 + "ipv6": "fe80::295b:a8db:d433:ebe%4",
  374 + "netmask_ipv4": "255.255.255.0",
  375 + "netmask_ipv6": "ffff:ffff:ffff:ffff::",
  376 + "nome": "Wi-Fi"
  377 + },
  378 + {
  379 + "ipv4": "192.168.56.1",
  380 + "ipv6": "fe80::19f2:4739:8a9e:45e4%16",
  381 + "netmask_ipv4": "255.255.255.0",
  382 + "netmask_ipv6": "ffff:ffff:ffff:ffff::",
  383 + "nome": "VirtualBox Host-Only Network"
  384 + }
  385 + ],
  386 + "operatingSystem": {
  387 + "idOs": 176,
  388 + "nomeOs": "Windows_NT"
  389 + },
  390 + "usuario": "Eric Menezes",
  391 + "nmComputador": "Notebook-XPTO",
  392 + "versaoAgente": "2.8.0"
  393 + }
  394 + }'
  395 + );
  396 + $logger->debug("Dados JSON do computador enviados sem MAC para o getUpdate \n".$this->client->getRequest()->getcontent());
  397 +
  398 + $response = $this->client->getResponse();
  399 + $status = $response->getStatusCode();
  400 + $logger->debug("Response status: $status");
  401 + $logger->debug("JSON do getUpdate: \n".$response->getContent());
  402 +
295 403 $this->assertEquals($status, 200);
296 404  
297 405 }
298 406  
  407 + /**
  408 + * Teste de inserção das coletas
  409 + */
299 410 public function testColeta() {
300 411 $logger = $this->container->get('logger');
301 412 $this->client->request(
... ... @@ -451,6 +562,8 @@ class NeoControllerTest extends WebTestCase
451 562 * Método que apaga todos os dados criados no teste
452 563 */
453 564 public function tearDown() {
  565 + // Executa método de limpeza de todos os casos de teste
  566 + parent::tearDown();
454 567  
455 568 }
456 569  
... ...