From 4185015befd7d585229b89a3ef87dbc5be3e93f7 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 21 Aug 2016 18:15:25 +0200 Subject: [PATCH] Improved tests --- module/Core/test/Entity/TagTest.php | 18 +++++++++ .../Core/test/Service/ShortUrlServiceTest.php | 40 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 module/Core/test/Entity/TagTest.php diff --git a/module/Core/test/Entity/TagTest.php b/module/Core/test/Entity/TagTest.php new file mode 100644 index 00000000..b41d7a36 --- /dev/null +++ b/module/Core/test/Entity/TagTest.php @@ -0,0 +1,18 @@ +setName('This is my name'); + $this->assertEquals($tag->getName(), $tag->jsonSerialize()); + } +} diff --git a/module/Core/test/Service/ShortUrlServiceTest.php b/module/Core/test/Service/ShortUrlServiceTest.php index 1244d3a5..a87f91e1 100644 --- a/module/Core/test/Service/ShortUrlServiceTest.php +++ b/module/Core/test/Service/ShortUrlServiceTest.php @@ -2,10 +2,12 @@ namespace ShlinkioTest\Shlink\Core\Service; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\EntityRepository; use PHPUnit_Framework_TestCase as TestCase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\Core\Entity\ShortUrl; +use Shlinkio\Shlink\Core\Entity\Tag; use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; use Shlinkio\Shlink\Core\Service\ShortUrlService; @@ -23,6 +25,8 @@ class ShortUrlServiceTest extends TestCase public function setUp() { $this->em = $this->prophesize(EntityManagerInterface::class); + $this->em->persist(Argument::any())->willReturn(null); + $this->em->flush()->willReturn(null); $this->service = new ShortUrlService($this->em->reveal()); } @@ -46,4 +50,40 @@ class ShortUrlServiceTest extends TestCase $list = $this->service->listShortUrls(); $this->assertEquals(4, $list->getCurrentItemCount()); } + + /** + * @test + * @expectedException \Shlinkio\Shlink\Core\Exception\InvalidShortCodeException + */ + public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode() + { + $shortCode = 'abc123'; + $repo = $this->prophesize(ShortUrlRepository::class); + $repo->findOneBy(['shortCode' => $shortCode])->willReturn(null) + ->shouldBeCalledTimes(1); + $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); + + $this->service->setTagsByShortCode($shortCode); + } + + /** + * @test + */ + public function providedTagsAreGetFromRepoAndSetToTheShortUrl() + { + $shortUrl = $this->prophesize(ShortUrl::class); + $shortUrl->setTags(Argument::any())->shouldBeCalledTimes(1); + $shortCode = 'abc123'; + $repo = $this->prophesize(ShortUrlRepository::class); + $repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl->reveal()) + ->shouldBeCalledTimes(1); + $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); + + $tagRepo = $this->prophesize(EntityRepository::class); + $tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag())->shouldbeCalledTimes(1); + $tagRepo->findOneBy(['name' => 'bar'])->willReturn(null)->shouldbeCalledTimes(1); + $this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal()); + + $this->service->setTagsByShortCode($shortCode, ['foo', 'bar']); + } }