|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Redmine\Tests\Unit\Api\Membership; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Redmine\Api\Membership; |
| 7 | +use Redmine\Exception\MissingParameterException; |
| 8 | +use Redmine\Http\HttpClient; |
| 9 | +use Redmine\Tests\Fixtures\AssertingHttpClient; |
| 10 | +use SimpleXMLElement; |
| 11 | + |
| 12 | +/** |
| 13 | + * @covers \Redmine\Api\Membership::update |
| 14 | + */ |
| 15 | +class UpdateTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @dataProvider getUpdateData |
| 19 | + */ |
| 20 | + public function testUpdateReturnsCorrectResponse($id, $parameters, $expectedPath, $expectedBody, $responseCode, $response) |
| 21 | + { |
| 22 | + $client = AssertingHttpClient::create( |
| 23 | + $this, |
| 24 | + [ |
| 25 | + 'PUT', |
| 26 | + $expectedPath, |
| 27 | + 'application/xml', |
| 28 | + $expectedBody, |
| 29 | + $responseCode, |
| 30 | + '', |
| 31 | + $response |
| 32 | + ] |
| 33 | + ); |
| 34 | + |
| 35 | + // Create the object under test |
| 36 | + $api = new Membership($client); |
| 37 | + |
| 38 | + // Perform the tests |
| 39 | + $this->assertSame('', $api->update($id, $parameters)); |
| 40 | + } |
| 41 | + |
| 42 | + public static function getUpdateData(): array |
| 43 | + { |
| 44 | + return [ |
| 45 | + 'test with one role_id' => [ |
| 46 | + 5, |
| 47 | + ['user_id' => 4, 'role_ids' => [2]], |
| 48 | + '/memberships/5.xml', |
| 49 | + '<?xml version="1.0" encoding="UTF-8"?><membership><role_ids type="array"><role_id>2</role_id></role_ids><user_id>4</user_id> |
| 50 | + </membership>', |
| 51 | + 204, |
| 52 | + '', |
| 53 | + ], |
| 54 | + 'test with multiple role_ids' => [ |
| 55 | + 5, |
| 56 | + ['user_id' => 4, 'role_ids' => [5, 6]], |
| 57 | + '/memberships/5.xml', |
| 58 | + '<?xml version="1.0" encoding="UTF-8"?><membership><role_ids type="array"><role_id>5</role_id><role_id>6</role_id></role_ids><user_id>4</user_id></membership>', |
| 59 | + 204, |
| 60 | + '', |
| 61 | + ], |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + public function testUpdateReturnsEmptyString() |
| 66 | + { |
| 67 | + $client = AssertingHttpClient::create( |
| 68 | + $this, |
| 69 | + [ |
| 70 | + 'PUT', |
| 71 | + '/memberships/5.xml', |
| 72 | + 'application/xml', |
| 73 | + '<?xml version="1.0" encoding="UTF-8"?><membership><role_ids>2</role_ids><user_id>4</user_id></membership>', |
| 74 | + 500, |
| 75 | + '', |
| 76 | + '' |
| 77 | + ] |
| 78 | + ); |
| 79 | + |
| 80 | + // Create the object under test |
| 81 | + $api = new Membership($client); |
| 82 | + |
| 83 | + // Perform the tests |
| 84 | + $return = $api->update(5, ['user_id' => 4, 'role_ids' => 2]); |
| 85 | + |
| 86 | + $this->assertSame('', $return); |
| 87 | + } |
| 88 | + |
| 89 | + public function testUpdateThrowsExceptionWithEmptyParameters() |
| 90 | + { |
| 91 | + // Create the used mock objects |
| 92 | + $client = $this->createMock(HttpClient::class); |
| 93 | + |
| 94 | + // Create the object under test |
| 95 | + $api = new Membership($client); |
| 96 | + |
| 97 | + $this->expectException(MissingParameterException::class); |
| 98 | + $this->expectExceptionMessage('Theses parameters are mandatory: `role_ids`'); |
| 99 | + |
| 100 | + // Perform the tests |
| 101 | + $api->update(5); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @dataProvider incompleteUpdateParameterProvider |
| 106 | + */ |
| 107 | + public function testUpdateThrowsExceptionIfMandatoyParametersAreMissing($parameters) |
| 108 | + { |
| 109 | + // Create the used mock objects |
| 110 | + $client = $this->createMock(HttpClient::class); |
| 111 | + |
| 112 | + // Create the object under test |
| 113 | + $api = new Membership($client); |
| 114 | + |
| 115 | + $this->expectException(MissingParameterException::class); |
| 116 | + $this->expectExceptionMessage('Theses parameters are mandatory: `role_ids`'); |
| 117 | + |
| 118 | + // Perform the tests |
| 119 | + $api->update(5, $parameters); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Provider for incomplete create parameters. |
| 124 | + * |
| 125 | + * @return array[] |
| 126 | + */ |
| 127 | + public static function incompleteUpdateParameterProvider(): array |
| 128 | + { |
| 129 | + return [ |
| 130 | + 'missing all mandatory parameters' => [ |
| 131 | + [], |
| 132 | + ], |
| 133 | + 'missing `role_ids` parameter' => [ |
| 134 | + [ |
| 135 | + 'user_id' => 4, |
| 136 | + ], |
| 137 | + ], |
| 138 | + ]; |
| 139 | + } |
| 140 | +} |
0 commit comments