delegator = new InitialApiKeyDelegator(); $this->container = $this->prophesize(ContainerInterface::class); } /** * @test * @dataProvider provideConfigs */ public function apiKeyIsInitializedWhenAppropriate(array $config, int $expectedCalls): void { $app = $this->prophesize(Application::class)->reveal(); $apiKeyRepo = $this->prophesize(ApiKeyRepositoryInterface::class); $em = $this->prophesize(EntityManagerInterface::class); $getConfig = $this->container->get('config')->willReturn($config); $getRepo = $em->getRepository(ApiKey::class)->willReturn($apiKeyRepo->reveal()); $getEm = $this->container->get(EntityManager::class)->willReturn($em->reveal()); $result = ($this->delegator)($this->container->reveal(), '', fn () => $app); self::assertSame($result, $app); $getConfig->shouldHaveBeenCalledOnce(); $getRepo->shouldHaveBeenCalledTimes($expectedCalls); $getEm->shouldHaveBeenCalledTimes($expectedCalls); $apiKeyRepo->createInitialApiKey(Argument::any())->shouldHaveBeenCalledTimes($expectedCalls); } public function provideConfigs(): iterable { yield 'no api key' => [[], 0]; yield 'null api key' => [['initial_api_key' => null], 0]; yield 'empty api key' => [['initial_api_key' => ''], 0]; yield 'valid api key' => [['initial_api_key' => 'the_initial_key'], 1]; } }