Skip to content

Commit 3155355

Browse files
committed
Add News::list() and listByProject() methdos, deprecate all() method
1 parent a31c99d commit 3155355

File tree

5 files changed

+237
-3
lines changed

5 files changed

+237
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- New method `Redmine\Api\IssueRelation::listByIssueId()` to list relations from an issue.
1818
- New method `Redmine\Api\IssueStatus::list()` to list issue statuses.
1919
- New method `Redmine\Api\Membership::listByProject()` to list memberships from a project.
20+
- New method `Redmine\Api\News::list()` to list news from all project.
21+
- New method `Redmine\Api\News::listByProject()` to list news from a project.
2022

2123
### Deprecated
2224

@@ -28,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2830
- `Redmine\Api\IssueRelation::all()` is deprecated, use `Redmine\Api\IssueRelation::listByIssueId()` instead
2931
- `Redmine\Api\IssueStatus::all()` is deprecated, use `Redmine\Api\IssueStatus::list()` instead
3032
- `Redmine\Api\Membership::all()` is deprecated, use `Redmine\Api\Membership::listByProject()` instead
33+
- `Redmine\Api\News::all()` is deprecated, use `Redmine\Api\News::list()` or `Redmine\Api\News::listByProject()` instead
3134

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

src/Redmine/Api/News.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Redmine\Api;
44

5+
use Redmine\Exception\InvalidParameterException;
6+
57
/**
68
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News
79
*
@@ -11,9 +13,51 @@ class News extends AbstractApi
1113
{
1214
private $news = [];
1315

16+
/**
17+
* List news for a given project.
18+
*
19+
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News#GET
20+
*
21+
* @param string|int $projectIdentifier project id or literal identifier
22+
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
23+
*
24+
* @return array list of news found
25+
*/
26+
final public function listByProject($projectIdentifier, array $params = []): array
27+
{
28+
if (! is_int($projectIdentifier) && ! is_string($projectIdentifier)) {
29+
throw new InvalidParameterException(sprintf(
30+
'%s(): Argument #1 ($projectIdentifier) must be of type int or string',
31+
__METHOD__
32+
));
33+
}
34+
35+
$this->news = $this->retrieveData('/projects/'.strval($projectIdentifier).'/news.json', $params);
36+
37+
return $this->news;
38+
}
39+
40+
/**
41+
* List news for all projects.
42+
*
43+
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News#GET
44+
*
45+
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
46+
*
47+
* @return array list of news found
48+
*/
49+
final public function list(array $params = []): array
50+
{
51+
$this->news = $this->retrieveData('/news.json', $params);
52+
53+
return $this->news;
54+
}
55+
1456
/**
1557
* List news (if no $project is given, it will return ALL the news).
1658
*
59+
* @deprecated since v2.4.0, use list() or listByProject() instead.
60+
*
1761
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News#GET
1862
*
1963
* @param string|int $project project id or literal identifier [optional]
@@ -23,9 +67,12 @@ class News extends AbstractApi
2367
*/
2468
public function all($project = null, array $params = [])
2569
{
26-
$path = null === $project ? '/news.json' : '/projects/'.$project.'/news.json';
27-
$this->news = $this->retrieveData($path, $params);
70+
@trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` or `'.__CLASS__.'::listByProject()` instead.', E_USER_DEPRECATED);
2871

29-
return $this->news;
72+
if (null === $project) {
73+
return $this->list($params);
74+
} else {
75+
return $this->listByProject(strval($project), $params);
76+
}
3077
}
3178
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Redmine\Tests\Unit\Api\News;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Redmine\Api\News;
7+
use Redmine\Client\Client;
8+
9+
/**
10+
* Tests for News::listByProject()
11+
*/
12+
class ListByProjectTest extends TestCase
13+
{
14+
/**
15+
* @covers \Redmine\Api\News::listByProject
16+
*/
17+
public function testListByProjectWithoutParametersReturnsResponse()
18+
{
19+
// Test values
20+
$projectId = 5;
21+
$response = '["API Response"]';
22+
$expectedReturn = ['API Response'];
23+
24+
// Create the used mock objects
25+
$client = $this->createMock(Client::class);
26+
$client->expects($this->once())
27+
->method('requestGet')
28+
->with(
29+
$this->stringStartsWith('/projects/5/news.json')
30+
)
31+
->willReturn(true);
32+
$client->expects($this->exactly(1))
33+
->method('getLastResponseBody')
34+
->willReturn($response);
35+
$client->expects($this->exactly(1))
36+
->method('getLastResponseContentType')
37+
->willReturn('application/json');
38+
39+
// Create the object under test
40+
$api = new News($client);
41+
42+
// Perform the tests
43+
$this->assertSame($expectedReturn, $api->listByProject($projectId));
44+
}
45+
46+
/**
47+
* @covers \Redmine\Api\News::listByProject
48+
*/
49+
public function testListByProjectWithParametersReturnsResponse()
50+
{
51+
// Test values
52+
$projectId = 5;
53+
$parameters = ['not-used'];
54+
$response = '["API Response"]';
55+
$expectedReturn = ['API Response'];
56+
57+
// Create the used mock objects
58+
$client = $this->createMock(Client::class);
59+
$client->expects($this->once())
60+
->method('requestGet')
61+
->with(
62+
$this->stringContains('not-used')
63+
)
64+
->willReturn(true);
65+
$client->expects($this->exactly(1))
66+
->method('getLastResponseBody')
67+
->willReturn($response);
68+
$client->expects($this->exactly(1))
69+
->method('getLastResponseContentType')
70+
->willReturn('application/json');
71+
72+
// Create the object under test
73+
$api = new News($client);
74+
75+
// Perform the tests
76+
$this->assertSame($expectedReturn, $api->listByProject($projectId, $parameters));
77+
}
78+
}

tests/Unit/Api/News/ListTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Redmine\Tests\Unit\Api\News;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Redmine\Api\News;
7+
use Redmine\Client\Client;
8+
9+
/**
10+
* Tests for News::list()
11+
*/
12+
class ListTest extends TestCase
13+
{
14+
/**
15+
* @covers \Redmine\Api\News::list
16+
*/
17+
public function testListWithoutParametersReturnsResponse()
18+
{
19+
// Test values
20+
$response = '["API Response"]';
21+
$expectedReturn = ['API Response'];
22+
23+
// Create the used mock objects
24+
$client = $this->createMock(Client::class);
25+
$client->expects($this->once())
26+
->method('requestGet')
27+
->with(
28+
$this->stringStartsWith('/news.json')
29+
)
30+
->willReturn(true);
31+
$client->expects($this->exactly(1))
32+
->method('getLastResponseBody')
33+
->willReturn($response);
34+
$client->expects($this->exactly(1))
35+
->method('getLastResponseContentType')
36+
->willReturn('application/json');
37+
38+
// Create the object under test
39+
$api = new News($client);
40+
41+
// Perform the tests
42+
$this->assertSame($expectedReturn, $api->list());
43+
}
44+
45+
/**
46+
* @covers \Redmine\Api\News::list
47+
*/
48+
public function testListWithParametersReturnsResponse()
49+
{
50+
// Test values
51+
$parameters = ['not-used'];
52+
$response = '["API Response"]';
53+
$expectedReturn = ['API Response'];
54+
55+
// Create the used mock objects
56+
$client = $this->createMock(Client::class);
57+
$client->expects($this->once())
58+
->method('requestGet')
59+
->with(
60+
$this->logicalAnd(
61+
$this->stringStartsWith('/news.json'),
62+
$this->stringContains('not-used')
63+
)
64+
)
65+
->willReturn(true);
66+
$client->expects($this->exactly(1))
67+
->method('getLastResponseBody')
68+
->willReturn($response);
69+
$client->expects($this->exactly(1))
70+
->method('getLastResponseContentType')
71+
->willReturn('application/json');
72+
73+
// Create the object under test
74+
$api = new News($client);
75+
76+
// Perform the tests
77+
$this->assertSame($expectedReturn, $api->list($parameters));
78+
}
79+
}

tests/Unit/Api/NewsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Redmine\Api\News;
77
use Redmine\Client\Client;
8+
use Redmine\Tests\Fixtures\MockClient;
89

910
/**
1011
* @coversDefaultClass \Redmine\Api\News
@@ -13,6 +14,32 @@
1314
*/
1415
class NewsTest extends TestCase
1516
{
17+
/**
18+
* Test all().
19+
*
20+
* @covers ::all
21+
*/
22+
public function testAllTriggersDeprecationWarning()
23+
{
24+
$api = new News(MockClient::create());
25+
26+
// PHPUnit 10 compatible way to test trigger_error().
27+
set_error_handler(
28+
function ($errno, $errstr): bool {
29+
$this->assertSame(
30+
'`Redmine\Api\News::all()` is deprecated since v2.4.0, use `Redmine\Api\News::list()` or `Redmine\Api\News::listByProject()` instead.',
31+
$errstr
32+
);
33+
34+
restore_error_handler();
35+
return true;
36+
},
37+
E_USER_DEPRECATED
38+
);
39+
40+
$api->all(5);
41+
}
42+
1643
/**
1744
* Test all().
1845
*

0 commit comments

Comments
 (0)