diff --git a/CHANGELOG.md b/CHANGELOG.md index 72fbf8a0..3e38a612 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,12 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Allow `Psr\Http\Message\RequestFactoryInterface` as Argument #2 ($requestFactory) in `Redmine\Client\Psr18Client::__construct()` +- New method `Redmine\Api\CustomField::list()` to list custom fields. - Added support for PHP 8.2 ### Deprecated - Providing Argument #2 ($requestFactory) in `Redmine\Client\Psr18Client::__construct()` as type `Psr\Http\Message\ServerRequestFactoryInterface` is deprecated, provide as type `Psr\Http\Message\RequestFactoryInterface` instead - `Redmine\Api\AbstractApi::attachCustomFieldXML()` is deprecated +- `Redmine\Api\CustomField::all()` is deprecated, use `Redmine\Api\CustomField::list()` instead - `Redmine\Api\Project::prepareParamsXml()` is deprecated ## [v2.2.0](https://github.com/kbsali/php-redmine-api/compare/v2.1.1...v2.2.0) - 2022-03-01 diff --git a/src/Redmine/Api/CustomField.php b/src/Redmine/Api/CustomField.php index 523a3726..7c3b7995 100644 --- a/src/Redmine/Api/CustomField.php +++ b/src/Redmine/Api/CustomField.php @@ -22,13 +22,31 @@ class CustomField extends AbstractApi * * @return array list of custom fields found */ - public function all(array $params = []) + public function list(array $params = []): array { $this->customFields = $this->retrieveData('/custom_fields.json', $params); return $this->customFields; } + /** + * List custom fields. + * + * @deprecated sind v2.4.0, use list() instead. + * + * @see http://www.redmine.org/projects/redmine/wiki/Rest_CustomFields#GET + * + * @param array $params optional parameters to be passed to the api (offset, limit, ...) + * + * @return array list of custom fields found + */ + public function all(array $params = []) + { + @trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` instead.', E_USER_DEPRECATED); + + return $this->list($params); + } + /** * Returns an array of custom fields with name/id pairs. * @@ -40,7 +58,7 @@ public function all(array $params = []) public function listing($forceUpdate = false, array $params = []) { if (empty($this->customFields) || $forceUpdate) { - $this->all($params); + $this->list($params); } $ret = []; foreach ($this->customFields['custom_fields'] as $e) { diff --git a/tests/Unit/Api/CustomField/ListTest.php b/tests/Unit/Api/CustomField/ListTest.php new file mode 100644 index 00000000..4a2e0274 --- /dev/null +++ b/tests/Unit/Api/CustomField/ListTest.php @@ -0,0 +1,162 @@ +createMock(Client::class); + $client->expects($this->once()) + ->method('requestGet') + ->with( + $this->stringStartsWith('/custom_fields.json') + ) + ->willReturn(true); + $client->expects($this->exactly(1)) + ->method('getLastResponseBody') + ->willReturn($response); + $client->expects($this->exactly(1)) + ->method('getLastResponseContentType') + ->willReturn('application/json'); + + // Create the object under test + $api = new CustomField($client); + + // Perform the tests + $this->assertSame($expectedResponse, $api->list()); + } + + /** + * @covers \Redmine\Api\CustomField::list + * @covers \Redmine\Api\CustomField::get + * @covers \Redmine\Api\CustomField::retrieveAll + * @covers \Redmine\Api\CustomField::isNotNull + */ + public function testListWithParametersReturnsResponse() + { + // Test values + $allParameters = ['not-used']; + $response = '["API Response"]'; + $expectedResponse = ['API Response']; + + // Create the used mock objects + $client = $this->createMock(Client::class); + $client->expects($this->once()) + ->method('requestGet') + ->with( + $this->stringContains('not-used') + ) + ->willReturn(true); + $client->expects($this->exactly(1)) + ->method('getLastResponseBody') + ->willReturn($response); + $client->expects($this->exactly(1)) + ->method('getLastResponseContentType') + ->willReturn('application/json'); + + // Create the object under test + $api = new CustomField($client); + + // Perform the tests + $this->assertSame($expectedResponse, $api->list($allParameters)); + } + + /** + * @covers \Redmine\Api\CustomField::list + * @covers \Redmine\Api\CustomField::get + * @covers \Redmine\Api\CustomField::retrieveAll + * @covers \Redmine\Api\CustomField::isNotNull + */ + public function testListWithHighLimitParametersReturnsResponse() + { + // Test values + $response = '{"limit":"100","items":[]}'; + $allParameters = ['limit' => 250]; + $expectedResponse = [ + 'limit' => ['100', '100', '100'], // TODO: Check response created by array_merge_recursive() + 'items' => [], + ]; + + // Create the used mock objects + $client = $this->createMock(Client::class); + $client->expects($this->exactly(3)) + ->method('requestGet') + ->with( + $this->stringStartsWith('/custom_fields.json') + ) + ->willReturn(true); + $client->expects($this->exactly(3)) + ->method('getLastResponseBody') + ->willReturn($response); + $client->expects($this->exactly(3)) + ->method('getLastResponseContentType') + ->willReturn('application/json'); + + // Create the object under test + $api = new CustomField($client); + + // Perform the tests + $this->assertSame($expectedResponse, $api->list($allParameters)); + } + + /** + * Test list(). + * + * @covers \Redmine\Api\CustomField::list + * @covers \Redmine\Api\CustomField::get + * @covers \Redmine\Api\CustomField::retrieveAll + * @covers \Redmine\Api\CustomField::isNotNull + */ + public function testListCallsEndpointUntilOffsetIsHigherThanTotalCount() + { + // Test values + $response = '{"limit":"100","offset":"10","total_count":"5","items":[]}'; + $allParameters = ['limit' => 250]; + $returnDataSet = [ + 'limit' => '100', + 'offset' => '10', + 'total_count' => '5', + 'items' => [], + ]; + + // Create the used mock objects + $client = $this->createMock(Client::class); + $client->expects($this->once()) + ->method('requestGet') + ->with( + $this->stringStartsWith('/custom_fields.json') + ) + ->willReturn(true); + $client->expects($this->exactly(1)) + ->method('getLastResponseBody') + ->willReturn($response); + $client->expects($this->exactly(1)) + ->method('getLastResponseContentType') + ->willReturn('application/json'); + + // Create the object under test + $api = new CustomField($client); + + // Perform the tests + $this->assertSame($returnDataSet, $api->list($allParameters)); + } +} diff --git a/tests/Unit/Api/CustomFieldTest.php b/tests/Unit/Api/CustomFieldTest.php index a3481100..29869937 100644 --- a/tests/Unit/Api/CustomFieldTest.php +++ b/tests/Unit/Api/CustomFieldTest.php @@ -50,131 +50,6 @@ public function testAllReturnsClientGetResponse() $this->assertSame($expectedResponse, $api->all()); } - /** - * Test all(). - * - * @covers ::all - * @covers ::get - * @covers ::retrieveAll - * @covers ::isNotNull - * @test - */ - public function testAllReturnsClientGetResponseWithParameters() - { - // Test values - $allParameters = ['not-used']; - $response = '["API Response"]'; - $expectedResponse = ['API Response']; - - // Create the used mock objects - $client = $this->createMock(Client::class); - $client->expects($this->once()) - ->method('requestGet') - ->with( - $this->stringContains('not-used') - ) - ->willReturn(true); - $client->expects($this->exactly(1)) - ->method('getLastResponseBody') - ->willReturn($response); - $client->expects($this->exactly(1)) - ->method('getLastResponseContentType') - ->willReturn('application/json'); - - // Create the object under test - $api = new CustomField($client); - - // Perform the tests - $this->assertSame($expectedResponse, $api->all($allParameters)); - } - - /** - * Test all(). - * - * @covers ::all - * @covers ::get - * @covers ::retrieveAll - * @covers ::isNotNull - * @test - */ - public function testAllReturnsClientGetResponseWithHighLimit() - { - // Test values - $response = '{"limit":"100","items":[]}'; - $allParameters = ['limit' => 250]; - $expectedResponse = [ - 'limit' => ['100', '100', '100'], // TODO: Check response created by array_merge_recursive() - 'items' => [], - ]; - - // Create the used mock objects - $client = $this->createMock(Client::class); - $client->expects($this->exactly(3)) - ->method('requestGet') - ->with( - $this->stringStartsWith('/custom_fields.json') - ) - ->willReturn(true); - $client->expects($this->exactly(3)) - ->method('getLastResponseBody') - ->willReturn($response); - $client->expects($this->exactly(3)) - ->method('getLastResponseContentType') - ->willReturn('application/json'); - - // Create the object under test - $api = new CustomField($client); - - // Perform the tests - $this->assertSame($expectedResponse, $api->all($allParameters)); - } - - /** - * Test all(). - * - * @covers ::all - * @covers ::get - * @covers ::retrieveAll - * @covers ::isNotNull - * @test - */ - public function testAllCallsEndpointUntilOffsetIsHigherThanTotalCount() - { - // Test values - $response = '{"limit":"100","offset":"10","total_count":"5","items":[]}'; - $allParameters = ['limit' => 250]; - $returnDataSet = [ - 'limit' => '100', - 'offset' => '10', - 'total_count' => '5', - 'items' => [], - ]; - - // Create the used mock objects - $client = $this->createMock(Client::class); - $client->expects($this->once()) - ->method('requestGet') - ->with( - $this->stringStartsWith('/custom_fields.json') - ) - ->willReturn(true); - $client->expects($this->exactly(1)) - ->method('getLastResponseBody') - ->willReturn($response); - $client->expects($this->exactly(1)) - ->method('getLastResponseContentType') - ->willReturn('application/json'); - - // Create the object under test - $api = new CustomField($client); - - // Perform the tests - $retrievedDataSet = $api->all($allParameters); - $this->assertTrue(is_array($retrievedDataSet)); - $this->assertArrayHasKey('limit', $retrievedDataSet); - $this->assertArrayHasKey('items', $retrievedDataSet); - } - /** * Test listing(). *