Advertisement
Guest User

Untitled

a guest
Sep 18th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. <?php
  2. namespace App\Tests\Functional;
  3. use App\Test\ApiHelper;
  4. use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
  5. use Hautelook\AliceBundle\PhpUnit\ReloadDatabaseTrait;
  6. use App\Entity\User;
  7. use App\Entity\Allergy;
  8. class AllergyResourceTest extends ApiHelper
  9. {
  10. use ReloadDatabaseTrait;
  11.  
  12. public function testViewAllergyListings()
  13. {
  14. $client = self::createClient();
  15. $client->request('GET', '/api/allergies', [
  16. 'headers' => ['Content-Type' => 'application/json'],
  17. 'json' => []
  18. ]);
  19.  
  20. $this->assertResponseStatusCodeSame(200);
  21. }
  22. public function testPostAllergy() {
  23. $client = self::createClient();
  24.  
  25. // Not logged in should throw a 401.
  26.  
  27. $client->request('POST', '/api/allergies', [
  28. 'json' => [
  29. 'name' => 'Nuts'
  30. ]
  31. ]);
  32. $this->assertResponseStatusCodeSame(401);
  33.  
  34. // Logged in as a normal user should throw a 403
  35. $this->createUserAndLogIn($client, 'roleuser@auxmkt.webdev', 'roleuser', 'password', array());
  36. $client->request('POST', '/api/allergies', [
  37. 'json' => [
  38. 'name' => 'Nuts'
  39. ]
  40. ]);
  41. $this->assertResponseStatusCodeSame(403);
  42.  
  43.  
  44. // Logged in as a editor should throw a 201
  45. $this->createUserAndLogIn($client, 'roleeditor@auxmkt.webdev', 'roleeditor', 'password', array('ROLE_EDITOR'));
  46. $client->request('POST', '/api/allergies', [
  47. 'json' => [
  48. 'name' => 'Nuts'
  49. ]
  50. ]);
  51. $this->assertResponseStatusCodeSame(201);
  52.  
  53. // Logged in as a admin should throw a 201
  54. $this->createUserAndLogIn($client, 'roleadmin@auxmkt.webdev', 'roleadmin', 'password', array('ROLE_ADMIN'));
  55. $client->request('POST', '/api/allergies', [
  56. 'json' => [
  57. 'name' => 'Nuts Update'
  58. ]
  59. ]);
  60. $this->assertResponseStatusCodeSame(201);
  61.  
  62. }
  63. public function testPutAllergy() {
  64. $client = self::createClient();
  65.  
  66.  
  67. $allergy = new Allergy();
  68. $allergy->setName("Nut");
  69. $em = $this->getEm();
  70. $em->persist($allergy);
  71. $em->flush();
  72.  
  73.  
  74. // Not logged in throws a 401
  75. $client->request('PUT', '/api/allergies/' . $allergy->getId(), [
  76. 'json' => [
  77. 'name' => 'Nuts'
  78. ]
  79. ]);
  80. $this->assertResponseStatusCodeSame(401);
  81.  
  82.  
  83. // Logged in as a normal user throws a 403
  84. $this->createUserAndLogIn($client, 'roleuser@auxmkt.webdev', 'roleuser', 'password', array());
  85. $client->request('PUT', '/api/allergies/' . $allergy->getId(), [
  86. 'json' => [
  87. 'name' => 'Nuts'
  88. ]
  89. ]);
  90. $this->assertResponseStatusCodeSame(403);
  91.  
  92. // Logged in as an editor should be allowed and throw a 200
  93. $this->createUserAndLogIn($client, 'roleeditor@auxmkt.webdev', 'roleeditor', 'password', array('ROLE_EDITOR'));
  94. $client->request('PUT', '/api/allergies/' . $allergy->getId(), [
  95. 'json' => [
  96. 'name' => 'Nuts'
  97. ]
  98. ]);
  99. $this->assertResponseStatusCodeSame(200);
  100.  
  101. // Logged in as an admin should be allowed and throw a 200
  102. $this->createUserAndLogIn($client, 'roleadmin@auxmkt.webdev', 'roleadmin', 'password', array('ROLE_ADMIN'));
  103. $client->request('PUT', '/api/allergies/' . $allergy->getId(), [
  104. 'json' => [
  105. 'name' => 'Nuts'
  106. ]
  107. ]);
  108. $this->assertResponseStatusCodeSame(200);
  109. }
  110. public function testDeleteAllergy() {
  111. $client = self::createClient();
  112.  
  113.  
  114. $allergy = new Allergy();
  115. $allergy->setName("Nut");
  116. $em = $this->getEm();
  117. $em->persist($allergy);
  118. $em->flush();
  119.  
  120.  
  121. // Not logged in throws a 401
  122. $client->request('DELETE', '/api/allergies/' . $allergy->getId(), [
  123. 'json' => [
  124. 'name' => 'Nuts'
  125. ]
  126. ]);
  127. $this->assertResponseStatusCodeSame(401);
  128.  
  129.  
  130. // Logged in as a normal user throws a 403
  131. $this->createUserAndLogIn($client, 'roleuser@auxmkt.webdev', 'roleuser', 'password', array());
  132. $client->request('DELETE', '/api/allergies/' . $allergy->getId(), [
  133. 'json' => [
  134. 'name' => 'Nuts'
  135. ]
  136. ]);
  137. $this->assertResponseStatusCodeSame(403);
  138.  
  139. // Logged in as an editor should be allowed and throw a 200
  140. $this->createUserAndLogIn($client, 'roleeditor@auxmkt.webdev', 'roleeditor', 'password', array('ROLE_EDITOR'));
  141. $client->request('DELETE', '/api/allergies/' . $allergy->getId(), [
  142. 'json' => [
  143. 'name' => 'Nuts'
  144. ]
  145. ]);
  146. $this->assertResponseStatusCodeSame(204);
  147.  
  148.  
  149.  
  150. $allergy = new Allergy();
  151. $allergy->setName("Nut");
  152. $em->persist($allergy);
  153. $em->flush();
  154.  
  155.  
  156. // Logged in as an admin should be allowed and throw a 200
  157. $this->createUserAndLogIn($client, 'roleadmin@auxmkt.webdev', 'roleadmin', 'password', array('ROLE_ADMIN'));
  158. $client->request('DELETE', '/api/allergies/' . $allergy->getId(), [
  159. 'json' => [
  160. 'name' => 'Nuts'
  161. ]
  162. ]);
  163. $this->assertResponseStatusCodeSame(204);
  164. }
  165.  
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement