From e330ad8787ec2f76fec57de59ea83424f11af84d Mon Sep 17 00:00:00 2001 From: Mikel Madariaga Date: Wed, 24 Apr 2024 11:14:41 +0200 Subject: [PATCH 1/3] tests: added API test settings & fixtures --- app/config/services_test.yaml | 4 -- app/features/bootstrap/bootstrap.php | 11 ++++ app/src/Demo/Stub/AdministratorStub.php | 37 +++++++++++ .../Stub/DataFixtures/FixtureHelperTrait.php | 60 ++++++++++++++++++ .../DataFixtures/ORM/AdministratorFixture.php | 42 +++++++++++++ app/src/Demo/Stub/StubTrait.php | 61 +++++++++++++++++++ 6 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 app/features/bootstrap/bootstrap.php create mode 100644 app/src/Demo/Stub/AdministratorStub.php create mode 100644 app/src/Demo/Stub/DataFixtures/FixtureHelperTrait.php create mode 100644 app/src/Demo/Stub/DataFixtures/ORM/AdministratorFixture.php create mode 100644 app/src/Demo/Stub/StubTrait.php diff --git a/app/config/services_test.yaml b/app/config/services_test.yaml index 352a3ec..4272563 100755 --- a/app/config/services_test.yaml +++ b/app/config/services_test.yaml @@ -9,10 +9,6 @@ services: Demo\Stub\: resource: '../src/Demo/Stub/*' - Doubles\Demo\Domain\Service\: - resource: '../src/Doubles/Demo/Domain/Service/*/*' - tags: [ { name: 'domain.service' } ] - Ivoz\Api\Behat\Context\FeatureContext: ~ App\Service\Behat\FeatureContext: diff --git a/app/features/bootstrap/bootstrap.php b/app/features/bootstrap/bootstrap.php new file mode 100644 index 0000000..af1d854 --- /dev/null +++ b/app/features/bootstrap/bootstrap.php @@ -0,0 +1,11 @@ +bootEnv(dirname(__DIR__, 2) . '/.env'); +} diff --git a/app/src/Demo/Stub/AdministratorStub.php b/app/src/Demo/Stub/AdministratorStub.php new file mode 100644 index 0000000..da2964d --- /dev/null +++ b/app/src/Demo/Stub/AdministratorStub.php @@ -0,0 +1,37 @@ +setUsername('admin') + ->setPass('changeme') + ->setEmail('testAdmin@irontec.com') + ->setName('Name') + ->setLastname('Last name'); + + $this->append($dto); + + $dto2 = (new AdministratorDto(2)) + ->setUsername('another_admin') + ->setPass('changeme') + ->setEmail('admin2@irontec.com') + ->setName('Name') + ->setLastname('Last name'); + + $this->append($dto2); + } +} diff --git a/app/src/Demo/Stub/DataFixtures/FixtureHelperTrait.php b/app/src/Demo/Stub/DataFixtures/FixtureHelperTrait.php new file mode 100644 index 0000000..89c6ab4 --- /dev/null +++ b/app/src/Demo/Stub/DataFixtures/FixtureHelperTrait.php @@ -0,0 +1,60 @@ +newInstanceWithoutConstructor(); + } + + protected function sanitizeEntityValues(EntityInterface $entity) + { + $sanitizer = function () { + $this->initChangelog(); + $this->sanitizeValues(); + }; + + $sanitizer->call($entity); + + return $entity; + } + + protected function disableLifecycleEvents(EntityManagerInterface $em) + { + $eventManager = $em->getEventManager(); + $doctrineEventSubscriber = $this->filterDoctrineEventSubscriber($eventManager); + + if (!$doctrineEventSubscriber) { + return; + } + + $eventManager->removeEventSubscriber($doctrineEventSubscriber); + } + + protected function filterDoctrineEventSubscriber(EventManager $eventManager) + { + $listenersByEvent = $eventManager->getListeners(); + + foreach ($listenersByEvent as $event => $listeners) { + foreach ($listeners as $listener) { + if ($listener instanceof DoctrineEventSubscriber) { + return $listener; + } + } + } + + return null; + } +} diff --git a/app/src/Demo/Stub/DataFixtures/ORM/AdministratorFixture.php b/app/src/Demo/Stub/DataFixtures/ORM/AdministratorFixture.php new file mode 100644 index 0000000..2434f80 --- /dev/null +++ b/app/src/Demo/Stub/DataFixtures/ORM/AdministratorFixture.php @@ -0,0 +1,42 @@ +disableLifecycleEvents($manager); + $manager + ->getClassMetadata(Administrator::class) + ->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE); + + $entities = $this->adminStub->getAll(); + foreach ($entities as $entity) { + $this->addReference( + '_reference_Administrator' . $entity->getId(), + $entity + ); + $manager->persist($entity); + } + + $manager->flush(); + } +} diff --git a/app/src/Demo/Stub/StubTrait.php b/app/src/Demo/Stub/StubTrait.php new file mode 100644 index 0000000..a6a34a3 --- /dev/null +++ b/app/src/Demo/Stub/StubTrait.php @@ -0,0 +1,61 @@ +entityAssembler = $entityAssembler; + } + + abstract protected function load(); + abstract protected function getEntityName(): string; + + protected function append(DataTransferObjectInterface $dto) + { + $entity = $this->entityAssembler->createFromDto( + $dto, + $this->getEntityName() + ); + $entityId = $entity->getId(); + + if (array_key_exists($entityId, $this->items)) { + throw new \Exception( + $entity->__toString() . ' already exists' + ); + } + + $this->items[$entityId] = $entity; + } + + public function getAll(): array + { + if (!$this->loaded) { + $this->load(); + $this->loaded = true; + } + + return $this->items; + } + + public function get(int $idx): EntityInterface + { + if (!$this->loaded) { + $this->load(); + $this->loaded = true; + } + + return $this->items[$idx]; + } +} From eddd1e178ad6f38c1756659d387e12a86a35ae43 Mon Sep 17 00:00:00 2001 From: Mikel Madariaga Date: Wed, 24 Apr 2024 11:14:59 +0200 Subject: [PATCH 2/3] tests: added admin API tests --- .../administrator/getAdministrator.feature | 47 ++++++++++++++++ .../administrator/postAdministrator.feature | 53 +++++++++++++++++++ .../administrator/putAdministrator.feature | 50 +++++++++++++++++ .../administrator/removeAdministrator.feature | 19 +++++++ 4 files changed, 169 insertions(+) create mode 100644 app/features/demo/administrator/getAdministrator.feature create mode 100644 app/features/demo/administrator/postAdministrator.feature create mode 100644 app/features/demo/administrator/putAdministrator.feature create mode 100644 app/features/demo/administrator/removeAdministrator.feature diff --git a/app/features/demo/administrator/getAdministrator.feature b/app/features/demo/administrator/getAdministrator.feature new file mode 100644 index 0000000..a782122 --- /dev/null +++ b/app/features/demo/administrator/getAdministrator.feature @@ -0,0 +1,47 @@ +Feature: Retrieve administrators + In order to manage administrators + As a client admin + I need to be able to retrieve them through the API. + + @createSchema + Scenario: Retrieve the administrators json list + Given I add Authorization header + When I add "Accept" header equal to "application/json" + And I send a "GET" request to "administrators" + Then the response status code should be 200 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/json; charset=utf-8" + And the JSON should be equal to: + """ + [ + { + "username": "admin", + "email": "testAdmin@irontec.com", + "id": 1 + }, + { + "username": "another_admin", + "email": "admin2@irontec.com", + "id": 2 + } + ] + """ + + Scenario: Retrieve certain administrators json + Given I add Authorization header + When I add "Accept" header equal to "application/json" + And I send a "GET" request to "administrators/1" + Then the response status code should be 200 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/json; charset=utf-8" + And the JSON should be equal to: + """ + { + "username": "admin", + "pass": "*****", + "email": "testAdmin@irontec.com", + "name": "Name", + "lastname": "Last name", + "id": 1 + } + """ diff --git a/app/features/demo/administrator/postAdministrator.feature b/app/features/demo/administrator/postAdministrator.feature new file mode 100644 index 0000000..b9c8161 --- /dev/null +++ b/app/features/demo/administrator/postAdministrator.feature @@ -0,0 +1,53 @@ +Feature: Create administrators + In order to manage administrators + As a client admin + I need to be able to create them through the API. + + @createSchema + Scenario: Create a administrators + Given I add Authorization header + When I add "Content-Type" header equal to "application/json" + And I add "Accept" header equal to "application/json" + And I send a "POST" request to "/administrators" with body: + """ + { + "username": "admin3", + "pass": "topSecret", + "email": "admin3@irontec.com", + "name": "Name", + "lastname": "Last name" + } + """ + Then the response status code should be 201 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/json; charset=utf-8" + And the JSON should be equal to: + """ + { + "username": "admin3", + "pass": "*****", + "email": "admin3@irontec.com", + "name": "Name", + "lastname": "Last name", + "id": 3 + } + """ + + Scenario: Retrieve created administrators + Given I add Authorization header + When I add "Accept" header equal to "application/json" + And I send a "GET" request to "/administrators/3" + Then the response status code should be 200 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/json; charset=utf-8" + And the JSON should be equal to: + """ + { + "username": "admin3", + "pass": "*****", + "email": "admin3@irontec.com", + "name": "Name", + "lastname": "Last name", + "id": 3 + } + """ diff --git a/app/features/demo/administrator/putAdministrator.feature b/app/features/demo/administrator/putAdministrator.feature new file mode 100644 index 0000000..c0dc9f0 --- /dev/null +++ b/app/features/demo/administrator/putAdministrator.feature @@ -0,0 +1,50 @@ + Feature: Update administrators + In order to manage administrators + As a client admin + I need to be able to update them through the API. + + @createSchema + Scenario: Unable to update a admin #1 + Given I add Authorization header + When I add "Content-Type" header equal to "application/json" + And I add "Accept" header equal to "application/json" + And I send a "PUT" request to "/administrators/1" with body: + """ + { + "username": "admin_updated", + "pass": "topSecret", + "email": "admin2_updated@irontec.com", + "name": "Name_updated", + "lastname": "Last name updated" + } + """ + Then the response status code should be 403 + + Scenario: Update a administrators + Given I add Authorization header + When I add "Content-Type" header equal to "application/json" + And I add "Accept" header equal to "application/json" + And I send a "PUT" request to "/administrators/2" with body: + """ + { + "username": "admin_updated", + "pass": "topSecret", + "email": "admin2_updated@irontec.com", + "name": "Name_updated", + "lastname": "Last name updated" + } + """ + Then the response status code should be 200 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/json; charset=utf-8" + And the JSON should be equal to: + """ + { + "username": "admin_updated", + "pass": "*****", + "email": "admin2_updated@irontec.com", + "name": "Name_updated", + "lastname": "Last name updated", + "id": 2 + } + """ diff --git a/app/features/demo/administrator/removeAdministrator.feature b/app/features/demo/administrator/removeAdministrator.feature new file mode 100644 index 0000000..364d08f --- /dev/null +++ b/app/features/demo/administrator/removeAdministrator.feature @@ -0,0 +1,19 @@ +Feature: Manage administrators + In order to manage administrators + As a client admin + I need to be able to delete them through the API. + + @createSchema + Scenario: Unable to remove a admin #1 + Given I add Authorization header + When I add "Content-Type" header equal to "application/json" + And I add "Accept" header equal to "application/json" + And I send a "DELETE" request to "/administrators/1" + Then the response status code should be 403 + + Scenario: Remove a administrators + Given I add Authorization header + When I add "Content-Type" header equal to "application/json" + And I add "Accept" header equal to "application/json" + And I send a "DELETE" request to "/administrators/2" + Then the response status code should be 204 \ No newline at end of file From 37e2ab4e4939bbaacdef097ccda4565b8386d3dc Mon Sep 17 00:00:00 2001 From: Mikel Madariaga Date: Wed, 24 Apr 2024 11:38:56 +0200 Subject: [PATCH 3/3] tests: added advanced matchers --- app/features/demo/administrator/postAdministrator.feature | 8 ++++---- app/features/demo/administrator/putAdministrator.feature | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/features/demo/administrator/postAdministrator.feature b/app/features/demo/administrator/postAdministrator.feature index b9c8161..2041dec 100644 --- a/app/features/demo/administrator/postAdministrator.feature +++ b/app/features/demo/administrator/postAdministrator.feature @@ -21,7 +21,7 @@ Feature: Create administrators Then the response status code should be 201 And the response should be in JSON And the header "Content-Type" should be equal to "application/json; charset=utf-8" - And the JSON should be equal to: + And the JSON should be like: """ { "username": "admin3", @@ -29,7 +29,7 @@ Feature: Create administrators "email": "admin3@irontec.com", "name": "Name", "lastname": "Last name", - "id": 3 + "id": "match:type(integer)" } """ @@ -40,7 +40,7 @@ Feature: Create administrators Then the response status code should be 200 And the response should be in JSON And the header "Content-Type" should be equal to "application/json; charset=utf-8" - And the JSON should be equal to: + And the JSON should be like: """ { "username": "admin3", @@ -48,6 +48,6 @@ Feature: Create administrators "email": "admin3@irontec.com", "name": "Name", "lastname": "Last name", - "id": 3 + "id": "match:regexp(/[0-9]+/)" } """ diff --git a/app/features/demo/administrator/putAdministrator.feature b/app/features/demo/administrator/putAdministrator.feature index c0dc9f0..ebfa84f 100644 --- a/app/features/demo/administrator/putAdministrator.feature +++ b/app/features/demo/administrator/putAdministrator.feature @@ -37,7 +37,7 @@ Then the response status code should be 200 And the response should be in JSON And the header "Content-Type" should be equal to "application/json; charset=utf-8" - And the JSON should be equal to: + And the JSON should be like: """ { "username": "admin_updated", @@ -45,6 +45,6 @@ "email": "admin2_updated@irontec.com", "name": "Name_updated", "lastname": "Last name updated", - "id": 2 + "id": "match:type(number)" } """