Skip to content

Commit

Permalink
add compatibility with DBAL 4 and ORM 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Feb 25, 2024
1 parent d873322 commit dbd82e1
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 127 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/test_orm_3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# TEMPORARILY USED until AuditBundle is compatible with ORM 3

name: Test

on:
pull_request:

permissions:
contents: read

jobs:
test:
name: PHP ${{ matrix.php-version }} + ${{ matrix.dependencies }} + ${{ matrix.variant }} (ORM 3)

runs-on: ubuntu-latest

continue-on-error: ${{ matrix.allowed-to-fail }}

env:
SYMFONY_REQUIRE: ${{matrix.symfony-require}}

strategy:
matrix:
include:
- php-version: '8.3'
dependencies: highest
allowed-to-fail: false
symfony-require: 7.0.*
variant: symfony/symfony:"7.0.*"
remove-audit-bundle: true

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install PHP with extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: pcov
tools: composer:v2, flex

- name: Add PHPUnit matcher
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Remove AuditBundle
if: matrix.remove-audit-bundle
run: composer remove sonata-project/entity-audit-bundle --dev --no-update

- name: Install variant
if: matrix.variant != 'normal' && !startsWith(matrix.variant, 'symfony/symfony')
run: composer require ${{ matrix.variant }} --no-update

- name: Install Composer dependencies (${{ matrix.dependencies }})
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependencies }}

- name: Run Tests with coverage
run: make coverage

- name: Send coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: build/logs/clover.xml
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"homepage": "https://docs.sonata-project.org/projects/SonataDoctrineORMAdminBundle",
"require": {
"php": "^8.0",
"doctrine/dbal": "^3.4",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/orm": "^2.14",
"doctrine/dbal": "^3.4 || ^4.0",
"doctrine/doctrine-bundle": "^2.10",
"doctrine/orm": "^2.17 || ^3.0",
"doctrine/persistence": "^3.0.2",
"sonata-project/admin-bundle": "^4.18",
"sonata-project/exporter": "^2.0 || ^3.0",
Expand Down
33 changes: 30 additions & 3 deletions src/FieldDescription/FieldDescriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace Sonata\DoctrineORMAdminBundle\FieldDescription;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry;
Expand All @@ -32,9 +34,12 @@ public function create(string $class, string $name, array $options = []): FieldD
return new FieldDescription(
$name,
$options,
$metadata->fieldMappings[$propertyName] ?? [],
$metadata->associationMappings[$propertyName] ?? [],
$parentAssociationMappings,
$this->mappingToArray($metadata->fieldMappings[$propertyName] ?? []),
$this->mappingToArray($metadata->associationMappings[$propertyName] ?? []),
array_map(
[$this, 'mappingToArray'],
$parentAssociationMappings,
),
$propertyName
);
}
Expand Down Expand Up @@ -95,4 +100,26 @@ private function getEntityManager(string $class): EntityManagerInterface

return $em;
}

/**
* @psalm-suppress UndefinedClass
* @phpstan-ignore-next-line
*/
private function mappingToArray(array|FieldMapping|AssociationMapping $mapping): array
{
if (\is_array($mapping)) {
return $mapping;
}

/**
* @psalm-suppress UndefinedClass
* @phpstan-ignore-next-line
*/
if ($mapping instanceof AssociationMapping) {
/* @phpstan-ignore-next-line */
return $mapping->toArray();
}

return (array) $mapping;
}
}
20 changes: 10 additions & 10 deletions src/Resources/views/Form/form_admin_fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ file that was distributed with this source code.

{% if sonata_admin.field_description is empty %}
{{ block('choice_widget') }}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::ONE_TO_ONE') %}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::ONE_TO_ONE') %}
{{ block('sonata_admin_orm_one_to_one_widget') }}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::MANY_TO_ONE') %}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::MANY_TO_ONE') %}
{{ block('sonata_admin_orm_many_to_one_widget') }}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::MANY_TO_MANY') %}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::MANY_TO_MANY') %}
{{ block('sonata_admin_orm_many_to_many_widget') }}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::ONE_TO_MANY') %}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::ONE_TO_MANY') %}
{{ block('sonata_admin_orm_one_to_many_widget') }}
{% else %}
{#INVALID MODE : {{ id }}#}
Expand All @@ -55,23 +55,23 @@ file that was distributed with this source code.

{% block sonata_type_admin_widget %}
{#admin {{ sonata_admin.field_description.mappingtype }}#}
{% if sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::ONE_TO_ONE') %}
{% if sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::ONE_TO_ONE') %}
{{ block('sonata_admin_orm_one_to_one_widget') }}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::MANY_TO_ONE') %}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::MANY_TO_ONE') %}
{{ block('sonata_admin_orm_many_to_one_widget') }}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::MANY_TO_MANY') %}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::MANY_TO_MANY') %}
{{ block('sonata_admin_orm_many_to_many_widget') }}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::ONE_TO_MANY') %}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::ONE_TO_MANY') %}
{{ block('sonata_admin_orm_one_to_many_widget') }}
{% else %}
INVALID MODE : {{ id }}
{% endif %}
{% endblock %}

{% block sonata_type_collection_widget %}
{% if sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::ONE_TO_MANY') %}
{% if sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::ONE_TO_MANY') %}
{{ block('sonata_admin_orm_one_to_many_widget') }}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::MANY_TO_MANY') %}
{% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadata::MANY_TO_MANY') %}
{{ block('sonata_admin_orm_many_to_many_widget') }}
{% else %}
INVALID MODE : {{ id }} - type : CollectionType - mapping : {{ sonata_admin.field_description.mappingtype }}
Expand Down
2 changes: 1 addition & 1 deletion tests/App/Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#[ORM\Entity]
class Item
{
#[ORM\Column(type: Types::DECIMAL)]
#[ORM\Column(type: Types::DECIMAL, precision: 2)]
private string $offeredPrice;

public function __construct(
Expand Down
2 changes: 1 addition & 1 deletion tests/App/Entity/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
private int $id,
#[ORM\Column(type: Types::STRING)]
private string $name = '',
#[ORM\Column(type: Types::DECIMAL)]
#[ORM\Column(type: Types::DECIMAL, precision: 2)]
private string $currentPrice = '0.0'
) {
}
Expand Down
2 changes: 2 additions & 0 deletions tests/App/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ doctrine:
orm:
auto_generate_proxy_classes: true
auto_mapping: true
report_fields_where_declared: true
validate_xml_mapping: true
mappings:
SonataORMTest:
type: attribute
Expand Down
4 changes: 4 additions & 0 deletions tests/Block/AuditBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ final class AuditBlockServiceTest extends BlockServiceTestCase

protected function setUp(): void
{
if (!class_exists(SimpleThingsAuditReader::class)) {
static::markTestSkipped('AuditBundle is not available');
}

parent::setUp();
$this->simpleThingsAuditReader = $this->createMock(SimpleThingsAuditReader::class);

Expand Down
6 changes: 3 additions & 3 deletions tests/Filter/BooleanFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function testFilterArray(): void

$filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray(['type' => null, 'value' => [BooleanType::TYPE_NO]]));

self::assertSameQuery(['WHERE alias.field IN ("0")'], $proxyQuery);
self::assertSameQuery(['WHERE alias.field IN(0)'], $proxyQuery);
static::assertTrue($filter->isActive());
}

Expand All @@ -165,7 +165,7 @@ public function testFilterArrayWithTreatNullAsFalse(): void

$filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray(['type' => null, 'value' => [BooleanType::TYPE_NO]]));

self::assertSameQuery(['WHERE alias.field IS NULL OR alias.field IN ("0")'], $proxyQuery);
self::assertSameQuery(['WHERE alias.field IS NULL OR alias.field IN(0)'], $proxyQuery);
static::assertTrue($filter->isActive());
}

Expand All @@ -181,7 +181,7 @@ public function testFilterArrayWithTreatNullAsTrue(): void

$filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray(['type' => null, 'value' => [BooleanType::TYPE_NO]]));

self::assertSameQuery(['WHERE alias.field IN ("0")'], $proxyQuery);
self::assertSameQuery(['WHERE alias.field IN(0)'], $proxyQuery);
static::assertTrue($filter->isActive());
}
}
8 changes: 4 additions & 4 deletions tests/Filter/ChoiceFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testFilterArrayEqual(): void

$filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray(['type' => EqualOperatorType::TYPE_EQUAL, 'value' => ['1', '2']]));

self::assertSameQuery(['WHERE alias.field IN :field_name_0'], $proxyQuery);
self::assertSameQuery(['WHERE alias.field IN(:field_name_0)'], $proxyQuery);
self::assertSameQueryParameters(['field_name_0' => ['1', '2']], $proxyQuery);
static::assertTrue($filter->isActive());
}
Expand All @@ -66,7 +66,7 @@ public function testFilterArrayNotEqual(): void

$filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray(['type' => EqualOperatorType::TYPE_NOT_EQUAL, 'value' => ['1', '2']]));

self::assertSameQuery(['WHERE alias.field NOT IN :field_name_0 OR alias.field IS NULL'], $proxyQuery);
self::assertSameQuery(['WHERE alias.field NOT IN(:field_name_0) OR alias.field IS NULL'], $proxyQuery);
self::assertSameQueryParameters(['field_name_0' => ['1', '2']], $proxyQuery);
static::assertTrue($filter->isActive());
}
Expand All @@ -80,7 +80,7 @@ public function testFilterArrayEqualWithNullValue(): void

$filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray(['type' => EqualOperatorType::TYPE_EQUAL, 'value' => ['1', null]]));

self::assertSameQuery(['WHERE alias.field IN :field_name_0 OR alias.field IS NULL'], $proxyQuery);
self::assertSameQuery(['WHERE alias.field IN(:field_name_0) OR alias.field IS NULL'], $proxyQuery);
self::assertSameQueryParameters(['field_name_0' => ['1', null]], $proxyQuery);
static::assertTrue($filter->isActive());
}
Expand All @@ -94,7 +94,7 @@ public function testFilterArrayNotEqualWithNullValue(): void

$filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray(['type' => EqualOperatorType::TYPE_NOT_EQUAL, 'value' => ['1', null]]));

self::assertSameQuery(['WHERE alias.field NOT IN :field_name_0'], $proxyQuery);
self::assertSameQuery(['WHERE alias.field NOT IN(:field_name_0)'], $proxyQuery);
self::assertSameQueryParameters(['field_name_0' => ['1', null]], $proxyQuery);
static::assertTrue($filter->isActive());
}
Expand Down
Loading

0 comments on commit dbd82e1

Please sign in to comment.