Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Tests\Functional;
- use App\Test\ApiHelper;
- use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
- use Hautelook\AliceBundle\PhpUnit\ReloadDatabaseTrait;
- use App\Entity\User;
- use App\Entity\Allergy;
- class AllergyResourceTest extends ApiHelper
- {
- use ReloadDatabaseTrait;
- public function testViewAllergyListings()
- {
- $client = self::createClient();
- $client->request('GET', '/api/allergies', [
- 'headers' => ['Content-Type' => 'application/json'],
- 'json' => []
- ]);
- $this->assertResponseStatusCodeSame(200);
- }
- public function testPostAllergy() {
- $client = self::createClient();
- // Not logged in should throw a 401.
- $client->request('POST', '/api/allergies', [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(401);
- // Logged in as a normal user should throw a 403
- $this->createUserAndLogIn($client, 'roleuser@auxmkt.webdev', 'roleuser', 'password', array());
- $client->request('POST', '/api/allergies', [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(403);
- // Logged in as a editor should throw a 201
- $this->createUserAndLogIn($client, 'roleeditor@auxmkt.webdev', 'roleeditor', 'password', array('ROLE_EDITOR'));
- $client->request('POST', '/api/allergies', [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(201);
- // Logged in as a admin should throw a 201
- $this->createUserAndLogIn($client, 'roleadmin@auxmkt.webdev', 'roleadmin', 'password', array('ROLE_ADMIN'));
- $client->request('POST', '/api/allergies', [
- 'json' => [
- 'name' => 'Nuts Update'
- ]
- ]);
- $this->assertResponseStatusCodeSame(201);
- }
- public function testPutAllergy() {
- $client = self::createClient();
- $allergy = new Allergy();
- $allergy->setName("Nut");
- $em = $this->getEm();
- $em->persist($allergy);
- $em->flush();
- // Not logged in throws a 401
- $client->request('PUT', '/api/allergies/' . $allergy->getId(), [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(401);
- // Logged in as a normal user throws a 403
- $this->createUserAndLogIn($client, 'roleuser@auxmkt.webdev', 'roleuser', 'password', array());
- $client->request('PUT', '/api/allergies/' . $allergy->getId(), [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(403);
- // Logged in as an editor should be allowed and throw a 200
- $this->createUserAndLogIn($client, 'roleeditor@auxmkt.webdev', 'roleeditor', 'password', array('ROLE_EDITOR'));
- $client->request('PUT', '/api/allergies/' . $allergy->getId(), [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(200);
- // Logged in as an admin should be allowed and throw a 200
- $this->createUserAndLogIn($client, 'roleadmin@auxmkt.webdev', 'roleadmin', 'password', array('ROLE_ADMIN'));
- $client->request('PUT', '/api/allergies/' . $allergy->getId(), [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(200);
- }
- public function testDeleteAllergy() {
- $client = self::createClient();
- $allergy = new Allergy();
- $allergy->setName("Nut");
- $em = $this->getEm();
- $em->persist($allergy);
- $em->flush();
- // Not logged in throws a 401
- $client->request('DELETE', '/api/allergies/' . $allergy->getId(), [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(401);
- // Logged in as a normal user throws a 403
- $this->createUserAndLogIn($client, 'roleuser@auxmkt.webdev', 'roleuser', 'password', array());
- $client->request('DELETE', '/api/allergies/' . $allergy->getId(), [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(403);
- // Logged in as an editor should be allowed and throw a 200
- $this->createUserAndLogIn($client, 'roleeditor@auxmkt.webdev', 'roleeditor', 'password', array('ROLE_EDITOR'));
- $client->request('DELETE', '/api/allergies/' . $allergy->getId(), [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(204);
- $allergy = new Allergy();
- $allergy->setName("Nut");
- $em->persist($allergy);
- $em->flush();
- // Logged in as an admin should be allowed and throw a 200
- $this->createUserAndLogIn($client, 'roleadmin@auxmkt.webdev', 'roleadmin', 'password', array('ROLE_ADMIN'));
- $client->request('DELETE', '/api/allergies/' . $allergy->getId(), [
- 'json' => [
- 'name' => 'Nuts'
- ]
- ]);
- $this->assertResponseStatusCodeSame(204);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement