PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Cacic/CommonBundle/Tests/Controller/NotificationsControllerTest.php

https://gitlab.com/adrianovieira/cacic
PHP | 233 lines | 156 code | 42 blank | 35 comment | 3 complexity | 98e5fc5ef31dc03e67188d511fb366a4 MD5 | raw file
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: eduardo
  5. * Date: 29/07/15
  6. * Time: 17:10
  7. */
  8. namespace Cacic\CommonBundle\Tests\Controller;
  9. use Cacic\CommonBundle\Entity\SoftwareRelatorio;
  10. use Cacic\CommonBundle\Tests\BaseTestCase;
  11. use Proxies\__CG__\Cacic\CommonBundle\Entity\So;
  12. class NotificationsControllerTest extends BaseTestCase
  13. {
  14. public function setUp()
  15. {
  16. // Load base data
  17. parent::setUp();
  18. $this->fixtures = $this->loadFixtures($this->classes)->getReferenceRepository();
  19. // Cliente que simula o agente
  20. $this->static_client = static::createClient();
  21. // Cliente autenticado
  22. $this->client = static::makeClient(true);
  23. $this->container = $this->client->getContainer();
  24. $this->logger = $this->container->get('logger');
  25. $this->em = $this->container->get('doctrine')->getManager();
  26. $kernel = $this->container->get('kernel');
  27. $this->ws_data_dir = $kernel->locateResource("@CacicWSBundle/Resources/data/fixtures/");
  28. $this->modifications = file_get_contents($this->ws_data_dir."modifications.json");
  29. $this->coleta_modifications = file_get_contents($this->ws_data_dir."coleta-modifications.json");
  30. }
  31. /**
  32. * Testa recuperação de lista de notificações
  33. */
  34. public function testGetNotifications() {
  35. // Cria dados para testar
  36. $this->static_client->request(
  37. 'POST',
  38. '/ws/neo/coleta',
  39. array(),
  40. array(),
  41. array(
  42. 'CONTENT_TYPE' => 'application/json',
  43. //'HTTPS' => true
  44. ),
  45. $this->coleta_modifications
  46. );
  47. $response = $this->static_client->getResponse();
  48. $status = $response->getStatusCode();
  49. $this->logger->debug("Response status: $status");
  50. //$logger->debug("JSON da coleta: \n".$response->getContent());
  51. $this->assertEquals($status, 200);
  52. // Verifica se o Software Coleta foi inserido
  53. $em = $this->container->get('doctrine')->getManager();
  54. $software = $em->getRepository("CacicCommonBundle:Software")->getByName('Messaging account plugin for AIM');
  55. $this->assertNotEmpty($software, "Software Messaging account plugin for AIM não encontrado após coleta");
  56. // Só vai criar a notificação se existir um relatório cadastrado
  57. $user = $em->getRepository("CacicCommonBundle:Usuario")->find(1);
  58. $em->persist($user);
  59. $relatorio = new SoftwareRelatorio();
  60. $relatorio->setNivelAcesso('publico');
  61. $relatorio->setNomeRelatorio('Software');
  62. $relatorio->setHabilitaNotificacao(true);
  63. $relatorio->setIdUsuario($user);
  64. $relatorio->addSoftware($software);
  65. $em->persist($relatorio);
  66. $em->flush();
  67. // Insere software no relatório
  68. $idRelatorio = $relatorio->getIdRelatorio();
  69. $idSoftware = $software->getIdSoftware();
  70. $sql = "INSERT INTO relatorios_software (id_relatorio, id_software)
  71. VALUES ($idRelatorio, $idSoftware)";
  72. $stmt = $em->getConnection()->prepare($sql);
  73. $stmt->execute();
  74. // Agora testa a busca pelo software
  75. $classProperty = $em->getRepository("CacicCommonBundle:ClassProperty")->findOneBy(array(
  76. 'nmPropertyName' => 'account-plugin-aim'
  77. ));
  78. $this->assertNotEmpty($classProperty, "Propriedade não encontrada pelo nome account-plugin-aim");
  79. $prop = $em->getRepository("CacicCommonBundle:PropriedadeSoftware")->findOneBy(array(
  80. 'classProperty' => $classProperty
  81. ));
  82. $this->assertNotEmpty($prop, "Propriedade de software não encontrada");
  83. $prop = $em->getRepository("CacicCommonBundle:PropriedadeSoftware")->findOneBy(array(
  84. 'software' => $software
  85. ));
  86. $this->assertNotEmpty($prop, "Software não encontradao");
  87. $softwareRelatorio = $em->getRepository("CacicCommonBundle:SoftwareRelatorio")->findSoftware('account-plugin-aim');
  88. $this->assertNotEmpty($softwareRelatorio, "Relatório não encontrado para o software account-plugin-aim");
  89. // Agora envia a modificação
  90. $this->static_client->request(
  91. 'POST',
  92. '/ws/neo/modifications',
  93. array(),
  94. array(),
  95. array(
  96. 'CONTENT_TYPE' => 'application/json',
  97. //'HTTPS' => true
  98. ),
  99. $this->modifications
  100. );
  101. $response = $this->static_client->getResponse();
  102. $status = $response->getStatusCode();
  103. $this->logger->debug("Response status: $status");
  104. $this->assertEquals($status, 200);
  105. // Limpa o cache para garantir o resultado
  106. $em->clear();
  107. // Conecta à rota
  108. $crawler = $this->client->request(
  109. 'GET',
  110. '/notifications/get',
  111. array(),
  112. array(),
  113. array(
  114. 'CONTENT_TYPE' => 'application/json',
  115. //'HTTPS' => true
  116. ),
  117. '{}'
  118. );
  119. // Testa rota
  120. $response = $this->client->getResponse();
  121. $status = $response->getStatusCode();
  122. $this->logger->debug("Response status: $status");
  123. $this->assertEquals(200, $status);
  124. $this->assertNotEmpty($response->getContent(), "Conteúdo de notificações vazio");
  125. //$this->logger->debug("NOTIFICATIONS:\n".$response->getContent());
  126. // Debug JSON Content
  127. $tmpfile = sys_get_temp_dir(). '/notifications.json';
  128. file_put_contents($tmpfile, $response->getContent());
  129. $this->logger->debug("NOTIFICATIONS: file temp = $tmpfile");
  130. $notifications_json = $response->getContent();
  131. $notifications = json_decode($notifications_json, true);
  132. $this->assertCount(2, $notifications, "Não foram encontradas duas notificações");
  133. // Testa o caso em que o software está desabilitado
  134. $softwareRelatorio = $em->getRepository("CacicCommonBundle:SoftwareRelatorio")->findSoftware('account-plugin-aim');
  135. $softwareRelatorio->setHabilitaNotificacao(false);
  136. $em->persist($softwareRelatorio);
  137. $sql = "DELETE FROM notifications";
  138. $stmt = $em->getConnection()->prepare($sql);
  139. $stmt->execute();
  140. $em->flush();
  141. // Agora envia a modificação
  142. $this->static_client->request(
  143. 'POST',
  144. '/ws/neo/modifications',
  145. array(),
  146. array(),
  147. array(
  148. 'CONTENT_TYPE' => 'application/json',
  149. //'HTTPS' => true
  150. ),
  151. $this->modifications
  152. );
  153. $response = $this->static_client->getResponse();
  154. $status = $response->getStatusCode();
  155. $this->logger->debug("Response status: $status");
  156. $this->assertEquals($status, 200);
  157. // Limpa o cache para garantir o resultado
  158. $em->clear();
  159. // Conecta à rota
  160. $crawler = $this->client->request(
  161. 'GET',
  162. '/notifications/get',
  163. array(),
  164. array(),
  165. array(
  166. 'CONTENT_TYPE' => 'application/json',
  167. //'HTTPS' => true
  168. ),
  169. '{}'
  170. );
  171. // Testa rota
  172. $response = $this->client->getResponse();
  173. $status = $response->getStatusCode();
  174. $this->logger->debug("Response status: $status");
  175. $this->assertEquals(200, $status);
  176. $tmpfile = sys_get_temp_dir(). '/notifications2.json';
  177. file_put_contents($tmpfile, $response->getContent());
  178. $this->logger->debug("NOTIFICATIONS: file temp = $tmpfile");
  179. $notifications_json = $response->getContent();
  180. $notifications = json_decode($notifications_json, true);
  181. foreach ($notifications as $elm) {
  182. if ($elm['object'] == 'Software') {
  183. $this->fail("A coleta de software foi encontrada nas notificações, e não deveria estar");
  184. }
  185. }
  186. }
  187. public function tearDown() {
  188. // Remove default data
  189. parent::tearDown();
  190. }
  191. }