From 7c9f572eb1d9eb8c948ac82743121dec9f9cdc70 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 7 Nov 2020 09:49:09 +0100 Subject: [PATCH] Deleted old domain resolvers and added tests for new short url relation resolvers --- .../Resolver/DomainResolverInterface.php | 12 --- .../Resolver/PersistenceDomainResolver.php | 29 ----- .../Domain/Resolver/SimpleDomainResolver.php | 15 --- .../PersistenceDomainResolverTest.php | 65 ------------ .../Resolver/SimpleDomainResolverTest.php | 41 ------- ...ersistenceShortUrlRelationResolverTest.php | 100 ++++++++++++++++++ .../SimpleShortUrlRelationResolverTest.php | 56 ++++++++++ 7 files changed, 156 insertions(+), 162 deletions(-) delete mode 100644 module/Core/src/Domain/Resolver/DomainResolverInterface.php delete mode 100644 module/Core/src/Domain/Resolver/PersistenceDomainResolver.php delete mode 100644 module/Core/src/Domain/Resolver/SimpleDomainResolver.php delete mode 100644 module/Core/test/Domain/Resolver/PersistenceDomainResolverTest.php delete mode 100644 module/Core/test/Domain/Resolver/SimpleDomainResolverTest.php create mode 100644 module/Core/test/ShortUrl/Resolver/PersistenceShortUrlRelationResolverTest.php create mode 100644 module/Core/test/ShortUrl/Resolver/SimpleShortUrlRelationResolverTest.php diff --git a/module/Core/src/Domain/Resolver/DomainResolverInterface.php b/module/Core/src/Domain/Resolver/DomainResolverInterface.php deleted file mode 100644 index c6ff55ff..00000000 --- a/module/Core/src/Domain/Resolver/DomainResolverInterface.php +++ /dev/null @@ -1,12 +0,0 @@ -em = $em; - } - - public function resolveDomain(?string $domain): ?Domain - { - if ($domain === null) { - return null; - } - - /** @var Domain|null $existingDomain */ - $existingDomain = $this->em->getRepository(Domain::class)->findOneBy(['authority' => $domain]); - return $existingDomain ?? new Domain($domain); - } -} diff --git a/module/Core/src/Domain/Resolver/SimpleDomainResolver.php b/module/Core/src/Domain/Resolver/SimpleDomainResolver.php deleted file mode 100644 index b0c0dd58..00000000 --- a/module/Core/src/Domain/Resolver/SimpleDomainResolver.php +++ /dev/null @@ -1,15 +0,0 @@ -em = $this->prophesize(EntityManagerInterface::class); - $this->domainResolver = new PersistenceDomainResolver($this->em->reveal()); - } - - /** @test */ - public function returnsEmptyWhenNoDomainIsProvided(): void - { - $getRepository = $this->em->getRepository(Domain::class); - - self::assertNull($this->domainResolver->resolveDomain(null)); - $getRepository->shouldNotHaveBeenCalled(); - } - - /** - * @test - * @dataProvider provideFoundDomains - */ - public function findsOrCreatesDomainWhenValueIsProvided(?Domain $foundDomain, string $authority): void - { - $repo = $this->prophesize(ObjectRepository::class); - $findDomain = $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain); - $getRepository = $this->em->getRepository(Domain::class)->willReturn($repo->reveal()); - - $result = $this->domainResolver->resolveDomain($authority); - - if ($foundDomain !== null) { - self::assertSame($result, $foundDomain); - } - self::assertInstanceOf(Domain::class, $result); - self::assertEquals($authority, $result->getAuthority()); - $findDomain->shouldHaveBeenCalledOnce(); - $getRepository->shouldHaveBeenCalledOnce(); - } - - public function provideFoundDomains(): iterable - { - $authority = 'doma.in'; - - yield 'without found domain' => [null, $authority]; - yield 'with found domain' => [new Domain($authority), $authority]; - } -} diff --git a/module/Core/test/Domain/Resolver/SimpleDomainResolverTest.php b/module/Core/test/Domain/Resolver/SimpleDomainResolverTest.php deleted file mode 100644 index ff5b6b90..00000000 --- a/module/Core/test/Domain/Resolver/SimpleDomainResolverTest.php +++ /dev/null @@ -1,41 +0,0 @@ -domainResolver = new SimpleDomainResolver(); - } - - /** - * @test - * @dataProvider provideDomains - */ - public function resolvesExpectedDomain(?string $domain): void - { - $result = $this->domainResolver->resolveDomain($domain); - - if ($domain === null) { - self::assertNull($result); - } else { - self::assertInstanceOf(Domain::class, $result); - self::assertEquals($domain, $result->getAuthority()); - } - } - - public function provideDomains(): iterable - { - yield 'with empty domain' => [null]; - yield 'with non-empty domain' => ['domain.com']; - } -} diff --git a/module/Core/test/ShortUrl/Resolver/PersistenceShortUrlRelationResolverTest.php b/module/Core/test/ShortUrl/Resolver/PersistenceShortUrlRelationResolverTest.php new file mode 100644 index 00000000..5791d579 --- /dev/null +++ b/module/Core/test/ShortUrl/Resolver/PersistenceShortUrlRelationResolverTest.php @@ -0,0 +1,100 @@ +em = $this->prophesize(EntityManagerInterface::class); + $this->resolver = new PersistenceShortUrlRelationResolver($this->em->reveal()); + } + + /** @test */ + public function returnsEmptyWhenNoDomainIsProvided(): void + { + $getRepository = $this->em->getRepository(Domain::class); + + self::assertNull($this->resolver->resolveDomain(null)); + $getRepository->shouldNotHaveBeenCalled(); + } + + /** + * @test + * @dataProvider provideFoundDomains + */ + public function findsOrCreatesDomainWhenValueIsProvided(?Domain $foundDomain, string $authority): void + { + $repo = $this->prophesize(ObjectRepository::class); + $findDomain = $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain); + $getRepository = $this->em->getRepository(Domain::class)->willReturn($repo->reveal()); + + $result = $this->resolver->resolveDomain($authority); + + if ($foundDomain !== null) { + self::assertSame($result, $foundDomain); + } + self::assertInstanceOf(Domain::class, $result); + self::assertEquals($authority, $result->getAuthority()); + $findDomain->shouldHaveBeenCalledOnce(); + $getRepository->shouldHaveBeenCalledOnce(); + } + + public function provideFoundDomains(): iterable + { + $authority = 'doma.in'; + + yield 'not found domain' => [null, $authority]; + yield 'found domain' => [new Domain($authority), $authority]; + } + + /** @test */ + public function returnsEmptyWhenNoApiKeyIsProvided(): void + { + $getRepository = $this->em->getRepository(ApiKey::class); + + self::assertNull($this->resolver->resolveApiKey(null)); + $getRepository->shouldNotHaveBeenCalled(); + } + + /** + * @test + * @dataProvider provideFoundApiKeys + */ + public function triesToFindApiKeyWhenValueIsProvided(?ApiKey $foundApiKey, string $key): void + { + $repo = $this->prophesize(ObjectRepository::class); + $find = $repo->findOneBy(['key' => $key])->willReturn($foundApiKey); + $getRepository = $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); + + $result = $this->resolver->resolveApiKey($key); + + self::assertSame($result, $foundApiKey); + $find->shouldHaveBeenCalledOnce(); + $getRepository->shouldHaveBeenCalledOnce(); + } + + public function provideFoundApiKeys(): iterable + { + $key = 'abc123'; + + yield 'not found api key' => [null, $key]; + yield 'found api key' => [new ApiKey(), $key]; + } +} diff --git a/module/Core/test/ShortUrl/Resolver/SimpleShortUrlRelationResolverTest.php b/module/Core/test/ShortUrl/Resolver/SimpleShortUrlRelationResolverTest.php new file mode 100644 index 00000000..e2d0822c --- /dev/null +++ b/module/Core/test/ShortUrl/Resolver/SimpleShortUrlRelationResolverTest.php @@ -0,0 +1,56 @@ +resolver = new SimpleShortUrlRelationResolver(); + } + + /** + * @test + * @dataProvider provideDomains + */ + public function resolvesExpectedDomain(?string $domain): void + { + $result = $this->resolver->resolveDomain($domain); + + if ($domain === null) { + self::assertNull($result); + } else { + self::assertInstanceOf(Domain::class, $result); + self::assertEquals($domain, $result->getAuthority()); + } + } + + public function provideDomains(): iterable + { + yield 'empty domain' => [null]; + yield 'non-empty domain' => ['domain.com']; + } + + /** + * @test + * @dataProvider provideKeys + */ + public function alwaysReturnsNullForApiKeys(?string $key): void + { + self::assertNull($this->resolver->resolveApiKey($key)); + } + + public function provideKeys(): iterable + { + yield 'empty api key' => [null]; + yield 'non-empty api key' => ['abc123']; + } +}