Skip to content

Commit

Permalink
Add News::list() and listByProject() methdos, deprecate all() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Oct 9, 2023
1 parent a31c99d commit 3155355
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New method `Redmine\Api\IssueRelation::listByIssueId()` to list relations from an issue.
- New method `Redmine\Api\IssueStatus::list()` to list issue statuses.
- New method `Redmine\Api\Membership::listByProject()` to list memberships from a project.
- New method `Redmine\Api\News::list()` to list news from all project.
- New method `Redmine\Api\News::listByProject()` to list news from a project.

### Deprecated

Expand All @@ -28,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Redmine\Api\IssueRelation::all()` is deprecated, use `Redmine\Api\IssueRelation::listByIssueId()` instead
- `Redmine\Api\IssueStatus::all()` is deprecated, use `Redmine\Api\IssueStatus::list()` instead
- `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

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

Expand Down
53 changes: 50 additions & 3 deletions src/Redmine/Api/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Redmine\Api;

use Redmine\Exception\InvalidParameterException;

/**
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News
*
Expand All @@ -11,9 +13,51 @@ class News extends AbstractApi
{
private $news = [];

/**
* List news for a given project.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News#GET
*
* @param string|int $projectIdentifier project id or literal identifier
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
*
* @return array list of news found
*/
final public function listByProject($projectIdentifier, array $params = []): array
{
if (! is_int($projectIdentifier) && ! is_string($projectIdentifier)) {
throw new InvalidParameterException(sprintf(
'%s(): Argument #1 ($projectIdentifier) must be of type int or string',
__METHOD__
));

Check warning on line 32 in src/Redmine/Api/News.php

View check run for this annotation

Codecov / codecov/patch

src/Redmine/Api/News.php#L29-L32

Added lines #L29 - L32 were not covered by tests
}

$this->news = $this->retrieveData('/projects/'.strval($projectIdentifier).'/news.json', $params);

return $this->news;
}

/**
* List news for all projects.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News#GET
*
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
*
* @return array list of news found
*/
final public function list(array $params = []): array
{
$this->news = $this->retrieveData('/news.json', $params);

return $this->news;
}

/**
* List news (if no $project is given, it will return ALL the news).
*
* @deprecated since v2.4.0, use list() or listByProject() instead.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News#GET
*
* @param string|int $project project id or literal identifier [optional]
Expand All @@ -23,9 +67,12 @@ class News extends AbstractApi
*/
public function all($project = null, array $params = [])
{
$path = null === $project ? '/news.json' : '/projects/'.$project.'/news.json';
$this->news = $this->retrieveData($path, $params);
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` or `'.__CLASS__.'::listByProject()` instead.', E_USER_DEPRECATED);

return $this->news;
if (null === $project) {
return $this->list($params);
} else {
return $this->listByProject(strval($project), $params);
}
}
}
78 changes: 78 additions & 0 deletions tests/Unit/Api/News/ListByProjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Redmine\Tests\Unit\Api\News;

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

/**
* Tests for News::listByProject()
*/
class ListByProjectTest extends TestCase
{
/**
* @covers \Redmine\Api\News::listByProject
*/
public function testListByProjectWithoutParametersReturnsResponse()
{
// Test values
$projectId = 5;
$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('/projects/5/news.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 News($client);

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

/**
* @covers \Redmine\Api\News::listByProject
*/
public function testListByProjectWithParametersReturnsResponse()
{
// Test values
$projectId = 5;
$parameters = ['not-used'];
$response = '["API Response"]';
$expectedReturn = ['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 News($client);

// Perform the tests
$this->assertSame($expectedReturn, $api->listByProject($projectId, $parameters));
}
}
79 changes: 79 additions & 0 deletions tests/Unit/Api/News/ListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Redmine\Tests\Unit\Api\News;

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

/**
* Tests for News::list()
*/
class ListTest extends TestCase
{
/**
* @covers \Redmine\Api\News::list
*/
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('/news.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 News($client);

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

/**
* @covers \Redmine\Api\News::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->once())
->method('requestGet')
->with(
$this->logicalAnd(
$this->stringStartsWith('/news.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 News($client);

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

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

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

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

$api->all(5);
}

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

0 comments on commit 3155355

Please sign in to comment.