From fd1fe907314cfa8668519edd6c1a4465fcff1dda Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 1 Oct 2019 22:00:46 +0200 Subject: [PATCH] Created tests for new domain resolvers --- .../PersistenceDomainResolverTest.php | 63 +++++++++++++++++++ .../Resolver/SimpleDomainResolverTest.php | 41 ++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 module/Core/test/Domain/Resolver/PersistenceDomainResolverTest.php create mode 100644 module/Core/test/Domain/Resolver/SimpleDomainResolverTest.php diff --git a/module/Core/test/Domain/Resolver/PersistenceDomainResolverTest.php b/module/Core/test/Domain/Resolver/PersistenceDomainResolverTest.php new file mode 100644 index 00000000..aeb72873 --- /dev/null +++ b/module/Core/test/Domain/Resolver/PersistenceDomainResolverTest.php @@ -0,0 +1,63 @@ +em = $this->prophesize(EntityManagerInterface::class); + $this->domainResolver = new PersistenceDomainResolver($this->em->reveal()); + } + + /** @test */ + public function returnsEmptyWhenNoDomainIsProvided(): void + { + $getRepository = $this->em->getRepository(Domain::class); + + $this->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) { + $this->assertSame($result, $foundDomain); + } + $this->assertInstanceOf(Domain::class, $result); + $this->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 new file mode 100644 index 00000000..5e175c92 --- /dev/null +++ b/module/Core/test/Domain/Resolver/SimpleDomainResolverTest.php @@ -0,0 +1,41 @@ +domainResolver = new SimpleDomainResolver(); + } + + /** + * @test + * @dataProvider provideDomains + */ + public function resolvesExpectedDomain(?string $domain): void + { + $result = $this->domainResolver->resolveDomain($domain); + + if ($domain === null) { + $this->assertNull($result); + } else { + $this->assertInstanceOf(Domain::class, $result); + $this->assertEquals($domain, $result->getAuthority()); + } + } + + public function provideDomains(): iterable + { + yield 'with empty domain' => [null]; + yield 'with non-empty domain' => ['domain.com']; + } +}