public function it_defines_organization_choices(OptionsResolver $resolver, OrganizationRepositoryInterface $organizationRepository, OrganizationInterface $organization) { $organizationRepository->findAvailable()->willReturn([$organization]); $resolver->setNormalizer('choices', Argument::type('callable'))->willReturn($resolver); $resolver->setDefaults(['invalid_message' => 'The selected organization does not exist'])->shouldBeCalled(); $this->configureOptions($resolver); }
public function it_throws_an_exception(FactoryInterface $factory, GeneratorInterface $generator, TenantInterface $tenant, OrganizationInterface $organization, OrganizationRepositoryInterface $organizationRepository) { $organizationRepository->findOneByCode('123456')->willReturn(null); $factory->create()->shouldNotBeCalled(); $generator->generate(6)->shouldNotBeCalled(); $tenant->setCode('123456')->shouldNotBeCalled(); $tenant->setOrganization($organization)->shouldNotBeCalled(); $this->shouldThrow(\InvalidArgumentException::class)->during('createForOrganization', ['123456']); }
/** * {@inheritdoc} */ public function createForOrganization($code) { if (null === ($organization = $this->organizationRepository->findOneByCode($code))) { throw new \InvalidArgumentException(sprintf('Organization does not exist with code "%s".', $code)); } /** @var TenantInterface $tenant */ $tenant = $this->create(); $tenant->setOrganization($organization); return $tenant; }
/** * Transforms a string (code) to an object (organization). * * @param string $organizationCode * * @return OrganizationInterface|null * * @throws TransformationFailedException if object (organization) is not found */ public function reverseTransform($organizationCode) { if (null === $organizationCode || '' === $organizationCode) { return; } $organization = $this->organizationRepository->findOneByCode($organizationCode); if (null === $organization) { throw new TransformationFailedException(sprintf('An organization with code "%s" does not exist!', $organizationCode)); } return $organization; }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setNormalizer('choices', function () { /** @var OrganizationInterface[] $organizations */ $organizations = $this->organizationRepository->findAvailable(); $choices = []; foreach ($organizations as $organization) { $choices[$organization->getName()] = $organization->getCode(); } return $choices; })->setDefaults(['invalid_message' => 'The selected organization does not exist']); }
public function it_should_reverse_transform(OrganizationRepositoryInterface $organizationRepository, OrganizationInterface $organization) { $organizationRepository->findOneByCode('123abc')->shouldBeCalled()->willReturn($organization); $this->reverseTransform('123abc')->shouldReturn($organization); }