|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * (c) Packagist Conductors UG (haftungsbeschränkt) <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PrivatePackagist\ApiClient\Api\Projects; |
| 11 | + |
| 12 | +use PrivatePackagist\ApiClient\Api\ApiTestCase; |
| 13 | + |
| 14 | +class PackagesTest extends ApiTestCase |
| 15 | +{ |
| 16 | + public function testAll() |
| 17 | + { |
| 18 | + $projectName = 'project'; |
| 19 | + $expected = [ |
| 20 | + [ |
| 21 | + 'id' => 1, |
| 22 | + 'name' => 'acme-website/package', |
| 23 | + ], |
| 24 | + ]; |
| 25 | + |
| 26 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 27 | + $api = $this->getApiMock(); |
| 28 | + $api->expects($this->once()) |
| 29 | + ->method('get') |
| 30 | + ->with($this->equalTo('/projects/project/packages/')) |
| 31 | + ->willReturn($expected); |
| 32 | + |
| 33 | + $this->assertSame($expected, $api->all($projectName)); |
| 34 | + } |
| 35 | + |
| 36 | + public function testAllWithFilters() |
| 37 | + { |
| 38 | + $projectName = 'project'; |
| 39 | + $expected = [ |
| 40 | + [ |
| 41 | + 'id' => 1, |
| 42 | + 'name' => 'acme-website/package', |
| 43 | + ], |
| 44 | + ]; |
| 45 | + |
| 46 | + $filters = [ |
| 47 | + 'origin' => \PrivatePackagist\ApiClient\Api\Packages::ORIGIN_PRIVATE, |
| 48 | + ]; |
| 49 | + |
| 50 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 51 | + $api = $this->getApiMock(); |
| 52 | + $api->expects($this->once()) |
| 53 | + ->method('get') |
| 54 | + ->with($this->equalTo('/projects/project/packages/'), $this->equalTo($filters)) |
| 55 | + ->willReturn($expected); |
| 56 | + |
| 57 | + $this->assertSame($expected, $api->all($projectName, $filters)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException |
| 62 | + */ |
| 63 | + public function testAllWithInvalidFilters() |
| 64 | + { |
| 65 | + $projectName = 'project'; |
| 66 | + $filters = [ |
| 67 | + 'origin' => 'invalid' |
| 68 | + ]; |
| 69 | + |
| 70 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 71 | + $api = $this->getApiMock(); |
| 72 | + $api->expects($this->never()) |
| 73 | + ->method('get'); |
| 74 | + |
| 75 | + $api->all($projectName, $filters); |
| 76 | + } |
| 77 | + |
| 78 | + public function testShow() |
| 79 | + { |
| 80 | + $projectName = 'project'; |
| 81 | + $expected = [ |
| 82 | + 'id' => 1, |
| 83 | + 'name' => 'acme-website/package', |
| 84 | + ]; |
| 85 | + |
| 86 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 87 | + $api = $this->getApiMock(); |
| 88 | + $api->expects($this->once()) |
| 89 | + ->method('get') |
| 90 | + ->with($this->equalTo('/projects/project/packages/acme-website/package')) |
| 91 | + ->willReturn($expected); |
| 92 | + |
| 93 | + $this->assertSame($expected, $api->show($projectName, 'acme-website/package')); |
| 94 | + } |
| 95 | + |
| 96 | + public function testCreateVcsPackage() |
| 97 | + { |
| 98 | + $projectName = 'project'; |
| 99 | + $expected = [ |
| 100 | + 'id' => 'job-id', |
| 101 | + 'status' => 'queued', |
| 102 | + ]; |
| 103 | + |
| 104 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 105 | + $api = $this->getApiMock(); |
| 106 | + $api->expects($this->once()) |
| 107 | + ->method('post') |
| 108 | + ->with($this->equalTo('/projects/project/packages/'), $this->equalTo(['repoType' => 'vcs', 'repoUrl' => 'localhost', 'credentials' => null])) |
| 109 | + ->willReturn($expected); |
| 110 | + |
| 111 | + $this->assertSame($expected, $api->createVcsPackage($projectName, 'localhost')); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @dataProvider customProvider |
| 116 | + */ |
| 117 | + public function testCreateCustomPackage($customJson) |
| 118 | + { |
| 119 | + $projectName = 'project'; |
| 120 | + $expected = [ |
| 121 | + 'id' => 'job-id', |
| 122 | + 'status' => 'queued', |
| 123 | + ]; |
| 124 | + |
| 125 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 126 | + $api = $this->getApiMock(); |
| 127 | + $api->expects($this->once()) |
| 128 | + ->method('post') |
| 129 | + ->with($this->equalTo('/projects/project/packages/'), $this->equalTo(['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null])) |
| 130 | + ->willReturn($expected); |
| 131 | + |
| 132 | + $this->assertSame($expected, $api->createCustomPackage($projectName, $customJson)); |
| 133 | + } |
| 134 | + |
| 135 | + public function customProvider() |
| 136 | + { |
| 137 | + return [ |
| 138 | + ['{}'], |
| 139 | + [new \stdClass()], |
| 140 | + ]; |
| 141 | + } |
| 142 | + |
| 143 | + public function testEditVcsPackage() |
| 144 | + { |
| 145 | + $projectName = 'project'; |
| 146 | + $expected = [ |
| 147 | + 'id' => 'job-id', |
| 148 | + 'status' => 'queued', |
| 149 | + ]; |
| 150 | + |
| 151 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 152 | + $api = $this->getApiMock(); |
| 153 | + $api->expects($this->once()) |
| 154 | + ->method('put') |
| 155 | + ->with($this->equalTo('/projects/project/packages/acme-website/package/'), $this->equalTo(['repoType' => 'vcs', 'repoUrl' => 'localhost', 'credentials' => null])) |
| 156 | + ->willReturn($expected); |
| 157 | + |
| 158 | + $this->assertSame($expected, $api->editVcsPackage($projectName, 'acme-website/package', 'localhost')); |
| 159 | + } |
| 160 | + |
| 161 | + public function testEditCustomPackage() |
| 162 | + { |
| 163 | + $projectName = 'project'; |
| 164 | + $expected = [ |
| 165 | + 'id' => 'job-id', |
| 166 | + 'status' => 'queued', |
| 167 | + ]; |
| 168 | + |
| 169 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 170 | + $api = $this->getApiMock(); |
| 171 | + $api->expects($this->once()) |
| 172 | + ->method('put') |
| 173 | + ->with($this->equalTo('/projects/project/packages/acme-website/package/'), $this->equalTo(['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null])) |
| 174 | + ->willReturn($expected); |
| 175 | + |
| 176 | + $this->assertSame($expected, $api->editCustomPackage($projectName, 'acme-website/package', '{}')); |
| 177 | + } |
| 178 | + |
| 179 | + public function testRemove() |
| 180 | + { |
| 181 | + $projectName = 'project'; |
| 182 | + $expected = []; |
| 183 | + |
| 184 | + /** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */ |
| 185 | + $api = $this->getApiMock(); |
| 186 | + $api->expects($this->once()) |
| 187 | + ->method('delete') |
| 188 | + ->with($this->equalTo('/projects/project/packages/acme-website/package/')) |
| 189 | + ->willReturn($expected); |
| 190 | + |
| 191 | + $this->assertSame($expected, $api->remove($projectName, 'acme-website/package')); |
| 192 | + } |
| 193 | + |
| 194 | + protected function getApiClass() |
| 195 | + { |
| 196 | + return Packages::class; |
| 197 | + } |
| 198 | +} |
0 commit comments