|
1
|
|
|
<?php |
|
2
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\Tag; |
|
3
|
|
|
|
|
4
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface; |
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Prophecy\Prophecy\MethodProphecy; |
|
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Entity\Tag; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface; |
|
11
|
|
|
use Shlinkio\Shlink\Rest\Action\Tag\UpdateTagAction; |
|
12
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
13
|
|
|
use Zend\I18n\Translator\Translator; |
|
14
|
|
|
|
|
15
|
|
|
class UpdateTagActionTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var UpdateTagAction |
|
19
|
|
|
*/ |
|
20
|
|
|
private $action; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ObjectProphecy |
|
23
|
|
|
*/ |
|
24
|
|
|
private $tagService; |
|
25
|
|
|
|
|
26
|
|
|
public function setUp() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->tagService = $this->prophesize(TagServiceInterface::class); |
|
29
|
|
|
$this->action = new UpdateTagAction($this->tagService->reveal(), Translator::factory([])); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @test |
|
34
|
|
|
* @dataProvider provideParams |
|
35
|
|
|
* @param array $bodyParams |
|
36
|
|
|
*/ |
|
37
|
|
|
public function whenInvalidParamsAreProvidedAnErrorIsReturned(array $bodyParams) |
|
38
|
|
|
{ |
|
39
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody($bodyParams); |
|
40
|
|
|
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertEquals(400, $resp->getStatusCode()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function provideParams() |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
|
|
[['oldName' => 'foo']], |
|
49
|
|
|
[['newName' => 'foo']], |
|
50
|
|
|
[[]], |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @test |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function requestingInvalidTagReturnsError() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([ |
|
60
|
|
|
'oldName' => 'foo', |
|
61
|
|
|
'newName' => 'bar', |
|
62
|
|
|
]); |
|
63
|
|
|
/** @var MethodProphecy $rename */ |
|
64
|
|
|
$rename = $this->tagService->renameTag('foo', 'bar')->willThrow(EntityDoesNotExistException::class); |
|
65
|
|
|
|
|
66
|
|
|
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertEquals(404, $resp->getStatusCode()); |
|
69
|
|
|
$rename->shouldHaveBeenCalled(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @test |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function correctInvocationRenamesTag() |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([ |
|
78
|
|
|
'oldName' => 'foo', |
|
79
|
|
|
'newName' => 'bar', |
|
80
|
|
|
]); |
|
81
|
|
|
/** @var MethodProphecy $rename */ |
|
82
|
|
|
$rename = $this->tagService->renameTag('foo', 'bar')->willReturn(new Tag()); |
|
83
|
|
|
|
|
84
|
|
|
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals(204, $resp->getStatusCode()); |
|
87
|
|
|
$rename->shouldHaveBeenCalled(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.