|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Redmine\Tests\Unit\Api\Attachment; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 6 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Redmine\Api\Attachment; |
| 9 | +use Redmine\Exception\UnexpectedResponseException; |
| 10 | +use Redmine\Tests\Fixtures\AssertingHttpClient; |
| 11 | + |
| 12 | +#[CoversClass(Attachment::class)] |
| 13 | +class UpdateTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @dataProvider getUpdateData |
| 17 | + */ |
| 18 | + #[DataProvider('getUpdateData')] |
| 19 | + public function testUpdateReturnsCorrectResponse($id, array $params, $expectedPath, $expectedContent, $expectedReturn) |
| 20 | + { |
| 21 | + $client = AssertingHttpClient::create( |
| 22 | + $this, |
| 23 | + [ |
| 24 | + 'PUT', |
| 25 | + $expectedPath, |
| 26 | + 'application/json', |
| 27 | + $expectedContent, |
| 28 | + 204, |
| 29 | + '', |
| 30 | + '' |
| 31 | + ] |
| 32 | + ); |
| 33 | + |
| 34 | + // Create the object under test |
| 35 | + $api = new Attachment($client); |
| 36 | + |
| 37 | + // Perform the tests |
| 38 | + $this->assertSame($expectedReturn, $api->update($id, $params)); |
| 39 | + } |
| 40 | + |
| 41 | + public static function getUpdateData(): array |
| 42 | + { |
| 43 | + return [ |
| 44 | + 'test with all params' => [ |
| 45 | + 5, |
| 46 | + [ |
| 47 | + 'filename' => 'renamed.zip', |
| 48 | + 'description' => 'updated', |
| 49 | + ], |
| 50 | + '/attachments/5.json', |
| 51 | + '{"attachment":{"filename":"renamed.zip","description":"updated"}}', |
| 52 | + true, |
| 53 | + ], |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + public function testUpdateThrowsUnexpectedResponseException() |
| 58 | + { |
| 59 | + $client = AssertingHttpClient::create( |
| 60 | + $this, |
| 61 | + [ |
| 62 | + 'PUT', |
| 63 | + '/attachments/5.json', |
| 64 | + 'application/json', |
| 65 | + '{"attachment":[]}', |
| 66 | + 403, |
| 67 | + '', |
| 68 | + '', |
| 69 | + ] |
| 70 | + ); |
| 71 | + |
| 72 | + $api = new Attachment($client); |
| 73 | + |
| 74 | + $this->expectException(UnexpectedResponseException::class); |
| 75 | + $this->expectExceptionMessage('The Redmine server replied with an unexpected response.'); |
| 76 | + |
| 77 | + $api->update(5, []); |
| 78 | + } |
| 79 | +} |
0 commit comments