Skip to content

Commit

Permalink
Add Query::list() method, deprecate all() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Oct 10, 2023
1 parent 34db28d commit 0fb1493
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New method `Redmine\Api\News::list()` to list news from all project.
- New method `Redmine\Api\News::listByProject()` to list news from a project.
- New method `Redmine\Api\Project::list()` to list projects.
- New method `Redmine\Api\Query::list()` to list projects.

### Deprecated

Expand All @@ -33,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Redmine\Api\Membership::all()` is deprecated, use `Redmine\Api\Membership::listByProject()` instead
- `Redmine\Api\News::all()` is deprecated, use `Redmine\Api\News::list()` or `Redmine\Api\News::listByProject()` instead
- `Redmine\Api\Project::all()` is deprecated, use `Redmine\Api\Project::list()` instead
- `Redmine\Api\Query::all()` is deprecated, use `Redmine\Api\Query::list()` instead

## [v2.3.0](https://github.com/kbsali/php-redmine-api/compare/v2.2.0...v2.3.0) - 2023-10-09

Expand Down
20 changes: 19 additions & 1 deletion src/Redmine/Api/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,28 @@ class Query extends AbstractApi
*
* @return array list of queries found
*/
public function all(array $params = [])
final public function list(array $params = []): array
{
$this->query = $this->retrieveData('/queries.json', $params);

return $this->query;
}

/**
* Returns the list of all custom queries visible by the user (public and private queries) for all projects.
*
* @deprecated since v2.4.0, use list() instead.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_Queries#GET
*
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
*
* @return array list of queries 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);
}
}
73 changes: 73 additions & 0 deletions tests/Unit/Api/Query/ListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Redmine\Tests\Unit\Api\Query;

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

/**
* @covers \Redmine\Api\Query::list
*/
class ListTest extends TestCase
{
public function testListWithoutParametersReturnsResponse()
{
// Test values
$response = '["API Response"]';
$expectedReturn = ['API Response'];

// Create the used mock objects
$client = $this->createMock(Client::class);
$client->expects($this->once())
->method('requestGet')
->with(
$this->stringStartsWith('/queries.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 Query($client);

// Perform the tests
$this->assertSame($expectedReturn, $api->list());
}

public function testListWithParametersReturnsResponse()
{
// Test values
$parameters = ['not-used'];
$response = '["API Response"]';
$expectedReturn = ['API Response'];

// Create the used mock objects
$client = $this->createMock(Client::class);
$client->expects($this->any())
->method('requestGet')
->with(
$this->logicalAnd(
$this->stringStartsWith('/queries.json'),
$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 Query($client);

// Perform the tests
$this->assertSame($expectedReturn, $api->list($parameters));
}
}
27 changes: 27 additions & 0 deletions tests/Unit/Api/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Redmine\Api\Query;
use Redmine\Client\Client;
use Redmine\Tests\Fixtures\MockClient;

/**
* @coversDefaultClass \Redmine\Api\Query
Expand All @@ -13,6 +14,32 @@
*/
class QueryTest extends TestCase
{
/**
* Test all().
*
* @covers ::all
*/
public function testAllTriggersDeprecationWarning()
{
$api = new Query(MockClient::create());

// PHPUnit 10 compatible way to test trigger_error().
set_error_handler(
function ($errno, $errstr): bool {
$this->assertSame(
'`Redmine\Api\Query::all()` is deprecated since v2.4.0, use `Redmine\Api\Query::list()` instead.',
$errstr
);

restore_error_handler();
return true;
},
E_USER_DEPRECATED
);

$api->all();
}

/**
* Test all().
*
Expand Down

0 comments on commit 0fb1493

Please sign in to comment.