From 6fb4a5d6e1f8f97f421d86cb7dcbad56cc603163 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Fri, 5 Feb 2021 13:28:28 +0300 Subject: [PATCH] fix FrontApiClientAuthenticator, add phpdoc --- .gitignore | 2 + ArgumentResolver/AbstractValueResolver.php | 12 ++++++ ArgumentResolver/CallbackValueResolver.php | 12 ++++++ ArgumentResolver/ClientValueResolver.php | 13 +++++++ DependencyInjection/Configuration.php | 5 +++ .../RetailCrmServiceExtension.php | 5 +++ .../InvalidRequestArgumentException.php | 3 ++ Models/Error.php | 5 +++ Resources/doc/index.md | 0 Response/ErrorJsonResponseFactory.php | 17 ++++++++ Security/AbstractClientAuthenticator.php | 28 +++++++++++++ Security/CallbackClientAuthenticator.php | 11 ++++++ Security/FrontApiClientAuthenticator.php | 19 ++++++++- .../CallbackValueResolverTest.php | 5 +++ .../ClientValueResolverTest.php | 5 +++ Tests/DataFixtures/RequestDto.php | 5 +++ Tests/DataFixtures/User.php | 5 +++ .../DependencyInjection/ConfigurationTest.php | 39 +++++++++++++++++++ .../CallbackClientAuthenticatorTest.php | 5 +++ .../FrontApiClientAuthenticatorTest.php | 7 +++- phpunit.xml.dist | 12 ++++++ 21 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 Resources/doc/index.md diff --git a/.gitignore b/.gitignore index 5202207..7690d76 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,7 @@ composer.lock /app/phpunit.xml /phpunit.xml .phpunit.result.cache +test-report.xml # Build data /build/ @@ -54,3 +55,4 @@ composer.lock /.web-server-pid .idea +coverage.xml diff --git a/ArgumentResolver/AbstractValueResolver.php b/ArgumentResolver/AbstractValueResolver.php index 95a75e1..e840f02 100644 --- a/ArgumentResolver/AbstractValueResolver.php +++ b/ArgumentResolver/AbstractValueResolver.php @@ -5,10 +5,20 @@ namespace RetailCrm\ServiceBundle\ArgumentResolver; use RetailCrm\ServiceBundle\Exceptions\InvalidRequestArgumentException; use Symfony\Component\Validator\Validator\ValidatorInterface; +/** + * Class AbstractValueResolver + * + * @package RetailCrm\ServiceBundle\ArgumentResolver + */ abstract class AbstractValueResolver { protected $validator; + /** + * AbstractValueResolver constructor. + * + * @param ValidatorInterface $validator + */ public function __construct(ValidatorInterface $validator) { $this->validator = $validator; @@ -16,6 +26,8 @@ abstract class AbstractValueResolver /** * @param object $data + * + * @return void */ protected function validate(object $data): void { diff --git a/ArgumentResolver/CallbackValueResolver.php b/ArgumentResolver/CallbackValueResolver.php index 25ebcdc..063a4b5 100644 --- a/ArgumentResolver/CallbackValueResolver.php +++ b/ArgumentResolver/CallbackValueResolver.php @@ -9,11 +9,23 @@ use Symfony\Component\Serializer\SerializerInterface; use Generator; use Symfony\Component\Validator\Validator\ValidatorInterface; +/** + * Class CallbackValueResolver + * + * @package RetailCrm\ServiceBundle\ArgumentResolver + */ class CallbackValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface { private $serializer; private $requestSchema; + /** + * CallbackValueResolver constructor. + * + * @param SerializerInterface $serializer + * @param ValidatorInterface $validator + * @param array $requestSchema + */ public function __construct( SerializerInterface $serializer, ValidatorInterface $validator, diff --git a/ArgumentResolver/ClientValueResolver.php b/ArgumentResolver/ClientValueResolver.php index 7c21b3a..0e0f940 100644 --- a/ArgumentResolver/ClientValueResolver.php +++ b/ArgumentResolver/ClientValueResolver.php @@ -11,12 +11,25 @@ use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; use Generator; +/** + * Class ClientValueResolver + * + * @package RetailCrm\ServiceBundle\ArgumentResolver + */ class ClientValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface { private $serializer; private $denormalizer; private $requestSchema; + /** + * ClientValueResolver constructor. + * + * @param ValidatorInterface $validator + * @param SerializerInterface $serializer + * @param DenormalizerInterface $denormalizer + * @param array $requestSchema + */ public function __construct( ValidatorInterface $validator, SerializerInterface $serializer, diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 93acfc1..b2f4693 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -5,6 +5,11 @@ namespace RetailCrm\ServiceBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; +/** + * Class Configuration + * + * @package RetailCrm\ServiceBundle\DependencyInjection + */ class Configuration implements ConfigurationInterface { /** diff --git a/DependencyInjection/RetailCrmServiceExtension.php b/DependencyInjection/RetailCrmServiceExtension.php index a4af4e0..49c1b44 100644 --- a/DependencyInjection/RetailCrmServiceExtension.php +++ b/DependencyInjection/RetailCrmServiceExtension.php @@ -10,6 +10,11 @@ use RetailCrm\ServiceBundle\Security\FrontApiClientAuthenticator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; +/** + * Class RetailCrmServiceExtension + * + * @package RetailCrm\ServiceBundle\DependencyInjection + */ class RetailCrmServiceExtension extends Extension { /** diff --git a/Exceptions/InvalidRequestArgumentException.php b/Exceptions/InvalidRequestArgumentException.php index 7786dd1..4d990f1 100644 --- a/Exceptions/InvalidRequestArgumentException.php +++ b/Exceptions/InvalidRequestArgumentException.php @@ -28,6 +28,9 @@ class InvalidRequestArgumentException extends InvalidArgumentException $this->validateErrors = $errors; } + /** + * @return iterable + */ public function getValidateErrors(): iterable { return $this->validateErrors; diff --git a/Models/Error.php b/Models/Error.php index b82b62d..ef6c462 100644 --- a/Models/Error.php +++ b/Models/Error.php @@ -2,6 +2,11 @@ namespace RetailCrm\ServiceBundle\Models; +/** + * Class Error + * + * @package RetailCrm\ServiceBundle\Models + */ class Error { /** diff --git a/Resources/doc/index.md b/Resources/doc/index.md new file mode 100644 index 0000000..e69de29 diff --git a/Response/ErrorJsonResponseFactory.php b/Response/ErrorJsonResponseFactory.php index cf87306..76bbcf5 100644 --- a/Response/ErrorJsonResponseFactory.php +++ b/Response/ErrorJsonResponseFactory.php @@ -7,15 +7,32 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Serializer\SerializerInterface; +/** + * Class ErrorJsonResponseFactory + * + * @package RetailCrm\ServiceBundle\Response + */ class ErrorJsonResponseFactory { private $serializer; + /** + * ErrorJsonResponseFactory constructor. + * + * @param SerializerInterface $serializer + */ public function __construct(SerializerInterface $serializer) { $this->serializer = $serializer; } + /** + * @param Error $error + * @param int $statusCode + * @param array $headers + * + * @return Response + */ public function create(Error $error, int $statusCode = Response::HTTP_BAD_REQUEST, array $headers = []): Response { return JsonResponse::fromJsonString( diff --git a/Security/AbstractClientAuthenticator.php b/Security/AbstractClientAuthenticator.php index 3e49c90..227b19e 100644 --- a/Security/AbstractClientAuthenticator.php +++ b/Security/AbstractClientAuthenticator.php @@ -12,17 +12,30 @@ use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; +/** + * Class AbstractClientAuthenticator + * + * @package RetailCrm\ServiceBundle\Security + */ abstract class AbstractClientAuthenticator extends AbstractGuardAuthenticator { public const AUTH_FIELD = 'clientId'; private $errorResponseFactory; + /** + * AbstractClientAuthenticator constructor. + * + * @param ErrorJsonResponseFactory $errorResponseFactory + */ public function __construct(ErrorJsonResponseFactory $errorResponseFactory) { $this->errorResponseFactory = $errorResponseFactory; } + /** + * {@inheritdoc } + */ public function start(Request $request, AuthenticationException $authException = null): Response { $error = new Error(); @@ -31,21 +44,33 @@ abstract class AbstractClientAuthenticator extends AbstractGuardAuthenticator return $this->errorResponseFactory->create($error,Response::HTTP_UNAUTHORIZED); } + /** + * {@inheritdoc } + */ public function getCredentials(Request $request): string { return $request->get(static::AUTH_FIELD); } + /** + * {@inheritdoc } + */ public function getUser($credentials, UserProviderInterface $userProvider): ?UserInterface { return $userProvider->loadUserByUsername($credentials); } + /** + * {@inheritdoc } + */ public function checkCredentials($credentials, UserInterface $user): bool { return true; } + /** + * {@inheritdoc } + */ public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response { $error = new Error(); @@ -54,6 +79,9 @@ abstract class AbstractClientAuthenticator extends AbstractGuardAuthenticator return $this->errorResponseFactory->create($error,Response::HTTP_FORBIDDEN); } + /** + * {@inheritdoc } + */ public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): ?Response { return null; diff --git a/Security/CallbackClientAuthenticator.php b/Security/CallbackClientAuthenticator.php index 3b61037..698e924 100644 --- a/Security/CallbackClientAuthenticator.php +++ b/Security/CallbackClientAuthenticator.php @@ -4,13 +4,24 @@ namespace RetailCrm\ServiceBundle\Security; use Symfony\Component\HttpFoundation\Request; +/** + * Class CallbackClientAuthenticator + * + * @package RetailCrm\ServiceBundle\Security + */ class CallbackClientAuthenticator extends AbstractClientAuthenticator { + /** + * {@inheritdoc } + */ public function supports(Request $request): bool { return $request->request->has(static::AUTH_FIELD) || $request->query->has(static::AUTH_FIELD); } + /** + * {@inheritdoc } + */ public function supportsRememberMe(): bool { return false; diff --git a/Security/FrontApiClientAuthenticator.php b/Security/FrontApiClientAuthenticator.php index 19cd8e3..af4fe26 100644 --- a/Security/FrontApiClientAuthenticator.php +++ b/Security/FrontApiClientAuthenticator.php @@ -6,10 +6,21 @@ use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Security; +/** + * Class FrontApiClientAuthenticator + * + * @package RetailCrm\ServiceBundle\Security + */ class FrontApiClientAuthenticator extends AbstractClientAuthenticator { private $security; + /** + * FrontApiClientAuthenticator constructor. + * + * @param ErrorJsonResponseFactory $errorResponseFactory + * @param Security $security + */ public function __construct( ErrorJsonResponseFactory $errorResponseFactory, Security $security @@ -19,15 +30,21 @@ class FrontApiClientAuthenticator extends AbstractClientAuthenticator $this->security = $security; } + /** + * {@inheritdoc } + */ public function supports(Request $request): bool { if ($this->security->getUser()) { return false; } - return true; + return $request->request->has(static::AUTH_FIELD); } + /** + * {@inheritdoc } + */ public function supportsRememberMe(): bool { return true; diff --git a/Tests/ArgumentResolver/CallbackValueResolverTest.php b/Tests/ArgumentResolver/CallbackValueResolverTest.php index 86021be..660653d 100644 --- a/Tests/ArgumentResolver/CallbackValueResolverTest.php +++ b/Tests/ArgumentResolver/CallbackValueResolverTest.php @@ -14,6 +14,11 @@ use Symfony\Component\Serializer\Serializer; use Symfony\Component\Validator\Validation; use Generator; +/** + * Class CallbackValueResolverTest + * + * @package RetailCrm\ServiceBundle\Tests\ArgumentResolver + */ class CallbackValueResolverTest extends TestCase { private $resolver; diff --git a/Tests/ArgumentResolver/ClientValueResolverTest.php b/Tests/ArgumentResolver/ClientValueResolverTest.php index 91a28f1..0b27a2f 100644 --- a/Tests/ArgumentResolver/ClientValueResolverTest.php +++ b/Tests/ArgumentResolver/ClientValueResolverTest.php @@ -14,6 +14,11 @@ use Symfony\Component\Serializer\Serializer; use Symfony\Component\Validator\Validation; use Generator; +/** + * Class ClientValueResolverTest + * + * @package RetailCrm\ServiceBundle\Tests\ArgumentResolver + */ class ClientValueResolverTest extends TestCase { private $resolver; diff --git a/Tests/DataFixtures/RequestDto.php b/Tests/DataFixtures/RequestDto.php index 468f9a2..f6a3d84 100644 --- a/Tests/DataFixtures/RequestDto.php +++ b/Tests/DataFixtures/RequestDto.php @@ -4,6 +4,11 @@ namespace RetailCrm\ServiceBundle\Tests\DataFixtures; use Symfony\Component\Validator\Constraints as Assert; +/** + * Class RequestDto + * + * @package RetailCrm\ServiceBundle\Tests\DataFixtures + */ class RequestDto { /** diff --git a/Tests/DataFixtures/User.php b/Tests/DataFixtures/User.php index b3531eb..8951ea5 100644 --- a/Tests/DataFixtures/User.php +++ b/Tests/DataFixtures/User.php @@ -4,6 +4,11 @@ namespace RetailCrm\ServiceBundle\Tests\DataFixtures; use Symfony\Component\Security\Core\User\UserInterface; +/** + * Class User + * + * @package RetailCrm\ServiceBundle\Tests\DataFixtures + */ class User implements UserInterface { public function getRoles(): array diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 19068e1..ba97ded 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -6,6 +6,11 @@ use PHPUnit\Framework\TestCase; use RetailCrm\ServiceBundle\DependencyInjection\Configuration; use Symfony\Component\Config\Definition\Processor; +/** + * Class ConfigurationTest + * + * @package RetailCrm\ServiceBundle\Tests\DependencyInjection + */ class ConfigurationTest extends TestCase { public function testConfig(): void @@ -34,5 +39,39 @@ class ConfigurationTest extends TestCase static::assertArrayHasKey('request_schema', $config); static::assertArrayHasKey('callback', $config['request_schema']); static::assertArrayHasKey('client', $config['request_schema']); + static::assertEquals( + [ + 'type' => 'type', + 'params' => ['param'] + ], + $config['request_schema']['callback'][0] + ); + static::assertEquals( + [ + 'type1', + 'type2' + ], + $config['request_schema']['client'] + ); + } + + public function testPartConfig(): void + { + $processor = new Processor(); + + $configs = [ + [ + 'request_schema' => [ + 'client' => [ + 'type', + ] + ] + ] + ]; + + $config = $processor->processConfiguration(new Configuration(), $configs); + + static::assertArrayHasKey('client', $config['request_schema']); + static::assertEquals(['type'], $config['request_schema']['client']); } } diff --git a/Tests/Security/CallbackClientAuthenticatorTest.php b/Tests/Security/CallbackClientAuthenticatorTest.php index 2f3bed3..12befe7 100644 --- a/Tests/Security/CallbackClientAuthenticatorTest.php +++ b/Tests/Security/CallbackClientAuthenticatorTest.php @@ -11,6 +11,11 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\User\UserInterface; +/** + * Class CallbackClientAuthenticatorTest + * + * @package RetailCrm\ServiceBundle\Tests\Security + */ class CallbackClientAuthenticatorTest extends TestCase { public function testStart(): void diff --git a/Tests/Security/FrontApiClientAuthenticatorTest.php b/Tests/Security/FrontApiClientAuthenticatorTest.php index 80d8352..6ee8a4e 100644 --- a/Tests/Security/FrontApiClientAuthenticatorTest.php +++ b/Tests/Security/FrontApiClientAuthenticatorTest.php @@ -14,6 +14,11 @@ use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\User\UserInterface; +/** + * Class FrontApiClientAuthenticatorTest + * + * @package RetailCrm\ServiceBundle\Tests\Security + */ class FrontApiClientAuthenticatorTest extends TestCase { public function testStart(): void @@ -100,7 +105,7 @@ class FrontApiClientAuthenticatorTest extends TestCase $security->method('getUser')->willReturn(null); $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security); - $result = $auth->supports(new Request()); + $result = $auth->supports(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123'])); static::assertTrue($result); } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6f6d82a..8003dcf 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,9 +10,21 @@ stopOnFailure="false" bootstrap="vendor/autoload.php" > + + + ./Tests + ./vendor + + + + + ./Tests + + +