Skip to content

Commit

Permalink
Add CustomFields::list(), deprecate all()
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Sep 28, 2023
1 parent 8665629 commit 58a0546
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 127 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions src/Redmine/Api/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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) {
Expand Down
162 changes: 162 additions & 0 deletions tests/Unit/Api/CustomField/ListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

namespace Redmine\Tests\Unit\Api\CustomField;

use PHPUnit\Framework\TestCase;
use Redmine\Api\CustomField;
use Redmine\Client\Client;

/**
* Tests for CustomField::list()
*/
class ListTest extends TestCase
{
/**
* @covers \Redmine\Api\CustomField::list
* @covers \Redmine\Api\CustomField::get
* @covers \Redmine\Api\CustomField::retrieveAll
* @covers \Redmine\Api\CustomField::isNotNull
*/
public function testListWithoutParametersReturnsResponse()
{
// Test values
$response = '["API Response"]';
$expectedResponse = ['API Response'];

// 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($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));
}
}
125 changes: 0 additions & 125 deletions tests/Unit/Api/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*
Expand Down

0 comments on commit 58a0546

Please sign in to comment.