-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from irontec/CDD-39-api-tests
API tests
- Loading branch information
Showing
10 changed files
with
380 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
putenv('APP_ENV=' . $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = 'test'); | ||
|
||
if (file_exists(dirname(__DIR__, 2) . '/config/bootstrap.php')) { | ||
require dirname(__DIR__, 2) . '/config/bootstrap.php'; | ||
} elseif (method_exists(Dotenv::class, 'bootEnv')) { | ||
(new Dotenv())->bootEnv(dirname(__DIR__, 2) . '/.env'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]", | ||
"id": 1 | ||
}, | ||
{ | ||
"username": "another_admin", | ||
"email": "[email protected]", | ||
"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": "[email protected]", | ||
"name": "Name", | ||
"lastname": "Last name", | ||
"id": 1 | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]", | ||
"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 like: | ||
""" | ||
{ | ||
"username": "admin3", | ||
"pass": "*****", | ||
"email": "[email protected]", | ||
"name": "Name", | ||
"lastname": "Last name", | ||
"id": "match:type(integer)" | ||
} | ||
""" | ||
|
||
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 like: | ||
""" | ||
{ | ||
"username": "admin3", | ||
"pass": "*****", | ||
"email": "[email protected]", | ||
"name": "Name", | ||
"lastname": "Last name", | ||
"id": "match:regexp(/[0-9]+/)" | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]", | ||
"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": "[email protected]", | ||
"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 like: | ||
""" | ||
{ | ||
"username": "admin_updated", | ||
"pass": "*****", | ||
"email": "[email protected]", | ||
"name": "Name_updated", | ||
"lastname": "Last name updated", | ||
"id": "match:type(number)" | ||
} | ||
""" |
19 changes: 19 additions & 0 deletions
19
app/features/demo/administrator/removeAdministrator.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Demo\Stub; | ||
|
||
use Demo\Domain\Model\Administrator\Administrator; | ||
use Demo\Domain\Model\Administrator\AdministratorDto; | ||
|
||
class AdministratorStub | ||
{ | ||
use StubTrait; | ||
|
||
protected function getEntityName(): string | ||
{ | ||
return Administrator::class; | ||
} | ||
|
||
protected function load() | ||
{ | ||
$dto = (new AdministratorDto(1)) | ||
->setUsername('admin') | ||
->setPass('changeme') | ||
->setEmail('[email protected]') | ||
->setName('Name') | ||
->setLastname('Last name'); | ||
|
||
$this->append($dto); | ||
|
||
$dto2 = (new AdministratorDto(2)) | ||
->setUsername('another_admin') | ||
->setPass('changeme') | ||
->setEmail('[email protected]') | ||
->setName('Name') | ||
->setLastname('Last name'); | ||
|
||
$this->append($dto2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Demo\Stub\DataFixtures; | ||
|
||
use Doctrine\Common\EventManager; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Ivoz\Core\Domain\Model\EntityInterface; | ||
use Ivoz\Core\Infrastructure\Domain\Service\Lifecycle\DoctrineEventSubscriber; | ||
|
||
trait FixtureHelperTrait | ||
{ | ||
/** | ||
* @param string $className | ||
* @return EntityInterface | ||
*/ | ||
protected function createEntityInstance(string $className) | ||
{ | ||
$reflectionClass = new \ReflectionClass($className); | ||
return $reflectionClass->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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/Demo/Stub/DataFixtures/ORM/AdministratorFixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Demo\Stub\DataFixtures\ORM; | ||
|
||
use Demo\Domain\Model\Administrator\Administrator; | ||
use Demo\Stub\AdministratorStub; | ||
use Doctrine\Bundle\FixturesBundle\Fixture; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\Persistence\ObjectManager; | ||
use Demo\Stub\DataFixtures\FixtureHelperTrait; | ||
|
||
class AdministratorFixture extends Fixture | ||
{ | ||
use FixtureHelperTrait; | ||
|
||
public function __construct( | ||
private AdministratorStub $adminStub | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function load(ObjectManager $manager) | ||
{ | ||
$this->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(); | ||
} | ||
} |
Oops, something went wrong.