|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Core\Service; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Entity\Tag; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; |
|
15
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; |
|
16
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlService; |
|
17
|
|
|
|
|
18
|
|
|
class ShortUrlServiceTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ShortUrlService |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $service; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ObjectProphecy|EntityManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $em; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class); |
|
32
|
|
|
$this->em->persist(Argument::any())->willReturn(null); |
|
33
|
|
|
$this->em->flush()->willReturn(null); |
|
34
|
|
|
$this->service = new ShortUrlService($this->em->reveal()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @test |
|
39
|
|
|
*/ |
|
40
|
|
|
public function listedUrlsAreReturnedFromEntityManager() |
|
41
|
|
|
{ |
|
42
|
|
|
$list = [ |
|
43
|
|
|
new ShortUrl(), |
|
44
|
|
|
new ShortUrl(), |
|
45
|
|
|
new ShortUrl(), |
|
46
|
|
|
new ShortUrl(), |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class); |
|
50
|
|
|
$repo->findList(Argument::cetera())->willReturn($list)->shouldBeCalledTimes(1); |
|
51
|
|
|
$repo->countList(Argument::cetera())->willReturn(count($list))->shouldBeCalledTimes(1); |
|
52
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$list = $this->service->listShortUrls(); |
|
55
|
|
|
$this->assertEquals(4, $list->getCurrentItemCount()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @test |
|
60
|
|
|
*/ |
|
61
|
|
|
public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode() |
|
62
|
|
|
{ |
|
63
|
|
|
$shortCode = 'abc123'; |
|
64
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class); |
|
65
|
|
|
$repo->findOneBy(['shortCode' => $shortCode])->willReturn(null) |
|
66
|
|
|
->shouldBeCalledTimes(1); |
|
67
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$this->expectException(InvalidShortCodeException::class); |
|
70
|
|
|
$this->service->setTagsByShortCode($shortCode); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @test |
|
75
|
|
|
*/ |
|
76
|
|
|
public function providedTagsAreGetFromRepoAndSetToTheShortUrl() |
|
77
|
|
|
{ |
|
78
|
|
|
$shortUrl = $this->prophesize(ShortUrl::class); |
|
79
|
|
|
$shortUrl->setTags(Argument::any())->shouldBeCalledTimes(1); |
|
80
|
|
|
$shortCode = 'abc123'; |
|
81
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class); |
|
82
|
|
|
$repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl->reveal()) |
|
83
|
|
|
->shouldBeCalledTimes(1); |
|
84
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
$tagRepo = $this->prophesize(EntityRepository::class); |
|
87
|
|
|
$tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag())->shouldbeCalledTimes(1); |
|
88
|
|
|
$tagRepo->findOneBy(['name' => 'bar'])->willReturn(null)->shouldbeCalledTimes(1); |
|
89
|
|
|
$this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal()); |
|
90
|
|
|
|
|
91
|
|
|
$this->service->setTagsByShortCode($shortCode, ['foo', 'bar']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @test |
|
96
|
|
|
*/ |
|
97
|
|
|
public function updateMetadataByShortCodeUpdatesProvidedData() |
|
98
|
|
|
{ |
|
99
|
|
|
$shortUrl = new ShortUrl(); |
|
100
|
|
|
|
|
101
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class); |
|
102
|
|
|
$findShortUrl = $repo->findOneBy(['shortCode' => 'abc123'])->willReturn($shortUrl); |
|
103
|
|
|
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); |
|
|
|
|
|
|
104
|
|
|
$flush = $this->em->flush($shortUrl)->willReturn(null); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$result = $this->service->updateMetadataByShortCode('abc123', ShortUrlMeta::createFromParams( |
|
107
|
|
|
(new \DateTime('2017-01-01 00:00:00'))->format(\DateTime::ATOM), |
|
108
|
|
|
(new \DateTime('2017-01-05 00:00:00'))->format(\DateTime::ATOM), |
|
109
|
|
|
null, |
|
110
|
|
|
5 |
|
111
|
|
|
)); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertSame($shortUrl, $result); |
|
114
|
|
|
$this->assertEquals(new \DateTime('2017-01-01 00:00:00'), $shortUrl->getValidSince()); |
|
115
|
|
|
$this->assertEquals(new \DateTime('2017-01-05 00:00:00'), $shortUrl->getValidUntil()); |
|
116
|
|
|
$this->assertEquals(5, $shortUrl->getMaxVisits()); |
|
117
|
|
|
$findShortUrl->shouldHaveBeenCalled(); |
|
118
|
|
|
$getRepo->shouldHaveBeenCalled(); |
|
119
|
|
|
$flush->shouldHaveBeenCalled(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: