@@ -139,22 +139,209 @@ public function testUpdateComment(): void
139139
140140 public function testGenerateCloneCommand (): void
141141 {
142- $ this ->markTestSkipped ('Not implemented for GitLab yet ' );
142+ $ repositoryName = 'test-clone-command- ' . \uniqid ();
143+ $ this ->vcsAdapter ->createRepository (static ::$ owner , $ repositoryName , false );
144+ $ directory = '/tmp/test-clone- ' . \uniqid ();
145+
146+ try {
147+ $ this ->vcsAdapter ->createFile (static ::$ owner , $ repositoryName , 'README.md ' , '# Test ' );
148+
149+ $ command = $ this ->vcsAdapter ->generateCloneCommand (
150+ static ::$ owner ,
151+ $ repositoryName ,
152+ static ::$ defaultBranch ,
153+ \Utopia \VCS \Adapter \Git::CLONE_TYPE_BRANCH ,
154+ $ directory ,
155+ '/ '
156+ );
157+
158+ $ this ->assertIsString ($ command );
159+ $ this ->assertStringContainsString ('git init ' , $ command );
160+ $ this ->assertStringContainsString ('git remote add origin ' , $ command );
161+ $ this ->assertStringContainsString ('git config core.sparseCheckout true ' , $ command );
162+ $ this ->assertStringContainsString ($ repositoryName , $ command );
163+
164+ $ output = [];
165+ \exec ($ command . ' 2>&1 ' , $ output , $ exitCode );
166+ $ this ->assertSame (0 , $ exitCode , implode ("\n" , $ output ));
167+ $ this ->assertFileExists ($ directory . '/README.md ' );
168+ } finally {
169+ $ this ->vcsAdapter ->deleteRepository (static ::$ owner , $ repositoryName );
170+ if (\is_dir ($ directory )) {
171+ \exec ('rm -rf ' . escapeshellarg ($ directory ));
172+ }
173+ }
143174 }
144175
145176 public function testGenerateCloneCommandWithCommitHash (): void
146177 {
147- $ this ->markTestSkipped ('Not implemented for GitLab yet ' );
178+ $ repositoryName = 'test-clone-commit- ' . \uniqid ();
179+ $ this ->vcsAdapter ->createRepository (static ::$ owner , $ repositoryName , false );
180+
181+ try {
182+ $ this ->vcsAdapter ->createFile (static ::$ owner , $ repositoryName , 'README.md ' , '# Test ' );
183+
184+ $ commit = $ this ->vcsAdapter ->getLatestCommit (static ::$ owner , $ repositoryName , static ::$ defaultBranch );
185+ $ commitHash = $ commit ['commitHash ' ];
186+
187+ $ directory = '/tmp/test-clone-commit- ' . \uniqid ();
188+ $ command = $ this ->vcsAdapter ->generateCloneCommand (
189+ static ::$ owner ,
190+ $ repositoryName ,
191+ $ commitHash ,
192+ \Utopia \VCS \Adapter \Git::CLONE_TYPE_COMMIT ,
193+ $ directory ,
194+ '/ '
195+ );
196+
197+ $ this ->assertIsString ($ command );
198+ $ this ->assertStringContainsString ('git fetch --depth=1 ' , $ command );
199+ $ this ->assertStringContainsString ($ commitHash , $ command );
200+ } finally {
201+ $ this ->vcsAdapter ->deleteRepository (static ::$ owner , $ repositoryName );
202+ }
148203 }
149204
150205 public function testGenerateCloneCommandWithTag (): void
151206 {
152- $ this ->markTestSkipped ('Not implemented for GitLab yet ' );
207+ $ repositoryName = 'test-clone-tag- ' . \uniqid ();
208+ $ this ->vcsAdapter ->createRepository (static ::$ owner , $ repositoryName , false );
209+
210+ try {
211+ $ this ->vcsAdapter ->createFile (static ::$ owner , $ repositoryName , 'README.md ' , '# Test ' );
212+
213+ $ commit = $ this ->vcsAdapter ->getLatestCommit (static ::$ owner , $ repositoryName , static ::$ defaultBranch );
214+ $ commitHash = $ commit ['commitHash ' ];
215+
216+ $ this ->vcsAdapter ->createTag (static ::$ owner , $ repositoryName , 'v1.0.0 ' , $ commitHash );
217+
218+ $ directory = '/tmp/test-clone-tag- ' . \uniqid ();
219+ $ command = $ this ->vcsAdapter ->generateCloneCommand (
220+ static ::$ owner ,
221+ $ repositoryName ,
222+ 'v1.0.0 ' ,
223+ \Utopia \VCS \Adapter \Git::CLONE_TYPE_TAG ,
224+ $ directory ,
225+ '/ '
226+ );
227+
228+ $ this ->assertIsString ($ command );
229+ $ this ->assertStringContainsString ('refs/tags ' , $ command );
230+ $ this ->assertStringContainsString ('v1.0.0 ' , $ command );
231+ } finally {
232+ $ this ->vcsAdapter ->deleteRepository (static ::$ owner , $ repositoryName );
233+ }
153234 }
154235
155236 public function testGenerateCloneCommandWithInvalidRepository (): void
156237 {
157- $ this ->markTestSkipped ('Not implemented for GitLab yet ' );
238+ $ directory = '/tmp/test-clone-invalid- ' . \uniqid ();
239+
240+ try {
241+ $ command = $ this ->vcsAdapter ->generateCloneCommand (
242+ static ::$ owner ,
243+ 'nonexistent-repo- ' . \uniqid (),
244+ static ::$ defaultBranch ,
245+ \Utopia \VCS \Adapter \Git::CLONE_TYPE_BRANCH ,
246+ $ directory ,
247+ '/ '
248+ );
249+
250+ $ output = [];
251+ \exec ($ command . ' 2>&1 ' , $ output , $ exitCode );
252+
253+ $ cloneFailed = ($ exitCode !== 0 ) || !file_exists ($ directory . '/README.md ' );
254+ $ this ->assertTrue ($ cloneFailed , 'Clone should have failed for nonexistent repository ' );
255+ } finally {
256+ if (\is_dir ($ directory )) {
257+ \exec ('rm -rf ' . escapeshellarg ($ directory ));
258+ }
259+ }
260+ }
261+
262+ public function testGetCommit (): void
263+ {
264+ $ repositoryName = 'test-get-commit- ' . \uniqid ();
265+ $ this ->vcsAdapter ->createRepository (static ::$ owner , $ repositoryName , false );
266+
267+ try {
268+ $ customMessage = 'Test commit message ' ;
269+ $ this ->vcsAdapter ->createFile (static ::$ owner , $ repositoryName , 'README.md ' , '# Test ' , $ customMessage );
270+
271+ $ latestCommit = $ this ->vcsAdapter ->getLatestCommit (static ::$ owner , $ repositoryName , static ::$ defaultBranch );
272+ $ commitHash = $ latestCommit ['commitHash ' ];
273+
274+ $ result = $ this ->vcsAdapter ->getCommit (static ::$ owner , $ repositoryName , $ commitHash );
275+
276+ $ this ->assertIsArray ($ result );
277+ $ this ->assertArrayHasKey ('commitHash ' , $ result );
278+ $ this ->assertArrayHasKey ('commitMessage ' , $ result );
279+ $ this ->assertArrayHasKey ('commitAuthor ' , $ result );
280+ $ this ->assertArrayHasKey ('commitUrl ' , $ result );
281+ $ this ->assertArrayHasKey ('commitAuthorAvatar ' , $ result );
282+ $ this ->assertArrayHasKey ('commitAuthorUrl ' , $ result );
283+ $ this ->assertSame ($ commitHash , $ result ['commitHash ' ]);
284+ $ this ->assertStringStartsWith ($ customMessage , $ result ['commitMessage ' ]);
285+ $ this ->assertNotEmpty ($ result ['commitUrl ' ]);
286+ } finally {
287+ $ this ->vcsAdapter ->deleteRepository (static ::$ owner , $ repositoryName );
288+ }
289+ }
290+
291+ public function testGetLatestCommit (): void
292+ {
293+ $ repositoryName = 'test-get-latest-commit- ' . \uniqid ();
294+ $ this ->vcsAdapter ->createRepository (static ::$ owner , $ repositoryName , false );
295+
296+ try {
297+ $ firstMessage = 'First commit ' ;
298+ $ secondMessage = 'Second commit ' ;
299+
300+ $ this ->vcsAdapter ->createFile (static ::$ owner , $ repositoryName , 'README.md ' , '# Test ' , $ firstMessage );
301+ $ commit1 = $ this ->vcsAdapter ->getLatestCommit (static ::$ owner , $ repositoryName , static ::$ defaultBranch );
302+
303+ $ this ->assertIsArray ($ commit1 );
304+ $ this ->assertNotEmpty ($ commit1 ['commitHash ' ]);
305+ $ this ->assertStringStartsWith ($ firstMessage , $ commit1 ['commitMessage ' ]);
306+ $ this ->assertNotEmpty ($ commit1 ['commitUrl ' ]);
307+
308+ $ commit1Hash = $ commit1 ['commitHash ' ];
309+
310+ $ this ->vcsAdapter ->createFile (static ::$ owner , $ repositoryName , 'test.txt ' , 'test ' , $ secondMessage );
311+ $ commit2 = $ this ->vcsAdapter ->getLatestCommit (static ::$ owner , $ repositoryName , static ::$ defaultBranch );
312+
313+ $ this ->assertStringStartsWith ($ secondMessage , $ commit2 ['commitMessage ' ]);
314+ $ this ->assertNotSame ($ commit1Hash , $ commit2 ['commitHash ' ]);
315+ } finally {
316+ $ this ->vcsAdapter ->deleteRepository (static ::$ owner , $ repositoryName );
317+ }
318+ }
319+
320+ public function testGetCommitWithInvalidHash (): void
321+ {
322+ $ repositoryName = 'test-get-commit-invalid- ' . \uniqid ();
323+ $ this ->vcsAdapter ->createRepository (static ::$ owner , $ repositoryName , false );
324+
325+ try {
326+ $ this ->expectException (\Exception::class);
327+ $ this ->vcsAdapter ->getCommit (static ::$ owner , $ repositoryName , 'invalid-sha-12345 ' );
328+ } finally {
329+ $ this ->vcsAdapter ->deleteRepository (static ::$ owner , $ repositoryName );
330+ }
331+ }
332+
333+ public function testGetLatestCommitWithInvalidBranch (): void
334+ {
335+ $ repositoryName = 'test-get-latest-commit-invalid- ' . \uniqid ();
336+ $ this ->vcsAdapter ->createRepository (static ::$ owner , $ repositoryName , false );
337+ $ this ->vcsAdapter ->createFile (static ::$ owner , $ repositoryName , 'README.md ' , '# Test ' );
338+
339+ try {
340+ $ this ->expectException (\Exception::class);
341+ $ this ->vcsAdapter ->getLatestCommit (static ::$ owner , $ repositoryName , 'non-existing-branch ' );
342+ } finally {
343+ $ this ->vcsAdapter ->deleteRepository (static ::$ owner , $ repositoryName );
344+ }
158345 }
159346
160347 public function testWebhookPushEvent (): void
0 commit comments