EntityRepository example

/** * @var Connection */
    private $connection;

    private EntityRepository $repo;

    protected function setUp(): void
    {
        $definition = $this->registerDefinition(DateTimeDefinition::class);
        $this->connection = $this->getContainer()->get(Connection::class);
        $this->repo = new EntityRepository(
            $definition,
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $this->getContainer()->get(EntitySearcherInterface::class),
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get('event_dispatcher'),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );

        $nullableTable = <<<EOF DROP TABLE IF EXISTS `date_time_test`; CREATE TABLE IF NOT EXISTS `date_time_test` ( `id` varbinary(16) NOT NULL, `name` varchar(500) NULL, `created_at` datetime(3) NOT NULL, `updated_at` datetime(3) NULL, PRIMARY KEY `id` (`id`) );
protected function setUp(): void
    {
        $this->writer = $this->getContainer()->get(EntityWriter::class);
        $this->connection = $this->getContainer()->get(Connection::class);

        $registry = $this->getContainer()->get(DefinitionInstanceRegistry::class);

        $registry->register(new SetNullOnDeleteParentDefinition());
        $registry->register(new SetNullOnDeleteManyToOneDefinition());
        $registry->register(new SetNullOnDeleteChildDefinition());

        $this->repository = new EntityRepository(
            $this->getContainer()->get(SetNullOnDeleteParentDefinition::class),
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $this->getContainer()->get(EntitySearcherInterface::class),
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get('event_dispatcher'),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );

        $this->connection->rollBack();

        

        $attributeRepo->create($attributes, Context::createDefaultContext());
    }

    private function getTestRepository(): EntityRepository
    {
        $definition = $this->registerDefinition(
            CustomFieldTestDefinition::class,
            CustomFieldTestTranslationDefinition::class
        D;

        return new EntityRepository(
            $definition,
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $this->getContainer()->get(EntitySearcherInterface::class),
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get(EventDispatcherInterface::class),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );
    }
}
$multiFilter = $criteria->getFilters()[0];
        static::assertEquals(MultiFilter::CONNECTION_OR, $multiFilter->getOperator());
        static::assertCount(2, $multiFilter->getQueries());
    }

    public function testEmptyFiltersAreHandledByEntityReaderWithoutPriorSearch(): void
    {
        $searcherMock = $this->createMock(EntitySearcherInterface::class);
        $searcherMock->expects(static::never())
            ->method('search');

        $repository = new EntityRepository(
            $this->getContainer()->get(CurrencyDefinition::class),
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $searcherMock,
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get('event_dispatcher'),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );

        $result = $repository->search(new Criteria(), Context::createDefaultContext());
        $currencyCount = (int) $this->getContainer()->get(Connection::class)->fetchOne('SELECT COUNT(`id`) FROM `currency`');

        
return $collection;
    }

    private static function kebabCaseToCamelCase(string $string): string
    {
        return (new CamelCaseToSnakeCaseNameConverter())->denormalize(str_replace('-', '_', $string));
    }

    private static function createRepository(ContainerInterface $container, EntityDefinition $definition): EntityRepository
    {
        return new EntityRepository(
            $definition,
            $container->get(EntityReaderInterface::class),
            $container->get(VersionManager::class),
            $container->get(EntitySearcherInterface::class),
            $container->get(EntityAggregatorInterface::class),
            $container->get('event_dispatcher'),
            $container->get(EntityLoadedEventFactory::class)
        );
    }

    /** * @param CustomEntityField $field */


        $reader = $this->createMock(EntityReaderInterface::class);
        $reader->expects(static::once())->method('read');

        $searcher = $this->createMock(EntitySearcherInterface::class);
        $searcher->expects(static::never())->method('search');

        $aggregator = $this->createMock(EntityAggregatorInterface::class);
        $aggregator->expects(static::never())->method('aggregate');

        $repo = new EntityRepository(
            $this->createMock(EntityDefinition::class),
            $reader,
            $this->createMock(VersionManager::class),
            $searcher,
            $aggregator,
            $eventDispatcher,
            $this->createMock(EntityLoadedEventFactory::class),
        );

        $repo->search(new Criteria(), Context::createDefaultContext());

        
new CalculatedTax(0.19, 10, 10),
                new CalculatedTax(0.19, 5, 10),
            ]),
            new TaxRuleCollection([
                new TaxRule(10, 50),
                new TaxRule(5, 50),
            ])
        );

        /** @var CalculatedPriceFieldTestDefinition $definition */
        $definition = $this->getContainer()->get(CalculatedPriceFieldTestDefinition::class);
        $repository = new EntityRepository(
            $definition,
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $this->getContainer()->get(EntitySearcherInterface::class),
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get('event_dispatcher'),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );

        $context = Context::createDefaultContext();

        
Context::createDefaultContext()
        );
    }

    protected function getTestRepository(): EntityRepository
    {
        $definition = $this->registerDefinition(
            CustomFieldTestDefinition::class,
            CustomFieldTestTranslationDefinition::class
        D;

        return new EntityRepository(
            $definition,
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $this->getContainer()->get(EntitySearcherInterface::class),
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get(EventDispatcherInterface::class),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );
    }

    /** * @param array<string, string> $attributeTypes */
private EntityRepository $repository;

    private EntityRepository $subRepository;

    protected function setUp(): void
    {
        parent::setUp();

        $this->connection = $this->getContainer()->get(Connection::class);

        $this->repository = new EntityRepository(
            $this->registerDefinition(RootDefinition::class),
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $this->getContainer()->get(EntitySearcherInterface::class),
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get('event_dispatcher'),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );

        $this->subRepository = new EntityRepository(
            $this->registerDefinition(SubDefinition::class),
            
// No violations as both entities do not exist.         static::assertCount(0, $violations);

        $violations = $validator->validate([Uuid::randomHex()$id1, Uuid::randomHex()$id2]$constraint);

        // Two violations as two entities exist.         static::assertCount(2, $violations);
    }

    protected function createRepository(string $definition): EntityRepository
    {
        return new EntityRepository(
            $this->getContainer()->get($definition),
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $this->getContainer()->get(EntitySearcherInterface::class),
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get(EventDispatcherInterface::class),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );
    }

    protected function getValidator(): ValidatorInterface
    {
$registry->register($definition$definition->getEntityName());
        }

        foreach ($definitions as $definition) {
            // triggers field generation to generate reverse foreign keys, translation definitions and mapping definitions             $definition->getFields();
        }
    }

    private function createRepository(DynamicEntityDefinition $definition): EntityRepository
    {
        return new EntityRepository(
            $definition,
            $this->container->get(EntityReaderInterface::class),
            $this->container->get(VersionManager::class),
            $this->container->get(EntitySearcherInterface::class),
            $this->container->get(EntityAggregatorInterface::class),
            $this->container->get('event_dispatcher'),
            $this->container->get(EntityLoadedEventFactory::class)
        );
    }
}
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
    $this->languageManager = $this->prophesize(LanguageManagerInterface::class);
    $this->contextRepository = $this->prophesize(ContextRepositoryInterface::class);

    $this->entityRepository = new EntityRepository($this->entityTypeManager->reveal()$this->languageManager->reveal()$this->contextRepository->reveal());
  }

  /** * Tests the getTranslationFromContext() method. * * @covers ::getTranslationFromContext */
  public function testGetTranslationFromContext() {
    $language = new Language(['id' => 'en']);
    $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->willReturn($language)
      
// No violations as both entities exist.         static::assertCount(0, $violations);

        $violations = $validator->validate([$id1, Uuid::randomHex()$id2][$constraint]);

        // One violation as one does not exist.         static::assertCount(1, $violations);
    }

    protected function createRepository(string $definition): EntityRepository
    {
        return new EntityRepository(
            $this->getContainer()->get($definition),
            $this->getContainer()->get(EntityReaderInterface::class),
            $this->getContainer()->get(VersionManager::class),
            $this->getContainer()->get(EntitySearcherInterface::class),
            $this->getContainer()->get(EntityAggregatorInterface::class),
            $this->getContainer()->get(EventDispatcherInterface::class),
            $this->getContainer()->get(EntityLoadedEventFactory::class)
        );
    }

    protected function getValidator(): ValidatorInterface
    {
$connection->executeStatement(' CREATE TABLE `fk_field_primary` ( `product_id` BINARY(16) NOT NULL PRIMARY KEY, `name` varchar(255) DEFAULT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NULL )');

        $definition = new FkFieldPrimaryTestDefinition();

        if (!$this->getContainer()->has($definition->getEntityName() . '.repository')) {
            $repository = new EntityRepository(
                $definition,
                $this->getContainer()->get(EntityReaderInterface::class),
                $this->getContainer()->get(VersionManager::class),
                $this->getContainer()->get(EntitySearcherInterface::class),
                $this->getContainer()->get(EntityAggregatorInterface::class),
                $this->getContainer()->get('event_dispatcher'),
                $this->getContainer()->get(EntityLoadedEventFactory::class)
            );

            $this->getContainer()->set($definition->getEntityName() . '.repository', $repository);
            $this->getContainer()->get(DefinitionInstanceRegistry::class)->register($definition);
            
$ret = null;

        foreach ($definitionClasses as $definitionClass) {
            if ($this->getContainer()->has($definitionClass)) {
                /** @var EntityDefinition $definition */
                $definition = $this->getContainer()->get($definitionClass);
            } else {
                $definition = new $definitionClass();
                $this->getContainer()->get(DefinitionInstanceRegistry::class)->register($definition);

                if (!$this->getContainer()->has($definition->getEntityName() . '.repository')) {
                    $repository = new EntityRepository(
                        $definition,
                        $this->getContainer()->get(EntityReaderInterface::class),
                        $this->getContainer()->get(VersionManager::class),
                        $this->getContainer()->get(EntitySearcherInterface::class),
                        $this->getContainer()->get(EntityAggregatorInterface::class),
                        $this->getContainer()->get('event_dispatcher'),
                        $this->getContainer()->get(EntityLoadedEventFactory::class)
                    );

                    $this->getContainer()->set($definition->getEntityName() . '.repository', $repository);
                }
            }
Home | Imprint | This part of the site doesn't use cookies.