ui = $this->getMockBuilder(CronUIInterface::class)->disableOriginalConstructor()->getMock(); $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock(); $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock(); $this->helloAssoService = $this->getMockBuilder(HelloAssoService::class)->disableOriginalConstructor()->getMock(); } private function getRefreshHelloassoTokensMockFor(string $methodName): TestableRefreshHelloassoTokens|MockObject { $refreshHelloassoTokens = $this->getMockBuilder(TestableRefreshHelloassoTokens::class) ->setConstructorArgs([$this->entityManager, $this->helloAssoService]) ->setMethodsExcept([$methodName, 'setUI', 'setLoggerInterface']) ->getMock(); $refreshHelloassoTokens->setUI($this->ui); $refreshHelloassoTokens->setLoggerInterface($this->logger); return $refreshHelloassoTokens; } /** * @see RefreshHelloassoTokens::preview() */ public function testPreview(): void { $refreshHelloassoTokens = $this->getRefreshHelloassoTokensMockFor('preview'); $organization1 = $this->getMockBuilder(Organization::class)->getMock(); $organization1->method('getId')->willReturn(1); $organization2 = $this->getMockBuilder(Organization::class)->getMock(); $organization2->method('getId')->willReturn(2); $helloAsso1 = $this->getMockBuilder(HelloAsso::class)->getMock(); $helloAsso1->method('getOrganization')->willReturn($organization1); $helloAsso1->method('getRefreshTokenCreatedAt')->willReturn(new \DateTime('2023-01-01 10:00:00')); $helloAsso2 = $this->getMockBuilder(HelloAsso::class)->getMock(); $helloAsso2->method('getOrganization')->willReturn($organization2); $helloAsso2->method('getRefreshTokenCreatedAt')->willReturn(new \DateTime('2023-01-02 15:30:00')); $helloAssoEntities = [$helloAsso1, $helloAsso2]; $refreshHelloassoTokens->expects(self::once()) ->method('getHelloassoAccounts') ->willReturn($helloAssoEntities); $this->ui->expects(self::exactly(3)) ->method('print') ->withConsecutive( ['Tokens to refresh :'], [' * Organization 1 : 2023-01-01 10:00:00'], [' * Organization 2 : 2023-01-02 15:30:00'] ); $refreshHelloassoTokens->preview(); } /** * @see RefreshHelloassoTokens::preview() */ public function testPreviewWhenNoTokens(): void { $refreshHelloassoTokens = $this->getRefreshHelloassoTokensMockFor('preview'); $refreshHelloassoTokens->expects(self::once()) ->method('getHelloassoAccounts') ->willReturn([]); $this->ui->expects(self::once()) ->method('print') ->with('No tokens to refresh'); $refreshHelloassoTokens->preview(); } /** * @see RefreshHelloassoTokens::execute() */ public function testExecute(): void { $refreshHelloassoTokens = $this->getRefreshHelloassoTokensMockFor('execute'); $organization1 = $this->getMockBuilder(Organization::class)->getMock(); $organization1->method('getId')->willReturn(1); $organization2 = $this->getMockBuilder(Organization::class)->getMock(); $organization2->method('getId')->willReturn(2); $helloAsso1 = $this->getMockBuilder(HelloAsso::class)->getMock(); $helloAsso1->method('getOrganization')->willReturn($organization1); $helloAsso1->method('getRefreshTokenCreatedAt')->willReturn(new \DateTime('2023-01-01 10:00:00')); $helloAsso2 = $this->getMockBuilder(HelloAsso::class)->getMock(); $helloAsso2->method('getOrganization')->willReturn($organization2); $helloAsso2->method('getRefreshTokenCreatedAt')->willReturn(new \DateTime('2023-01-02 15:30:00')); $helloAssoEntities = [$helloAsso1, $helloAsso2]; $refreshHelloassoTokens->expects(self::once()) ->method('getHelloassoAccounts') ->willReturn($helloAssoEntities); $this->helloAssoService->expects(self::exactly(2)) ->method('refreshTokens') ->withConsecutive( [$helloAsso1], [$helloAsso2] ); $this->logger->expects(self::exactly(4)) ->method('info') ->withConsecutive( ['2 tokens to refresh'], [' * Refresh token for organization 1 : 2023-01-01 10:00:00'], [' * Refresh token for organization 2 : 2023-01-02 15:30:00'], ['Tokens refreshed'] ); $refreshHelloassoTokens->execute(); } /** * @see RefreshHelloassoTokens::execute() */ public function testExecuteWhenNoTokens(): void { $refreshHelloassoTokens = $this->getRefreshHelloassoTokensMockFor('execute'); $refreshHelloassoTokens->expects(self::once()) ->method('getHelloassoAccounts') ->willReturn([]); $this->logger->expects(self::once()) ->method('info') ->with('No tokens to refresh'); $this->helloAssoService->expects(self::never()) ->method('refreshTokens'); $refreshHelloassoTokens->execute(); } /** * @see RefreshHelloassoTokens::execute() */ public function testExecuteWithCallsLimit(): void { $refreshHelloassoTokens = $this->getRefreshHelloassoTokensMockFor('execute'); $helloAssoEntities = []; // Create 15 entities to test the API calls limit (limit is 10) for ($i = 1; $i <= 15; $i++) { $organization = $this->getMockBuilder(Organization::class)->getMock(); $organization->method('getId')->willReturn($i); $helloAsso = $this->getMockBuilder(HelloAsso::class)->getMock(); $helloAsso->method('getOrganization')->willReturn($organization); $helloAsso->method('getRefreshTokenCreatedAt')->willReturn(new \DateTime("2023-01-" . str_pad($i, 2, '0', STR_PAD_LEFT) . " 10:00:00")); $helloAssoEntities[] = $helloAsso; } $refreshHelloassoTokens->expects(self::once()) ->method('getHelloassoAccounts') ->willReturn($helloAssoEntities); // Should only process the first 10 due to API limit $this->helloAssoService->expects(self::exactly(10)) ->method('refreshTokens'); $this->logger->expects(self::exactly(11)) ->method('info'); $this->logger->expects(self::once()) ->method('warning') ->with('API calls limit reached, aborting'); $refreshHelloassoTokens->execute(); } /** * @see RefreshHelloassoTokens::execute() */ public function testExecuteWithExactCallsLimit(): void { $refreshHelloassoTokens = $this->getRefreshHelloassoTokensMockFor('execute'); $helloAssoEntities = []; // Create exactly 10 entities (the limit) for ($i = 1; $i <= 10; $i++) { $organization = $this->getMockBuilder(Organization::class)->getMock(); $organization->method('getId')->willReturn($i); $helloAsso = $this->getMockBuilder(HelloAsso::class)->getMock(); $helloAsso->method('getOrganization')->willReturn($organization); $helloAsso->method('getRefreshTokenCreatedAt')->willReturn(new \DateTime("2023-01-" . str_pad($i, 2, '0', STR_PAD_LEFT) . " 10:00:00")); $helloAssoEntities[] = $helloAsso; } $refreshHelloassoTokens->expects(self::once()) ->method('getHelloassoAccounts') ->willReturn($helloAssoEntities); $this->helloAssoService->expects(self::exactly(10)) ->method('refreshTokens'); $this->logger->expects(self::exactly(11)) ->method('info'); $this->logger->expects(self::never()) ->method('warning'); $refreshHelloassoTokens->execute(); } /** * @see RefreshHelloassoTokens::getHelloassoAccounts() */ public function testGetHelloassoAccounts(): void { $refreshHelloassoTokens = $this->getRefreshHelloassoTokensMockFor('getHelloassoAccounts'); $helloAssoRepository = $this->getMockBuilder(HelloAssoRepository::class) ->disableOriginalConstructor() ->getMock(); $helloAsso1 = $this->getMockBuilder(HelloAsso::class)->getMock(); $helloAsso2 = $this->getMockBuilder(HelloAsso::class)->getMock(); $expectedResult = [$helloAsso1, $helloAsso2]; $this->entityManager->expects(self::once()) ->method('getRepository') ->with(HelloAsso::class) ->willReturn($helloAssoRepository); $helloAssoRepository->expects(self::once()) ->method('findOldRefreshTokens') ->with(24) ->willReturn($expectedResult); $result = $refreshHelloassoTokens->getHelloassoAccounts(); $this->assertEquals($expectedResult, $result); } }