<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\DoctrineORMAdminBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddAuditEntityCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

final class AddAuditEntityCompilerPassTest extends TestCase
{
    /**
     * @phpstan-return iterable<array-key, array{bool, array<string, array{audit: bool|null, audited: bool}>}>
     */
    public function processDataProvider(): iterable
    {
        return [
            [true, [
                'admin1' => ['audit' => null,  'audited' => true],
                'admin2' => ['audit' => true,  'audited' => true],
                'admin3' => ['audit' => false, 'audited' => false],
            ]],
            [false, [
                'admin1' => ['audit' => null,  'audited' => false],
                'admin2' => ['audit' => true,  'audited' => true],
                'admin3' => ['audit' => false, 'audited' => false],
            ]],
        ];
    }

    /**
     * @phpstan-param array<string, array{audit: bool|null, audited: bool}> $services
     *
     * @dataProvider processDataProvider
     */
    public function testProcess(bool $force, array $services): void
    {
        $container = $this->createMock(ContainerBuilder::class);

        $container
            ->expects(static::any())
            ->method('hasDefinition')
            ->willReturnCallback(static function (string $id): bool {
                return 'simplethings_entityaudit.config' === $id;
            });

        $container
            ->expects(static::any())
            ->method('getParameter')
            ->willReturnCallback(static function (string $id) use ($force) {
                if ('sonata_doctrine_orm_admin.audit.force' === $id) {
                    return $force;
                }

                if ('simplethings.entityaudit.audited_entities' === $id) {
                    return [];
                }

                return null;
            });

        $container
            ->expects(static::any())
            ->method('findTaggedServiceIds')
            ->willReturnCallback(static function (string $id) use ($services): array {
                if ('sonata.admin' !== $id) {
                    return [];
                }

                $tags = [];
                foreach ($services as $serviceId => $service) {
                    $attributes = ['manager_type' => 'orm'];

                    $audit = $service['audit'];
                    if (null !== $audit) {
                        $attributes['audit'] = $audit;
                    }

                    $tags[$serviceId] = [0 => $attributes];
                }

                return $tags;
            });

        $container
            ->expects(static::any())
            ->method('getDefinition')
            ->willReturnCallback(static function (string $id): Definition {
                return new Definition(null, [null, $id]);
            });

        $expectedAuditedEntities = [];

        foreach ($services as $id => $service) {
            if ($service['audited']) {
                $expectedAuditedEntities[] = $id;
            }
        }

        $container
            ->expects(static::once())
            ->method('setParameter')
            ->with('simplethings.entityaudit.audited_entities', $expectedAuditedEntities);

        $compilerPass = new AddAuditEntityCompilerPass();
        $compilerPass->process($container);
    }
}
<commit_msg>Add test case for not defined audit configuration<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\DoctrineORMAdminBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddAuditEntityCompilerPass;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Product;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\SimpleEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\UuidEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\VersionedEntity;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

final class AddAuditEntityCompilerPassTest extends TestCase
{
    /**
     * @phpstan-return iterable<array-key, array{bool, array<string, array{audit?: bool|null, class: class-string}>, class-string[]}>
     */
    public function processDataProvider(): iterable
    {
        return [
            [
                true,
                [
                    'admin1' => ['audit' => null,  'class' => Product::class],
                    'admin2' => ['audit' => true,  'class' => SimpleEntity::class],
                    'admin3' => ['audit' => false, 'class' => UuidEntity::class],
                    'admin4' => ['class' => VersionedEntity::class],
                ],
                [
                    Product::class,
                    SimpleEntity::class,
                    VersionedEntity::class,
                ],
            ],
            [
                false,
                [
                    'admin1' => ['audit' => null,  'class' => Product::class],
                    'admin2' => ['audit' => true,  'class' => SimpleEntity::class],
                    'admin3' => ['audit' => false, 'class' => UuidEntity::class],
                    'admin4' => ['class' => VersionedEntity::class],
                ],
                [
                    SimpleEntity::class,
                ],
            ],
        ];
    }

    /**
     * @phpstan-param array<string, array{audit?: bool|null, class: class-string}> $services
     * @phpstan-param class-string[] $expectedAuditedEntities
     *
     * @dataProvider processDataProvider
     */
    public function testProcess(bool $force, array $services, array $expectedAuditedEntities): void
    {
        $container = new ContainerBuilder();
        $container->setDefinition('simplethings_entityaudit.config', new Definition());
        $container->setDefinition('sonata.admin.audit.manager', new Definition());

        $container->setParameter('sonata_doctrine_orm_admin.audit.force', $force);
        $container->setParameter('simplethings.entityaudit.audited_entities', []);

        foreach ($services as $serviceId => $service) {
            $definition = new Definition();

            $attributes = [
                'manager_type' => 'orm',
                'model_class' => $service['class'],
            ];

            if (isset($service['audit'])) {
                $attributes['audit'] = $service['audit'];
            }

            $definition->addTag('sonata.admin', $attributes);

            $container->setDefinition($serviceId, $definition);
        }

        $compilerPass = new AddAuditEntityCompilerPass();
        $compilerPass->process($container);

        static::assertSame($expectedAuditedEntities, $container->getParameter('simplethings.entityaudit.audited_entities'));
    }
}

{'fraction_non_alphanumeric': 0.13480457325179473, 'fraction_numerical': 0.002260037224142515, 'mean_word_length': 2.076891615541922, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1374090', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1968, 'n_words': 388}