em = $this->prophesize(EntityManagerInterface::class); $this->em->persist(Argument::any())->willReturn(null); $this->em->flush()->willReturn(null); $this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class); $this->titleResolutionHelper = $this->prophesize(ShortUrlTitleResolutionHelperInterface::class); $this->service = new ShortUrlService( $this->em->reveal(), $this->urlResolver->reveal(), $this->titleResolutionHelper->reveal(), new SimpleShortUrlRelationResolver(), ); } /** * @test * @dataProvider provideAdminApiKeys */ public function listedUrlsAreReturnedFromEntityManager(?ApiKey $apiKey): void { $list = [ ShortUrl::createEmpty(), ShortUrl::createEmpty(), ShortUrl::createEmpty(), ShortUrl::createEmpty(), ]; $repo = $this->prophesize(ShortUrlRepository::class); $repo->findList(Argument::cetera())->willReturn($list)->shouldBeCalledOnce(); $repo->countList(Argument::cetera())->willReturn(count($list))->shouldBeCalledOnce(); $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $paginator = $this->service->listShortUrls(ShortUrlsParams::emptyInstance(), $apiKey); self::assertCount(4, $paginator); self::assertCount(4, $paginator->getCurrentPageResults()); } /** * @test * @dataProvider provideShortUrlEdits */ public function updateShortUrlUpdatesProvidedData( int $expectedValidateCalls, ShortUrlEdit $shortUrlEdit, ?ApiKey $apiKey, ): void { $originalLongUrl = 'originalLongUrl'; $shortUrl = ShortUrl::withLongUrl($originalLongUrl); $findShortUrl = $this->urlResolver->resolveShortUrl( ShortUrlIdentifier::fromShortCodeAndDomain('abc123'), $apiKey, )->willReturn($shortUrl); $flush = $this->em->flush()->willReturn(null); $processTitle = $this->titleResolutionHelper->processTitleAndValidateUrl($shortUrlEdit)->willReturn( $shortUrlEdit, ); $result = $this->service->updateShortUrl( ShortUrlIdentifier::fromShortCodeAndDomain('abc123'), $shortUrlEdit, $apiKey, ); self::assertSame($shortUrl, $result); self::assertEquals($shortUrlEdit->validSince(), $shortUrl->getValidSince()); self::assertEquals($shortUrlEdit->validUntil(), $shortUrl->getValidUntil()); self::assertEquals($shortUrlEdit->maxVisits(), $shortUrl->getMaxVisits()); self::assertEquals($shortUrlEdit->longUrl() ?? $originalLongUrl, $shortUrl->getLongUrl()); $findShortUrl->shouldHaveBeenCalled(); $flush->shouldHaveBeenCalled(); $processTitle->shouldHaveBeenCalledTimes($expectedValidateCalls); } public function provideShortUrlEdits(): iterable { yield 'no long URL' => [0, ShortUrlEdit::fromRawData( [ 'validSince' => Chronos::parse('2017-01-01 00:00:00')->toAtomString(), 'validUntil' => Chronos::parse('2017-01-05 00:00:00')->toAtomString(), 'maxVisits' => 5, ], ), null]; yield 'long URL' => [1, ShortUrlEdit::fromRawData( [ 'validSince' => Chronos::parse('2017-01-01 00:00:00')->toAtomString(), 'maxVisits' => 10, 'longUrl' => 'modifiedLongUrl', ], ), ApiKey::create()]; yield 'long URL with validation' => [1, ShortUrlEdit::fromRawData( [ 'longUrl' => 'modifiedLongUrl', 'validateUrl' => true, ], ), null]; } }