Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API tests #16

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/config/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions app/features/bootstrap/bootstrap.php
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');
}
47 changes: 47 additions & 0 deletions app/features/demo/administrator/getAdministrator.feature
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
}
"""
53 changes: 53 additions & 0 deletions app/features/demo/administrator/postAdministrator.feature
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]+/)"
}
"""
50 changes: 50 additions & 0 deletions app/features/demo/administrator/putAdministrator.feature
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 app/features/demo/administrator/removeAdministrator.feature
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
37 changes: 37 additions & 0 deletions app/src/Demo/Stub/AdministratorStub.php
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);
}
}
60 changes: 60 additions & 0 deletions app/src/Demo/Stub/DataFixtures/FixtureHelperTrait.php
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 app/src/Demo/Stub/DataFixtures/ORM/AdministratorFixture.php
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();
}
}
Loading
Loading