public function testSubmit()
 {
     $choice = new \stdClass();
     $choice->{$identifierPath = 'id'} = $id = 1;
     $choice->{$labelPath = 'label'} = $label = 'label';
     $this->resource->expects($this->once())->method('getModel')->will($this->returnValue($model = get_class($choice)));
     $this->resource->expects($this->once())->method('getIdPropertyPath')->will($this->returnValue($identifierPath));
     $this->resource->expects($this->once())->method('getLabelPropertyPath')->will($this->returnValue($labelPath));
     $this->managerRegistry->expects($this->once())->method('getManagerForClass')->with($this->identicalTo($model))->will($this->returnValue($entityManager = $this->createEntityManagerMock()));
     $entityManager->expects($this->once())->method('getRepository')->with($this->identicalTo($model))->will($this->returnValue($repository = $this->createRepositoryMock()));
     $entityManager->expects($this->exactly(3))->method('getClassMetadata')->with($this->identicalTo($model))->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
     $classMetadata->expects($this->once())->method('getName')->will($this->returnValue($model));
     $classMetadata->expects($this->once())->method('getIdentifierFieldNames')->will($this->returnValue([$identifier = $identifierPath]));
     $classMetadata->expects($this->exactly(2))->method('getTypeOfField')->with($this->identicalTo($identifier))->will($this->returnValue('integer'));
     $repository->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock($entityManager)));
     $queryBuilder->expects($this->exactly(2))->method('getQuery')->will($this->returnValue($query = $this->createQueryMock()));
     $query->expects($this->once())->method('execute')->will($this->returnValue($choices = [$choice]));
     $queryBuilder->expects($this->once())->method('getRootAliases')->will($this->returnValue([$rootAlias = 'root_alias']));
     $queryBuilder->expects($this->once())->method('expr')->will($this->returnValue($expr = $this->createExprMock()));
     $queryBuilder->expects($this->once())->method('getEntityManager')->will($this->returnValue($entityManager));
     $queryBuilder->expects($this->once())->method('getRootEntities')->will($this->returnValue([$model]));
     $expr->expects($this->once())->method('in')->with($this->identicalTo($rootAlias . '.' . $identifier), $this->identicalTo(':' . ($parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_' . $identifier)))->will($this->returnValue($where = 'where'));
     $queryBuilder->expects($this->once())->method('andWhere')->with($this->identicalTo($where))->will($this->returnSelf());
     $query->expects($this->once())->method('setParameter')->with($this->identicalTo($parameter), $this->identicalTo([(string) $id]), $this->identicalTo(Connection::PARAM_INT_ARRAY))->will($this->returnSelf());
     $query->expects($this->once())->method('getResult')->will($this->returnValue([$choice]));
     $entityManager->expects($this->exactly(2))->method('contains')->with($this->identicalTo($choice))->will($this->returnValue(true));
     $classMetadata->expects($this->exactly(2))->method('getIdentifierValues')->with($this->identicalTo($choice))->will($this->returnValue([$id]));
     $form = $this->factory->create(ResourceChoiceType::class, null, ['resource' => $this->resource])->submit($id);
     $this->assertSame($this->resource, $form->getConfig()->getOption('resource'));
     $this->assertSame($identifierPath, $form->getConfig()->getOption('choice_value'));
     $this->assertSame($labelPath, $form->getConfig()->getOption('choice_label'));
     $this->assertSame('', $form->getConfig()->getOption('placeholder'));
     $this->assertSame($choice, $form->getData());
     $form->createView();
 }
Exemple #2
0
 public function testCreateWithoutOptions()
 {
     $this->resource->expects($this->once())->method('getModel')->will($this->returnValue($model = Fixture::class));
     $fixture = $this->factory->create();
     $this->assertInstanceOf($model, $fixture);
     $this->assertNull($fixture->getName());
 }
 public function testSubmit()
 {
     $choice = new \stdClass();
     $choice->{$identifierPath = 'id'} = $id = 1;
     $choice->{$labelPath = 'label'} = $label = 'label';
     $this->resource->expects($this->once())->method('getModel')->will($this->returnValue($model = get_class($choice)));
     $this->resource->expects($this->once())->method('getIdPropertyPath')->will($this->returnValue($identifierPath));
     $this->resource->expects($this->once())->method('getLabelPropertyPath')->will($this->returnValue($labelPath));
     $this->managerRegistry->expects($this->once())->method('getManagerForClass')->with($this->identicalTo($model))->will($this->returnValue($documentManager = $this->createDocumentManagerMock()));
     $documentManager->expects($this->once())->method('getRepository')->with($this->identicalTo($model))->will($this->returnValue($repository = $this->createRepositoryMock()));
     $documentManager->expects($this->exactly(2))->method('getClassMetadata')->with($this->identicalTo($model))->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
     $classMetadata->expects($this->once())->method('getName')->will($this->returnValue($model));
     $classMetadata->expects($this->once())->method('getIdentifierFieldNames')->will($this->returnValue([$identifier = $identifierPath]));
     $classMetadata->expects($this->once())->method('getTypeOfField')->with($this->identicalTo($identifier))->will($this->returnValue('integer'));
     $repository->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock($documentManager)));
     $queryBuilder->expects($this->once())->method('getQuery')->will($this->returnValue($query = $this->createQueryMock()));
     $query->expects($this->once())->method('execute')->will($this->returnValue($iterator = $this->createIteratorMock()));
     $iterator->expects($this->once())->method('toArray')->will($this->returnValue([$choice]));
     $form = $this->factory->create(ResourceChoiceType::class, null, ['resource' => $this->resource])->submit($id);
     $this->assertSame($this->resource, $form->getConfig()->getOption('resource'));
     $this->assertSame($identifierPath, $form->getConfig()->getOption('choice_value'));
     $this->assertSame($labelPath, $form->getConfig()->getOption('choice_label'));
     $this->assertSame('', $form->getConfig()->getOption('placeholder'));
     $this->assertSame($choice, $form->getData());
     $form->createView();
 }
Exemple #4
0
 public function testSubmit()
 {
     $this->translatableResource->expects($this->once())->method('getModel')->will($this->returnValue(TranslatableTest::class));
     $this->translatableResource->expects($this->once())->method('getRelation')->with($this->identicalTo('translation'))->will($this->returnValue($translationResource = $this->createResourceMock()));
     $translationResource->expects($this->once())->method('getForm')->will($this->returnValue(TranslationTestType::class));
     $this->translatableFactory->expects($this->once())->method('create')->will($this->returnValue(new TranslatableTest()));
     $form = $this->formFactory->create(TranslatableTestType::class)->submit(['translations' => ['en' => ['value' => 'value-en'], 'fr' => ['value' => 'value-fr']]]);
     $translatable = $form->getData();
     $view = $form->createView();
     $this->assertInstanceOf(TranslatableTest::class, $translatable);
     $this->assertCount(1, $view->children);
     $this->assertArrayHasKey('translations', $view->children);
     $this->assertCount(2, $translatable->getTranslations());
     $this->assertTrue($translatable->getTranslations()->containsKey('en'));
     $this->assertTrue($translatable->getTranslations()->containsKey('fr'));
     $this->assertCount(2, $view->children['translations']);
     $this->assertArrayHasKey('en', $view->children['translations']);
     $this->assertArrayHasKey('fr', $view->children['translations']);
     $this->assertInstanceOf(TranslationTest::class, $translatable->getTranslations()['en']);
     $this->assertSame('value-en', $translatable->getTranslations()['en']->getValue());
     $this->assertCount(1, $view->children['translations']['en']);
     $this->assertArrayHasKey('value', $view->children['translations']['en']);
     $this->assertInstanceOf(TranslationTest::class, $translatable->getTranslations()['fr']);
     $this->assertSame('value-fr', $translatable->getTranslations()['fr']->getValue());
     $this->assertCount(1, $view->children['translations']['fr']);
     $this->assertArrayHasKey('value', $view->children['translations']['fr']);
 }
 /**
  * {@inheritdoc}
  */
 public function create(array $options = [])
 {
     $translatable = parent::create($options);
     $translatable->setLocales($this->localeContext->getLocales());
     $translatable->setFallbackLocale($this->localeContext->getFallbackLocale());
     $translatable->setTranslationClass($this->resource->getTranslation()->getModel());
     return $translatable;
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $this->resource = $this->createResourceMock();
     $this->resourceFactory = $this->createFactoryMock();
     $this->resource->expects($this->any())->method('getName')->will($this->returnValue('model'));
     $this->resourceType = $this->getMockBuilder(AbstractResourceType::class)->setConstructorArgs([$this->resource, $this->resourceFactory])->getMockForAbstractClass();
     $this->formFactory = Forms::createFormFactoryBuilder()->addType(new ResourceType())->addType($this->resourceType)->getFormFactory();
 }
Exemple #7
0
 /**
  * {@inheritdoc}
  */
 public function create(array $options = [])
 {
     $class = $this->resource->getModel();
     $object = new $class();
     foreach ($options as $propertyPath => $value) {
         $this->propertyAccessor->setValue($object, $propertyPath, $value);
     }
     return $object;
 }
 public function testCreateWithoutOptions()
 {
     $this->resource->expects($this->once())->method('getModel')->will($this->returnValue($translatableClass = TranslatableTest::class));
     $this->localeContext->expects($this->once())->method('getLocales')->will($this->returnValue($locales = ['fr', 'es']));
     $this->localeContext->expects($this->once())->method('getFallbackLocale')->will($this->returnValue($fallbackLocale = 'en'));
     $translatable = $this->factory->create();
     $this->assertInstanceOf($translatableClass, $translatable);
     $this->assertSame($locales, $translatable->getLocales());
     $this->assertSame($fallbackLocale, $translatable->getFallbackLocale());
     $this->assertSame($this->translationFactory, $translatable->getTranslationFactory());
 }
Exemple #9
0
 /**
  * @param mixed       $value
  * @param string|null $alias
  * @param string|null $indexBy
  *
  * @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder
  */
 private function setUpQueryBuilder($value = false, $alias = null, $indexBy = null)
 {
     if ($alias === null) {
         $this->resource->expects($this->once())->method('getName')->will($this->returnValue($resource = 'resource'));
     } else {
         $resource = $alias;
     }
     $this->entityManager->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
     $queryBuilder->expects($this->once())->method('select')->with($this->identicalTo($resource))->will($this->returnSelf());
     $queryBuilder->expects($this->once())->method('from')->with($this->identicalTo($this->class), $this->identicalTo($resource), $this->identicalTo($indexBy))->will($this->returnSelf());
     $queryBuilder->expects($this->any())->method('getRootAliases')->will($this->returnValue([$resource]));
     if ($value === false) {
         return $queryBuilder;
     }
     $queryBuilder->expects($this->once())->method('expr')->will($this->returnValue($expr = $this->createExprMock()));
     if ($value === null) {
         $expr->expects($this->once())->method('isNull')->with($this->identicalTo($resource . '.' . ($property = 'foo')))->will($this->returnValue($expression = 'expression'));
     } else {
         $expr->expects($this->once())->method(is_string($value) ? 'eq' : 'in')->with($this->identicalTo($resource . '.' . ($property = 'foo')), $this->matchesRegularExpression('/:' . $resource . '_' . $property . '_[a-z0-9]{22}/'))->will($this->returnValue($expression = 'expression'));
         $queryBuilder->expects($this->once())->method('setParameter')->with($this->matchesRegularExpression('/' . $resource . '_' . $property . '_[a-z0-9]{22}/'), $this->identicalTo($value))->will($this->returnSelf());
     }
     $queryBuilder->expects($this->once())->method('andWhere')->with($this->identicalTo($expression))->will($this->returnSelf());
     $queryBuilder->expects($this->once())->method('addOrderBy')->with($this->identicalTo($resource . '.' . ($order = 'baz')), $this->identicalTo($sort = 'ASC'))->will($this->returnSelf());
     return $queryBuilder;
 }
 public function testFindOneBy()
 {
     $this->classMetadata->expects($this->once())->method('getFieldNames')->will($this->returnValue([$translatableField = 'translatable_field', $bothField = 'both_field']));
     $this->classMetadata->expects($this->once())->method('getAssociationMapping')->with($this->identicalTo('translations'))->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class]));
     $translationClassMetadata = $this->createClassMetadataMock();
     $translationClassMetadata->expects($this->once())->method('getFieldNames')->will($this->returnValue([$translationField = 'translation_field', $bothField]));
     $this->entityManager->expects($this->once())->method('getClassMetadata')->with($this->identicalTo($translationClass))->will($this->returnValue($translationClassMetadata));
     $this->resource->expects($this->once())->method('getName')->will($this->returnValue($name = 'resource'));
     $this->entityManager->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
     $queryBuilder->expects($this->once())->method('select')->with($this->identicalTo($name))->will($this->returnSelf());
     $queryBuilder->expects($this->once())->method('from')->with($this->identicalTo($this->class), $this->identicalTo($name), $this->isNull())->will($this->returnSelf());
     $queryBuilder->expects($this->once())->method('addSelect')->with($this->identicalTo('resource_translation'))->will($this->returnSelf());
     $queryBuilder->expects($this->exactly(5))->method('getRootAliases')->will($this->returnValue([$name]));
     $queryBuilder->expects($this->once())->method('leftJoin')->with($this->identicalTo($name . '.translations'), $this->identicalTo($translation = 'resource_translation'), $this->isNull(), $this->isNull(), $this->isNull())->will($this->returnSelf());
     $queryBuilder->expects($this->exactly(3))->method('expr')->will($this->returnValue($expr = $this->createExprMock()));
     $expr->expects($this->at(0))->method('eq')->with($this->identicalTo($name . '.' . $translatableField), $this->matchesRegularExpression('/:' . $name . '_' . $translatableField . '_[a-z0-9]{22}/'))->will($this->returnValue($translatableWhere = 'translatable_where'));
     $expr->expects($this->at(1))->method('eq')->with($this->identicalTo($translation . '.' . $translationField), $this->matchesRegularExpression('/:' . $translation . '_' . $translationField . '_[a-z0-9]{22}/'))->will($this->returnValue($translationWhere = 'translation_where'));
     $expr->expects($this->at(2))->method('eq')->with($this->identicalTo($name . '.' . $bothField), $this->matchesRegularExpression('/:' . $name . '_' . $bothField . '_[a-z0-9]{22}/'))->will($this->returnValue($bothWhere = 'both_where'));
     $queryBuilder->expects($this->exactly(3))->method('andWhere')->will($this->returnValueMap([[$translatableWhere, $queryBuilder], [$translationWhere, $queryBuilder], [$bothWhere, $queryBuilder]]));
     $queryBuilder->expects($this->at(9))->method('setParameter')->with($this->matchesRegularExpression('/' . $name . '_' . $translatableField . '_[a-z0-9]{22}/'), $this->identicalTo($translatableValue = 'translatable_value'), $this->isNull())->will($this->returnSelf());
     $queryBuilder->expects($this->at(13))->method('setParameter')->with($this->matchesRegularExpression('/' . $translation . '_' . $translationField . '_[a-z0-9]{22}/'), $this->identicalTo($translationValue = 'translation_value'), $this->isNull())->will($this->returnSelf());
     $queryBuilder->expects($this->at(17))->method('setParameter')->with($this->matchesRegularExpression('/' . $name . '_' . $bothField . '_[a-z0-9]{22}/'), $this->identicalTo($bothValue = 'both_value'), $this->isNull())->will($this->returnSelf());
     $queryBuilder->expects($this->once())->method('getQuery')->will($this->returnValue($query = $this->createQueryMock()));
     $query->expects($this->once())->method('getOneOrNullResult')->will($this->returnValue($result = 'result'));
     $this->assertSame($result, $this->translatableRepository->findOneBy([$translatableField => $translatableValue, $translationField => $translationValue, $bothField => $bothValue]));
 }
Exemple #11
0
 /**
  * @param DomainEvent     $event
  * @param string          $action
  * @param string          $state
  * @param \Exception|null $exception
  */
 private function dispatchEvent(DomainEvent $event, $action, $state, \Exception $exception = null)
 {
     if ($state === self::STATE_ERROR) {
         $event->setStopped(true);
     }
     $this->eventDispatcher->dispatch('lug.' . $this->resource->getName() . '.' . $state . '_' . $action, $event);
     if ($event->isStopped()) {
         throw new DomainException($event->getStatusCode(), $event->getMessage(), 0, $exception);
     }
 }
Exemple #12
0
 public function testFlushByPassException()
 {
     $this->resource->expects($this->exactly(2))->method('getName')->will($this->returnValue($name = 'name'));
     $this->objectManager->expects($this->once())->method('flush')->will($this->throwException(new \Exception()));
     $this->eventDispatcher->expects($this->at(0))->method('dispatch')->with($this->identicalTo('lug.' . $name . '.pre_' . ($action = 'flush')), $this->callback(function (DomainEvent $event) use($action) {
         return $event->getResource() === $this->resource && $event->getObject() === null && $event->getAction() === $action;
     }));
     $this->eventDispatcher->expects($this->at(1))->method('dispatch')->with($this->identicalTo('lug.' . $name . '.error_' . $action), $this->callback(function (DomainEvent $event) use($action) {
         $result = $event->getResource() === $this->resource && $event->getObject() === null && $event->getAction() === $action;
         $event->setStopped(false);
         return $result;
     }));
     $this->domainManager->flush();
 }
Exemple #13
0
 public function testSubmitDefaultLocale()
 {
     $this->localeProvider->expects($this->once())->method('getDefaultLocale')->will($this->returnValue($defaultLocale = $this->createLocaleMock()));
     $defaultLocale->expects($this->exactly(2))->method('getCode')->will($this->returnValue($defaultLocaleCode = 'fr'));
     $defaultLocale->expects($this->never())->method('setCode');
     $defaultLocale->expects($this->never())->method('setEnabled');
     $defaultLocale->expects($this->never())->method('setRequired');
     $this->resource->expects($this->once())->method('getModel')->will($this->returnValue(get_class($defaultLocale)));
     $this->repository->expects($this->once())->method('findAll')->will($this->returnValue([$defaultLocale]));
     $form = $this->formFactory->create(LocaleType::class, $defaultLocale)->submit(['code' => 'USD', 'enabled' => false, 'required' => false]);
     $view = $form->createView();
     $this->assertCount(4, $view->children);
     $this->assertArrayHasKey('code', $view->children);
     $this->assertArrayHasKey('enabled', $view->children);
     $this->assertArrayHasKey('required', $view->children);
     $this->assertArrayHasKey('submit', $view->children);
     $this->assertArrayHasKey('disabled', $view->children['code']->vars);
     $this->assertTrue($view->children['code']->vars['disabled']);
     $this->assertArrayHasKey('disabled', $view->children['enabled']->vars);
     $this->assertTrue($view->children['enabled']->vars['disabled']);
     $this->assertArrayHasKey('disabled', $view->children['required']->vars);
     $this->assertTrue($view->children['required']->vars['disabled']);
 }
Exemple #14
0
 /**
  * @param ResourceInterface $resource
  *
  * @return Definition
  */
 private function createDomainManagerDefinition(ResourceInterface $resource)
 {
     $definition = new Definition($resource->getDomainManager(), [new Reference('lug.resource.' . $resource->getName()), new Reference('event_dispatcher'), new Reference('lug.manager.' . $resource->getName()), new Reference('lug.repository.' . $resource->getName())]);
     $definition->addTag('lug.domain_manager', ['resource' => $resource->getName()]);
     return $definition;
 }
Exemple #15
0
 /**
  * @param ResourceInterface $resource
  *
  * @return Definition
  */
 private function createControllerDefinition(ResourceInterface $resource)
 {
     $definition = new Definition($resource->getController(), [new Reference('lug.resource.' . $resource->getName())]);
     $definition->addMethodCall('setContainer', [new Reference('service_container')]);
     $definition->addTag('lug.controller', ['resource' => $resource->getName()]);
     return $definition;
 }
Exemple #16
0
 /**
  * {@inheritdoc}
  */
 public function resolveForm(ResourceInterface $resource)
 {
     return $this->resolveParameter('form', $resource->getForm());
 }
Exemple #17
0
 /**
  * {@inheritdoc}
  */
 public function create(array $options = [])
 {
     $class = $this->resource->getModel();
     return new $class();
 }
Exemple #18
0
 /**
  * @return string
  */
 private function getAlias()
 {
     return $this->resource->getName();
 }
 /**
  * @param ResourceInterface $resource
  *
  * @return ArrayNodeDefinition
  */
 private function createDriverMappingNode(ResourceInterface $resource)
 {
     $mappingNode = $this->createNode('mapping')->addDefaultsIfNotSet();
     $mappingNode->children()->append($this->createNode('path', 'scalar', $resource->getDriverMappingPath()))->append($this->createNode('format', 'scalar', $resource->getDriverMappingFormat()));
     return $mappingNode;
 }
Exemple #20
0
 public function testCreate()
 {
     $this->resource->expects($this->once())->method('getModel')->will($this->returnValue($model = \stdClass::class));
     $this->assertInstanceOf($model, $this->factory->create());
 }
Exemple #21
0
 /**
  * @return DomainManagerInterface
  */
 protected function getDomainManager()
 {
     return $this->get('lug.resource.registry.domain_manager')[$this->resource->getName()];
 }