Skip to content

Commit

Permalink
Configure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakhar Shokel committed Apr 26, 2024
1 parent 2b39d97 commit ac56e72
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 82 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: [ master ]
branches: [ master, add-php-8-attributes-support ]
pull_request:
branches: [ master ]
branches: [ master, add-php-8-attributes-support ]

permissions:
contents: read
Expand Down
26 changes: 22 additions & 4 deletions src/DependencyInjection/PayseraApiExtension.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<?php

declare(strict_types=1);

namespace Paysera\Bundle\ApiBundle\DependencyInjection;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Persistence\ObjectRepository;
use Paysera\Bundle\ApiBundle\Service\PathAttributeResolver\DoctrinePathAttributeResolver;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Routing\AttributeRouteControllerLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\Kernel;

class PayseraApiExtension extends Extension
{
Expand All @@ -27,8 +30,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/services'));
class_exists(AttributeRouteControllerLoader::class)
? $loader->load('attributes.xml')
: $loader->load('annotations.xml')
;
: $loader->load('annotations.xml');

$container->setParameter('paysera_api.locales', $config['locales']);
if (count($config['locales']) === 0) {
Expand Down Expand Up @@ -56,6 +58,7 @@ class_exists(AttributeRouteControllerLoader::class)
}

$this->configurePagination($container, $config['pagination']);
$this->overrideDummyAnnotationRegistry($loader);
}

private function buildPathAttributeResolverDefinition(string $className, string $field): Definition
Expand Down Expand Up @@ -89,4 +92,19 @@ private function configurePagination(ContainerBuilder $container, array $paginat
$paginationConfig['maximum_limit']
);
}

private function overrideDummyAnnotationRegistry(Loader\XmlFileLoader $loader): void
{
if (Kernel::VERSION_ID < 40000 || Kernel::VERSION >= 50400) {
return;
}

// override the dummy registry when doctrine/annotations v2 is used
if (
!method_exists(AnnotationRegistry::class, 'registerLoader')
|| !method_exists(AnnotationRegistry::class, 'registerUniqueLoader')
) {
$loader->load('annotation_registry.xml');
}
}
}
9 changes: 9 additions & 0 deletions src/Resources/config/services/annotation_registry.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="annotations.dummy_registry" class="Doctrine\Common\Annotations\AnnotationRegistry"/>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Controller;
namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Controller\Attribute;

use Paysera\Bundle\ApiBundle\Attribute\RequiredPermissions;
use Symfony\Component\HttpFoundation\Response;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Controller;
namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Controller\Attribute;

use Paysera\Bundle\ApiBundle\Attribute\Body;
use Paysera\Bundle\ApiBundle\Attribute\Validation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Controller;
namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Controller\Attribute;

use Paysera\Bundle\ApiBundle\Attribute\Body;
use Paysera\Bundle\ApiBundle\Attribute\BodyContentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$prefix = Kernel::MAJOR_VERSION <= 4 ? 'legacy_' : '';
$loader->load($prefix . 'services.xml');

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>

<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="../../Controller/Attribute/" type="annotation"/>
</routes>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="../../Controller/" type="annotation"/>
<import resource="../../Controller/AnnotatedClassRequiredPermissionsController.php" type="annotation"/>
<import resource="../../Controller/AnnotatedClassValidationController.php" type="annotation"/>
<import resource="../../Controller/AnnotatedController.php" type="annotation"/>
<import resource="../../Controller/DefaultController.php" type="annotation"/>
<import resource="../../Controller/PagedQueryController.php" type="annotation"/>
<import resource="../../Controller/PersistedEntityController.php" type="annotation"/>

<route id="paysera_fixture_test_1" path="/config/by-full-controller">
<default key="_controller">PayseraFixtureTestBundle:Default:action1</default>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="../../Controller/" type="annotation"/>
<import resource="../../Controller/AnnotatedClassRequiredPermissionsController.php" type="annotation"/>
<import resource="../../Controller/AnnotatedClassValidationController.php" type="annotation"/>
<import resource="../../Controller/AnnotatedController.php" type="annotation"/>
<import resource="../../Controller/DefaultController.php" type="annotation"/>
<import resource="../../Controller/PagedQueryController.php" type="annotation"/>
<import resource="../../Controller/PersistedEntityController.php" type="annotation"/>

<route id="paysera_fixture_test_1" path="/config/by-full-controller">
<default key="_controller">Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Controller\DefaultController::action1Action</default>
Expand Down
13 changes: 13 additions & 0 deletions tests/Functional/Fixtures/FixtureTestBundle/Service/TestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Service;

class TestHelper
{
public static function phpAttributeSupportExists(): bool
{
return PHP_VERSION_ID >= 80100;
}
}
8 changes: 7 additions & 1 deletion tests/Functional/Fixtures/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Service\TestHelper;
use Paysera\Bundle\NormalizationBundle\PayseraNormalizationBundle;
use Paysera\Bundle\ApiBundle\PayseraApiBundle;
use Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\PayseraFixtureTestBundle;
Expand Down Expand Up @@ -38,7 +39,12 @@ public function registerBundles(): array

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__ . '/config/' . $this->commonFile);
$loader->load(__DIR__ . '/config/services.yml');
$loader->load(__DIR__ . '/config/' . $this->configFile);
$loader->load(__DIR__ . '/config/' . $this->commonFile);

if (TestHelper::phpAttributeSupportExists()) {
$loader->load(__DIR__ . '/config/attributed_common.yml');
}
}
}
3 changes: 3 additions & 0 deletions tests/Functional/Fixtures/config/attributed_common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
framework:
router:
resource: '%kernel.project_dir%/tests/Functional/Fixtures/config/attributed_routing.yml'
7 changes: 7 additions & 0 deletions tests/Functional/Fixtures/config/attributed_routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
paysera_fixture_test:
resource: "@PayseraFixtureTestBundle/Resources/config/routing.xml"
prefix: /

paysera_fixture_attributed_test:
resource: '@PayseraFixtureTestBundle/Resources/config/attributed_routing.xml'
prefix: /
34 changes: 0 additions & 34 deletions tests/Functional/Fixtures/config/common.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
framework:
secret: 'secret'
router:
resource: '%kernel.project_dir%/tests/Functional/Fixtures/config/routing.yml'
validation: ~

security:
enable_authenticator_manager: true
providers:
in_memory:
memory:
users:
user:
password: pass
roles: 'ROLE_USER'
admin:
password: pass
roles: ['ROLE_USER', 'ROLE_ADMIN']
firewalls:
config:
pattern: ^/(config)/
Expand All @@ -26,25 +14,3 @@ security:
password_hashers:
Symfony\Component\Security\Core\User\InMemoryUser:
algorithm: plaintext

doctrine:
orm:
auto_mapping: true
dbal:
driver: pdo_sqlite
memory: true
charset: UTF8

services:
logger:
class: Psr\Log\NullLogger # Symfony\Component\ErrorHandler\BufferingLogger
public: true
rest_registry:
alias: paysera_api.rest_request_options_registry
public: true

paysera_api:
path_attribute_resolvers:
Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Entity\PersistedEntity:
field: someField
Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Entity\SimplePersistedEntity: ~
34 changes: 0 additions & 34 deletions tests/Functional/Fixtures/config/legacy_common.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
framework:
secret: 'secret'
router:
resource: '%kernel.root_dir%/config/legacy_routing.yml'
validation: ~

security:
providers:
in_memory:
memory:
users:
user:
password: pass
roles: 'ROLE_USER'
admin:
password: pass
roles: ['ROLE_USER', 'ROLE_ADMIN']
firewalls:
main:
anonymous: true
http_basic: ~
stateless: true
encoders:
Symfony\Component\Security\Core\User\User: plaintext

doctrine:
orm:
auto_mapping: true
dbal:
driver: pdo_sqlite
memory: true
charset: UTF8

services:
logger:
class: Psr\Log\NullLogger
public: true
rest_registry:
alias: paysera_api.rest_request_options_registry
public: true

paysera_api:
path_attribute_resolvers:
Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Entity\PersistedEntity:
field: someField
Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Entity\SimplePersistedEntity: ~
37 changes: 37 additions & 0 deletions tests/Functional/Fixtures/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
framework:
secret: 'secret'
validation: ~

security:
providers:
in_memory:
memory:
users:
user:
password: pass
roles: 'ROLE_USER'
admin:
password: pass
roles: ['ROLE_USER', 'ROLE_ADMIN']

doctrine:
orm:
auto_mapping: true
dbal:
driver: pdo_sqlite
memory: true
charset: UTF8

services:
logger:
class: Psr\Log\NullLogger # Symfony\Component\ErrorHandler\BufferingLogger
public: true
rest_registry:
alias: paysera_api.rest_request_options_registry
public: true

paysera_api:
path_attribute_resolvers:
Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Entity\PersistedEntity:
field: someField
Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Entity\SimplePersistedEntity: ~
3 changes: 2 additions & 1 deletion tests/Functional/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Service\TestHelper;
use Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\TestKernel;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Routing\AttributeRouteControllerLoader;
Expand Down Expand Up @@ -116,7 +117,7 @@ protected function checkAttributeConfigurationSupport(): void
$this->markTestSkipped('Unsupported Symfony version');
}

if (PHP_VERSION_ID < 80100) {
if (!TestHelper::phpAttributeSupportExists()) {
$this->markTestSkipped('Unsupported PHP version');
}
}
Expand Down

0 comments on commit ac56e72

Please sign in to comment.