Skip to content

Commit 5481ecb

Browse files
authored
Merge pull request #96 from lingoda/fix-ci
Fixes to make CI green
2 parents cfb9a5e + 7053b43 commit 5481ecb

16 files changed

+138
-236
lines changed

.github/workflows/test.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
matrix:
1414
install-args: ['', '--prefer-lowest']
1515
php-version: ['7.2', '7.3', '7.4', '8.0']
16+
fail-fast: false
1617
steps:
1718
# Cancel previous runs of the same branch
1819
- name: cancel
@@ -36,7 +37,7 @@ jobs:
3637
- name: composer-cache
3738
uses: actions/[email protected]
3839
with:
39-
path: ${{ needs.prepare.outputs.composercachedir }}
40+
path: ${{ steps.composercache.outputs.dir }}
4041
key: composer-${{ hashFiles('**/composer.json') }}-${{ matrix.install-args }}
4142
restore-keys: |
4243
composer-${{ hashFiles('**/composer.json') }}-${{ matrix.install-args }}
@@ -45,12 +46,11 @@ jobs:
4546
4647
- name: composer
4748
run: |
48-
composer check-platform-reqs --no-interaction
4949
composer update ${{ matrix.install-args }} --no-interaction --no-progress --prefer-dist
5050
5151
- name: phpunit
5252
run: |
53-
vendor/bin/phpunit
53+
vendor/bin/simple-phpunit --no-coverage
5454
5555
- name: phpstan-cache
5656
uses: actions/[email protected]
@@ -66,5 +66,4 @@ jobs:
6666
- name: phpstan
6767
run: |
6868
mkdir -p .phpstan-cache
69-
APP_ENV=dev bin/console cache:clear
7069
vendor/bin/phpstan analyse --no-progress --no-interaction --memory-limit=1G

Controller/GraphQL/LoginController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function login(string $userName, string $password, Request $request): Use
8282

8383
// Fire the login event manually
8484
$event = new InteractiveLoginEvent($request, $token);
85+
// @phpstan-ignore-next-line BC for Symfony4
8586
$this->eventDispatcher->dispatch($event, 'security.interactive_login');
8687

8788
return $user;

Controller/GraphqliteController.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,16 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym
106106
return new JsonResponse($result->toArray($this->debug), $httpCodeDecider->decideHttpStatusCode($result));
107107
}
108108
if (is_array($result)) {
109-
$finalResult = array_map(function (ExecutionResult $executionResult) {
109+
$finalResult = array_map(function (ExecutionResult $executionResult): array {
110110
return $executionResult->toArray($this->debug);
111111
}, $result);
112112
// Let's return the highest result.
113113
$statuses = array_map([$httpCodeDecider, 'decideHttpStatusCode'], $result);
114114
$status = empty($statuses) ? 500 : max($statuses);
115+
115116
return new JsonResponse($finalResult, $status);
116117
}
117-
if ($result instanceof Promise) {
118-
throw new RuntimeException('Only SyncPromiseAdapter is supported');
119-
}
120-
/* @phpstan-ignore-next-line */
121-
throw new RuntimeException('Unexpected response from StandardServer::executePsrRequest'); // @codeCoverageIgnore
118+
119+
throw new RuntimeException('Only SyncPromiseAdapter is supported');
122120
}
123121
}

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Symfony\Component\Cache\Psr16Cache;
1414
use TheCodingMachine\GraphQLite\Mappers\StaticClassListTypeMapper;
1515
use TheCodingMachine\GraphQLite\Mappers\StaticClassListTypeMapperFactory;
16+
use Webmozart\Assert\Assert;
1617
use function class_exists;
1718
use Doctrine\Common\Annotations\AnnotationException;
1819
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
@@ -102,14 +103,19 @@ class GraphqliteCompilerPass implements CompilerPassInterface
102103
public function process(ContainerBuilder $container): void
103104
{
104105
$reader = $this->getAnnotationReader();
105-
$this->cacheDir = $container->getParameter('kernel.cache_dir');
106+
$cacheDir = $container->getParameter('kernel.cache_dir');
107+
Assert::string($cacheDir);
108+
$this->cacheDir = $cacheDir;
106109
//$inputTypeUtils = new InputTypeUtils($reader, $namingStrategy);
107110

108111
// Let's scan the whole container and tag the services that belong to the namespace we want to inspect.
109112
$controllersNamespaces = $container->getParameter('graphqlite.namespace.controllers');
113+
Assert::isIterable($controllersNamespaces);
110114
$typesNamespaces = $container->getParameter('graphqlite.namespace.types');
115+
Assert::isIterable($typesNamespaces);
111116

112117
$firewallName = $container->getParameter('graphqlite.security.firewall_name');
118+
Assert::string($firewallName);
113119
$firewallConfigServiceName = 'security.firewall.map.config.'.$firewallName;
114120

115121
// 2 seconds of TTL in environment mode. Otherwise, let's cache forever!
@@ -183,14 +189,23 @@ public function process(ContainerBuilder $container): void
183189
if ($container->getParameter('graphqlite.security.introspection') === false) {
184190
$rulesDefinition[] = $container->findDefinition(DisableIntrospection::class);
185191
}
186-
if ($container->getParameter('graphqlite.security.maximum_query_complexity')) {
187-
$complexity = (int) $container->getParameter('graphqlite.security.maximum_query_complexity');
188-
$rulesDefinition[] = $container->findDefinition(QueryComplexity::class)->setArgument(0, $complexity);
192+
193+
$complexity = $container->getParameter('graphqlite.security.maximum_query_complexity');
194+
if ($complexity) {
195+
Assert::integerish($complexity);
196+
197+
$rulesDefinition[] = $container->findDefinition(QueryComplexity::class)
198+
->setArgument(0, (int) $complexity);
189199
}
190-
if ($container->getParameter('graphqlite.security.maximum_query_depth')) {
191-
$depth = (int) $container->getParameter('graphqlite.security.maximum_query_depth');
192-
$rulesDefinition[] = $container->findDefinition(QueryDepth::class)->setArgument(0, $depth);
200+
201+
$depth = $container->getParameter('graphqlite.security.maximum_query_depth');
202+
if ($depth) {
203+
Assert::integerish($depth);
204+
205+
$rulesDefinition[] = $container->findDefinition(QueryDepth::class)
206+
->setArgument(0, (int) $depth);
193207
}
208+
194209
$serverConfigDefinition->addMethodCall('setValidationRules', [$rulesDefinition]);
195210

196211
if ($disableMe === false) {
@@ -332,7 +347,7 @@ private function mapAdderToTag(string $tag, string $methodName, ContainerBuilder
332347
*/
333348
private function makePublicInjectedServices(ReflectionClass $refClass, AnnotationReader $reader, ContainerBuilder $container, bool $isController): void
334349
{
335-
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container, $isController) {
350+
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container, $isController): array {
336351
$services = [];
337352
foreach ($refClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
338353
$field = $reader->getRequestAnnotation($method, Field::class) ?? $reader->getRequestAnnotation($method, Query::class) ?? $reader->getRequestAnnotation($method, Mutation::class);
@@ -346,6 +361,7 @@ private function makePublicInjectedServices(ReflectionClass $refClass, Annotatio
346361
}
347362
}
348363
}
364+
349365
return $services;
350366
});
351367

@@ -484,7 +500,7 @@ private function getClassList(string $namespace, int $globTtl = 2, bool $recursi
484500
// The autoloader might trigger errors if the file does not respect PSR-4 or if the
485501
// Symfony DebugAutoLoader is installed. (see https://github.com/thecodingmachine/graphqlite/issues/216)
486502
require_once $phpFile;
487-
// Does it exists now?
503+
// @phpstan-ignore-next-line Does it exist now?
488504
if (! class_exists($className, false)) {
489505
continue;
490506
}

DependencyInjection/GraphqliteExtension.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ public function load(array $configs, ContainerBuilder $container): void
4242
if (!is_array($controllers)) {
4343
$controllers = [ $controllers ];
4444
}
45-
$namespaceController = array_map(function($namespace) { return rtrim($namespace, '\\') . '\\'; }, $controllers);
45+
$namespaceController = array_map(
46+
function($namespace): string {
47+
return rtrim($namespace, '\\') . '\\';
48+
},
49+
$controllers
50+
);
4651
} else {
4752
$namespaceController = [];
4853
}
@@ -51,7 +56,12 @@ public function load(array $configs, ContainerBuilder $container): void
5156
if (!is_array($types)) {
5257
$types = [ $types ];
5358
}
54-
$namespaceType = array_map(function($namespace) { return rtrim($namespace, '\\') . '\\'; }, $types);
59+
$namespaceType = array_map(
60+
function($namespace): string {
61+
return rtrim($namespace, '\\') . '\\';
62+
},
63+
$types
64+
);
5565
} else {
5666
$namespaceType = [];
5767
}
@@ -84,20 +94,6 @@ public function load(array $configs, ContainerBuilder $container): void
8494
->addTag('graphql.root_type_mapper_factory');
8595
}
8696

87-
private function getNamespaceDir(string $namespace): string
88-
{
89-
$classNameMapper = ClassNameMapper::createFromComposerFile(null, null, true);
90-
91-
$possibleFileNames = $classNameMapper->getPossibleFileNames($namespace.'Xxx');
92-
if (count($possibleFileNames) > 1) {
93-
throw new \RuntimeException(sprintf('According to your composer.json, classes belonging to the "%s" namespace can be located in several directories: %s. This is an issue for the GraphQLite lib. Please make sure that a namespace can only be resolved to one PHP file.', $namespace, implode(", ", $possibleFileNames)));
94-
} elseif (empty($possibleFileNames)) {
95-
throw new \RuntimeException(sprintf('Files in namespace "%s" cannot be autoloaded by Composer. Please set up a PSR-4 autoloader in Composer or change the namespace configured in "graphqlite.namespace.controllers" and "graphqlite.namespace.types"', $namespace));
96-
}
97-
98-
return substr($possibleFileNames[0], 0, -8);
99-
}
100-
10197
/**
10298
* @param array<string, int> $debug
10399
* @return int

DependencyInjection/OverblogGraphiQLEndpointWiringPass.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace TheCodingMachine\Graphqlite\Bundle\DependencyInjection;
44

5-
use Overblog\GraphiQLBundle\Config\GraphiQLControllerEndpoint;
6-
use Overblog\GraphiQLBundle\Config\GraphqlEndpoint\RootResolver;
75
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
86
use Symfony\Component\DependencyInjection\ContainerBuilder;
97
use Symfony\Component\DependencyInjection\Definition;

GraphiQL/EndpointResolver.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
namespace TheCodingMachine\Graphqlite\Bundle\GraphiQL;
44

55
use Overblog\GraphiQLBundle\Config\GraphiQLControllerEndpoint;
6+
use Overblog\GraphiQLBundle\Config\GraphQLEndpoint\GraphQLEndpointInvalidSchemaException;
67
use Symfony\Component\HttpFoundation\RequestStack;
8+
use Webmozart\Assert\Assert;
79

810
final class EndpointResolver implements GraphiQLControllerEndpoint
911
{
12+
/**
13+
* @var RequestStack
14+
*/
1015
protected $requestStack;
1116

1217
public function __construct(RequestStack $requestStack)
@@ -18,6 +23,7 @@ public function getBySchema($name)
1823
{
1924
if ('default' === $name) {
2025
$request = $this->requestStack->getCurrentRequest();
26+
Assert::notNull($request);
2127

2228
return $request->getBaseUrl().'/graphql';
2329
}

Mappers/RequestParameterMiddleware.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use phpDocumentor\Reflection\DocBlock;
88
use phpDocumentor\Reflection\Type;
9+
use ReflectionNamedType;
910
use ReflectionParameter;
1011
use Symfony\Component\HttpFoundation\Request;
1112
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotations;
@@ -15,13 +16,13 @@
1516

1617
class RequestParameterMiddleware implements ParameterMiddlewareInterface
1718
{
18-
1919
public function mapParameter(ReflectionParameter $parameter, DocBlock $docBlock, ?Type $paramTagType, ParameterAnnotations $parameterAnnotations, ParameterHandlerInterface $next): ParameterInterface
2020
{
2121
$parameterType = $parameter->getType();
22-
if ($parameterType && $parameterType->getName() === Request::class) {
22+
if ($parameterType instanceof ReflectionNamedType && $parameterType->getName() === Request::class) {
2323
return new RequestParameter();
2424
}
25+
2526
return $next->mapParameter($parameter, $docBlock, $paramTagType, $parameterAnnotations);
2627
}
2728
}

Server/ServerConfig.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
namespace TheCodingMachine\Graphqlite\Bundle\Server;
55

66
use GraphQL\Error\InvariantViolation;
7+
use GraphQL\GraphQL;
8+
use GraphQL\Language\AST\DocumentNode;
9+
use GraphQL\Server\OperationParams;
710
use GraphQL\Utils\Utils;
811
use GraphQL\Validator\DocumentValidator;
912
use GraphQL\Validator\Rules\ValidationRule;
@@ -27,7 +30,15 @@ class ServerConfig extends \GraphQL\Server\ServerConfig
2730
*/
2831
public function setValidationRules($validationRules)
2932
{
30-
parent::setValidationRules(array_merge(DocumentValidator::defaultRules(), $validationRules));
33+
parent::setValidationRules(
34+
function (OperationParams $params, DocumentNode $doc, string $operationType) use ($validationRules): array {
35+
$validationRules = is_callable($validationRules)
36+
? $validationRules($params, $doc, $operationType)
37+
: $validationRules;
38+
39+
return array_merge(DocumentValidator::defaultRules(), $validationRules);
40+
}
41+
);
3142

3243
return $this;
3344
}

Tests/FunctionalTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ private function logIn(ContainerInterface $container)
534534
$container->get('security.token_storage')->setToken($token);
535535
}
536536

537+
/**
538+
* @requires PHP 8.0
539+
*/
537540
public function testPhp8Attributes(): void
538541
{
539542
$kernel = new GraphqliteTestingKernel();

Tests/NoSecurityBundleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function testServiceWiring(): void
2929
$kernel = new GraphqliteTestingKernel(true, null, false, null, true, null, null, ['TheCodingMachine\\Graphqlite\\Bundle\\Tests\\NoSecurityBundleFixtures\\Controller\\']);
3030
$kernel->boot();
3131
$container = $kernel->getContainer();
32+
self::assertNotNull($container);
3233

3334
$schema = $container->get(Schema::class);
3435
$this->assertInstanceOf(Schema::class, $schema);

Types/SymfonyUserInterfaceType.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,36 @@
33

44
namespace TheCodingMachine\Graphqlite\Bundle\Types;
55

6+
use Symfony\Component\Security\Core\Role\Role;
67
use TheCodingMachine\GraphQLite\Annotations\Field;
78
use TheCodingMachine\GraphQLite\Annotations\SourceField;
89
use TheCodingMachine\GraphQLite\Annotations\Type;
910
use Symfony\Component\Security\Core\User\UserInterface;
11+
use TheCodingMachine\GraphQLite\FieldNotFoundException;
1012

1113
/**
1214
* @Type(class=UserInterface::class)
13-
* @SourceField(name="userName")
1415
*/
1516
class SymfonyUserInterfaceType
1617
{
18+
/**
19+
* @Field
20+
*/
21+
public function getUserName(UserInterface $user): string
22+
{
23+
// @phpstan-ignore-next-line Forward Compatibility for Symfony >=5.3
24+
if (method_exists($user, 'getUserIdentifier')) {
25+
return $user->getUserIdentifier();
26+
}
27+
28+
// @phpstan-ignore-next-line Backward Compatibility for Symfony <5.3
29+
if (method_exists($user, 'getUsername')) {
30+
return $user->getUsername();
31+
}
32+
33+
throw FieldNotFoundException::missingField(UserInterface::class, 'userName');
34+
}
35+
1736
/**
1837
* @Field()
1938
* @return string[]
@@ -22,8 +41,13 @@ public function getRoles(UserInterface $user): array
2241
{
2342
$roles = [];
2443
foreach ($user->getRoles() as $role) {
25-
$roles[] = (string) $role;
44+
// @phpstan-ignore-next-line BC for Symfony 4
45+
if (class_exists(Role::class) && $role instanceof Role) {
46+
$role = $role->getRole();
47+
}
48+
49+
$roles[] = $role;
2650
}
2751
return $roles;
2852
}
29-
}
53+
}

0 commit comments

Comments
 (0)