Skip to content

Commit

Permalink
[1.x] Add tests (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
imdhemy committed Oct 22, 2022
2 parents ecbb33c + ecff3ba commit 2d1d32a
Show file tree
Hide file tree
Showing 34 changed files with 678 additions and 218 deletions.
4 changes: 4 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
'php_unit_method_casing' => ['case' => 'snake_case'],
// > Strict
'declare_strict_types' => true,
// > Operator
'not_operator_with_successor_space' => true,
// > Cast Notation
'cast_spaces' => ['space' => 'none'],
])->setFinder($finder);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"ext-mongodb": "*",
"doctrine/annotations": "^1.0",
"doctrine/mongodb-odm-bundle": "^4.5",
"gesdinet/jwt-refresh-token-bundle": "^1.1",
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
return new Kernel($context['APP_ENV'], (bool)$context['APP_DEBUG']);
};
13 changes: 11 additions & 2 deletions src/Foundation/Action/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@

/**
* Base action class.
* Each use case in the application layer should extend this class.
* It extends the Symfony AbstractController to provide access to the container
* besides the use case specific methods.
*/
abstract class AbstractAction extends AbstractController implements ActionInterface
{
protected function validate(FormInterface $form, ValidatableInterface $request): void
/**
* Validates the given form and throws a ValidationException if the form is not valid.
*
* @param FormInterface $form The form to validate
* @param ValidatableInterface $request The validatable object that contains the data to validate
*/
public function validate(FormInterface $form, ValidatableInterface $request): void
{
$form->submit($request->formInput());

if (!$form->isValid()) {
if (! $form->isValid()) {
$validationException = new ValidationException();
$validationException->setErrorIterator($form->getErrors(true));

Expand Down
4 changes: 4 additions & 0 deletions src/Foundation/Exception/SiklidException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use RuntimeException;

/**
* Class SiklidException
* All application exceptions should extend this class.
*/
class SiklidException extends RuntimeException
{
}
8 changes: 7 additions & 1 deletion src/Foundation/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

/**
* Class ValidationException
* This exception should be thrown when a validation error occurs.
*
* @todo: Add tests for the validation exception.
*/
class ValidationException extends SiklidException implements RenderableInterface
{
protected ?FormErrorIterator $errorIterator = null;
Expand Down Expand Up @@ -51,7 +57,7 @@ private function formatErrorMessages(): array
$errorMessage = $error->getMessage();

/** @var string $propertyPath */
if (!isset($errors[$propertyPath])) {
if (! isset($errors[$propertyPath])) {
$errors[$propertyPath] = [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Siklid\Application\Auth\Token;
namespace App\Foundation\Security\Token;

use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Siklid\Application\Auth\Token;
namespace App\Foundation\Security\Token;

interface HasAccessToken
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

declare(strict_types=1);

namespace App\Siklid\Application\Auth\Token;
namespace App\Foundation\Security\Token;

use App\Siklid\Document\AccessToken;
use Gesdinet\JWTRefreshTokenBundle\Generator\RefreshTokenGeneratorInterface;
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* The default token manager.
*/
class TokenManager implements TokenManagerInterface
{
private JWTTokenManagerInterface $JWTTokenManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

declare(strict_types=1);

namespace App\Siklid\Application\Auth\Token;
namespace App\Foundation\Security\Token;

use Symfony\Component\Security\Core\User\UserInterface;

/**
* Token manager interface.
* All access token managers must implement this interface.
*/
interface TokenManagerInterface
{
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/Util/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Json
public function jsonToArray(string $json): array
{
try {
return (array) json_decode($json, true, 512, JSON_THROW_ON_ERROR);
return (array)json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException) {
return [];
}
Expand Down
6 changes: 3 additions & 3 deletions src/Siklid/Application/Auth/RegisterByEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace App\Siklid\Application\Auth;

use App\Foundation\Action\AbstractAction;
use App\Siklid\Application\Auth\Forms\UserType;
use App\Siklid\Application\Auth\Request\RegisterByEmailRequest as Request;
use App\Siklid\Application\Auth\Token\TokenManagerInterface;
use App\Foundation\Security\Token\TokenManagerInterface;
use App\Siklid\Document\User;
use App\Siklid\Form\UserType;
use App\Siklid\Request\Auth\RegisterByEmailRequest as Request;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface as Hash;

Expand Down
9 changes: 0 additions & 9 deletions src/Siklid/Command/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -42,14 +41,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
abstract public function handle(): int;

/**
* Creates a new output section.
*/
protected function outputSection(): ConsoleSectionOutput
{
return $this->output->section();
}

protected function info(string $message): void
{
$this->output->writeln(sprintf('<info>%s</info>', $message));
Expand Down
27 changes: 0 additions & 27 deletions src/Siklid/Command/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Siklid\Command;

use App\Siklid\Document\OAuthClient;
use App\Siklid\Document\User;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Component\Console\Attribute\AsCommand;
Expand Down Expand Up @@ -42,7 +41,6 @@ public function handle(): int
$this->info('Setup the application...');

$this->createAdminUser();
$this->createOAuthClient();

return self::SUCCESS;
}
Expand Down Expand Up @@ -70,29 +68,4 @@ private function createAdminUser(): void

$this->success('- Admin user created.');
}

private function createOAuthClient(): void
{
$oAuthClientsRepository = $this->dm->getRepository(OAuthClient::class);
$first = $oAuthClientsRepository->findOneBy([]);

if ($first) {
$this->warning('- OAuth client already exists.');

return;
}

$client = new OAuthClient();

$client->setName('Siklid.1.x');
$client->setPersonalAccessClient(true);
$client->setPasswordClient(true);
$client->setSecret($this->hasher->hash('secret'));
$client->setRedirectUri('https://localhost');

$this->dm->persist($client);
$this->dm->flush();

$this->success('- OAuth client created.');
}
}
2 changes: 1 addition & 1 deletion src/Siklid/Document/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Siklid\Document;

use App\Siklid\Application\Auth\Token\AccessTokenInterface;
use App\Foundation\Security\Token\AccessTokenInterface;
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
Expand Down
Loading

0 comments on commit 2d1d32a

Please sign in to comment.