|
4 | 4 |
|
5 | 5 | namespace Buddy\Repman\Tests\Unit\Service\Integration\BitbucketApi; |
6 | 6 |
|
| 7 | +use Bitbucket\Api\AbstractApi; |
7 | 8 | use Bitbucket\Api\CurrentUser; |
8 | 9 | use Bitbucket\Api\Repositories as RepositoriesApi; |
9 | 10 | use Bitbucket\Client; |
@@ -76,28 +77,70 @@ public function testThrowExceptionWhenPrimaryEmailNotFound(): void |
76 | 77 | $this->api->primaryEmail('token'); |
77 | 78 | } |
78 | 79 |
|
79 | | - public function testFetchRepositories(): void |
| 80 | + /** |
| 81 | + * Every workspace has to be asked separately: the endpoint that answered for all of |
| 82 | + * them at once was withdrawn by Bitbucket (CHANGE-2770), so a user whose |
| 83 | + * repositories live in more than one workspace is the case that matters here. |
| 84 | + */ |
| 85 | + public function testFetchRepositoriesFromEveryWorkspace(): void |
80 | 86 | { |
81 | | - $this->pagerMock->method('fetchAll')->willReturn([ |
82 | | - [ |
83 | | - 'uuid' => '099acebd-5158-459e-b05c-30e51b49a1a8', |
84 | | - 'full_name' => 'repman/left-pad', |
85 | | - 'links' => ['html' => ['href' => 'https://gitlab.com/repman/left-pad']], |
86 | | - ], |
87 | | - [ |
88 | | - 'uuid' => '74fb57b9-0820-4165-bba0-892eef8f69b8', |
89 | | - 'full_name' => 'repman/right-pad', |
90 | | - 'links' => ['html' => ['href' => 'https://gitlab.com/repman/right-pad']], |
91 | | - ], |
92 | | - ]); |
93 | | - $this->clientMock->method('repositories')->willReturn($this->createMock(RepositoriesApi::class)); |
| 87 | + $currentUser = $this->createMock(CurrentUser::class); |
| 88 | + $workspaceApis = [ |
| 89 | + 'repman' => $this->createMock(RepositoriesApi\Workspaces::class), |
| 90 | + 'other-team' => $this->createMock(RepositoriesApi\Workspaces::class), |
| 91 | + ]; |
| 92 | + |
| 93 | + $repos = $this->createMock(RepositoriesApi::class); |
| 94 | + $repos->method('workspaces')->willReturnCallback( |
| 95 | + fn (string $slug): RepositoriesApi\Workspaces => $workspaceApis[$slug] |
| 96 | + ); |
| 97 | + $this->clientMock->method('currentUser')->willReturn($currentUser); |
| 98 | + $this->clientMock->method('repositories')->willReturn($repos); |
| 99 | + |
| 100 | + $this->pagerMock->method('fetchAll')->willReturnCallback( |
| 101 | + function (AbstractApi $api, string $method, array $parameters = []) use ($currentUser, $workspaceApis): array { |
| 102 | + if ($api === $currentUser) { |
| 103 | + // unfiltered, or workspaces the user is only a member of drop out |
| 104 | + self::assertSame([], $parameters); |
| 105 | + |
| 106 | + return [['slug' => 'repman'], ['slug' => 'other-team']]; |
| 107 | + } |
| 108 | + |
| 109 | + self::assertSame([['role' => 'member']], $parameters); |
| 110 | + |
| 111 | + if ($api === $workspaceApis['repman']) { |
| 112 | + return [[ |
| 113 | + 'uuid' => '099acebd-5158-459e-b05c-30e51b49a1a8', |
| 114 | + 'full_name' => 'repman/left-pad', |
| 115 | + 'links' => ['html' => ['href' => 'https://bitbucket.org/repman/left-pad']], |
| 116 | + ]]; |
| 117 | + } |
| 118 | + |
| 119 | + return [[ |
| 120 | + 'uuid' => '74fb57b9-0820-4165-bba0-892eef8f69b8', |
| 121 | + 'full_name' => 'other-team/right-pad', |
| 122 | + 'links' => ['html' => ['href' => 'https://bitbucket.org/other-team/right-pad']], |
| 123 | + ]]; |
| 124 | + } |
| 125 | + ); |
94 | 126 |
|
95 | 127 | self::assertEquals(new Repositories([ |
96 | | - new Repository('099acebd-5158-459e-b05c-30e51b49a1a8', 'repman/left-pad', 'https://gitlab.com/repman/left-pad.git'), |
97 | | - new Repository('74fb57b9-0820-4165-bba0-892eef8f69b8', 'repman/right-pad', 'https://gitlab.com/repman/right-pad.git'), |
| 128 | + new Repository('099acebd-5158-459e-b05c-30e51b49a1a8', 'repman/left-pad', 'https://bitbucket.org/repman/left-pad.git'), |
| 129 | + new Repository('74fb57b9-0820-4165-bba0-892eef8f69b8', 'other-team/right-pad', 'https://bitbucket.org/other-team/right-pad.git'), |
98 | 130 | ]), $this->api->repositories('token')); |
99 | 131 | } |
100 | 132 |
|
| 133 | + public function testFetchRepositoriesWhenUserBelongsToNoWorkspace(): void |
| 134 | + { |
| 135 | + $currentUser = $this->createMock(CurrentUser::class); |
| 136 | + $this->clientMock->method('currentUser')->willReturn($currentUser); |
| 137 | + // nothing to ask about, so the repositories endpoint is never reached |
| 138 | + $this->clientMock->expects(self::never())->method('repositories'); |
| 139 | + $this->pagerMock->method('fetchAll')->willReturn([]); |
| 140 | + |
| 141 | + self::assertEquals(new Repositories([]), $this->api->repositories('token')); |
| 142 | + } |
| 143 | + |
101 | 144 | public function testAddHookWhenNotExist(): void |
102 | 145 | { |
103 | 146 | $repos = $this->createMock(RepositoriesApi::class); |
|
0 commit comments