55namespace Laminas \AutomaticReleases \Test \Unit \Git ;
66
77use Laminas \AutomaticReleases \Git \CreateTagViaConsole ;
8+ use Laminas \AutomaticReleases \Git \HasTag ;
9+ use Laminas \AutomaticReleases \Git \HasTagViaConsole ;
810use Laminas \AutomaticReleases \Git \Value \BranchName ;
911use Laminas \AutomaticReleases \Gpg \ImportGpgKeyFromStringViaTemporaryFile ;
1012use Laminas \AutomaticReleases \Gpg \SecretKeyId ;
1315use Psl \Filesystem ;
1416use Psl \Shell ;
1517use Psr \Http \Message \UriInterface ;
18+ use Psr \Log \LoggerInterface ;
1619
1720use function Psl \File \read ;
21+ use function sprintf ;
1822
1923/** @covers \Laminas\AutomaticReleases\Git\CreateTagViaConsole */
2024final class CreateTagViaConsoleTest extends TestCase
@@ -46,19 +50,24 @@ protected function setUp(): void
4650
4751 public function testCreatesSignedTag (): void
4852 {
53+ $ logger = $ this ->createMock (LoggerInterface::class);
54+ $ logger ->expects (self ::never ())->method ('info ' );
55+
4956 $ sourceUri = $ this ->createMock (UriInterface::class);
57+ $ sourceUri ->method ('__toString ' )->willReturn ($ this ->repository );
5058
51- $ sourceUri ->method ('__toString ' )
52- ->willReturn ($ this ->repository );
59+ $ hasTag = $ this ->createMock (HasTag::class);
60+ $ hasTag ->method ('__invoke ' )
61+ ->with ($ this ->repository , 'name-of-the-tag ' )
62+ ->willReturn (false );
5363
54- (new CreateTagViaConsole ())
55- ->__invoke (
56- $ this ->repository ,
57- BranchName::fromName ('tag-branch ' ),
58- 'name-of-the-tag ' ,
59- 'changelog text for the tag ' ,
60- $ this ->key ,
61- );
64+ (new CreateTagViaConsole ($ hasTag , $ logger ))(
65+ $ this ->repository ,
66+ BranchName::fromName ('tag-branch ' ),
67+ 'name-of-the-tag ' ,
68+ 'changelog text for the tag ' ,
69+ $ this ->key ,
70+ );
6271
6372 Shell \execute ('git ' , ['tag ' , '-v ' , 'name-of-the-tag ' ], $ this ->repository );
6473
@@ -69,4 +78,46 @@ public function testCreatesSignedTag(): void
6978 self ::assertStringContainsString ('a commit ' , $ fetchedTag );
7079 self ::assertStringContainsString ('-----BEGIN PGP SIGNATURE----- ' , $ fetchedTag );
7180 }
81+
82+ public function testSkipsIfTagAlreadyExists (): void
83+ {
84+ $ tagName = 'name-of-the-tag ' ;
85+ $ logger = $ this ->createMock (LoggerInterface::class);
86+ $ logger ->expects (self ::never ())
87+ ->method ('info ' );
88+
89+ $ sourceUri = $ this ->createMock (UriInterface::class);
90+ $ sourceUri ->method ('__toString ' )->willReturn ($ this ->repository );
91+
92+ $ hasTag = new HasTagViaConsole ();
93+
94+ (new CreateTagViaConsole ($ hasTag , $ logger ))(
95+ $ this ->repository ,
96+ BranchName::fromName ('tag-branch ' ),
97+ $ tagName ,
98+ 'changelog text for the tag ' ,
99+ $ this ->key ,
100+ );
101+
102+ Shell \execute ('git ' , ['tag ' , '-v ' , $ tagName ], $ this ->repository );
103+ $ fetchedTag = Shell \execute ('git ' , ['show ' , $ tagName ], $ this ->repository );
104+
105+ self ::assertStringContainsString ('tag name-of-the-tag ' , $ fetchedTag );
106+ self ::assertStringContainsString ('changelog text for the tag ' , $ fetchedTag );
107+ self ::assertStringContainsString ('a commit ' , $ fetchedTag );
108+ self ::assertStringContainsString ('-----BEGIN PGP SIGNATURE----- ' , $ fetchedTag );
109+
110+ $ logger = $ this ->createMock (LoggerInterface::class);
111+ $ logger ->expects (self ::once ())
112+ ->method ('info ' )
113+ ->with (sprintf ('[CreateTagViaConsole] Skipping this step; tag "%s" already exists. ' , $ tagName ));
114+
115+ (new CreateTagViaConsole ($ hasTag , $ logger ))(
116+ $ this ->repository ,
117+ BranchName::fromName ('tag-branch ' ),
118+ $ tagName ,
119+ 'changelog text for the tag ' ,
120+ $ this ->key ,
121+ );
122+ }
72123}
0 commit comments