|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Redmine\Tests\Unit\Api\IssueCategory; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Redmine\Api\IssueCategory; |
| 11 | +use Redmine\Exception\InvalidParameterException; |
| 12 | +use Redmine\Http\HttpClient; |
| 13 | +use Redmine\Tests\Fixtures\AssertingHttpClient; |
| 14 | +use stdClass; |
| 15 | + |
| 16 | +#[CoversClass(IssueCategory::class)] |
| 17 | +class ListNamesByProjectTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @dataProvider getListNamesByProjectData |
| 21 | + */ |
| 22 | + #[DataProvider('getListNamesByProjectData')] |
| 23 | + public function testListNamesByProjectReturnsCorrectResponse($projectIdentifier, $expectedPath, $responseCode, $response, $expectedResponse) |
| 24 | + { |
| 25 | + $client = AssertingHttpClient::create( |
| 26 | + $this, |
| 27 | + [ |
| 28 | + 'GET', |
| 29 | + $expectedPath, |
| 30 | + 'application/json', |
| 31 | + '', |
| 32 | + $responseCode, |
| 33 | + 'application/json', |
| 34 | + $response, |
| 35 | + ], |
| 36 | + ); |
| 37 | + |
| 38 | + // Create the object under test |
| 39 | + $api = new IssueCategory($client); |
| 40 | + |
| 41 | + // Perform the tests |
| 42 | + $this->assertSame($expectedResponse, $api->listNamesByProject($projectIdentifier)); |
| 43 | + } |
| 44 | + |
| 45 | + public static function getListNamesByProjectData(): array |
| 46 | + { |
| 47 | + return [ |
| 48 | + 'test without issue categories' => [ |
| 49 | + 5, |
| 50 | + '/projects/5/issue_categories.json', |
| 51 | + 201, |
| 52 | + <<<JSON |
| 53 | + { |
| 54 | + "issue_categories": [] |
| 55 | + } |
| 56 | + JSON, |
| 57 | + [], |
| 58 | + ], |
| 59 | + 'test with multiple categories' => [ |
| 60 | + 'test-project', |
| 61 | + '/projects/test-project/issue_categories.json', |
| 62 | + 201, |
| 63 | + <<<JSON |
| 64 | + { |
| 65 | + "issue_categories": [ |
| 66 | + {"id": 9, "name": "IssueCategory 1"}, |
| 67 | + {"id": 8, "name": "IssueCategory 2"}, |
| 68 | + {"id": 7, "name": "IssueCategory 3"} |
| 69 | + ] |
| 70 | + } |
| 71 | + JSON, |
| 72 | + [ |
| 73 | + 9 => "IssueCategory 1", |
| 74 | + 8 => "IssueCategory 2", |
| 75 | + 7 => "IssueCategory 3", |
| 76 | + ], |
| 77 | + ], |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + public function testListNamesByProjectCallsHttpClientOnlyOnce() |
| 82 | + { |
| 83 | + $client = AssertingHttpClient::create( |
| 84 | + $this, |
| 85 | + [ |
| 86 | + 'GET', |
| 87 | + '/projects/5/issue_categories.json', |
| 88 | + 'application/json', |
| 89 | + '', |
| 90 | + 200, |
| 91 | + 'application/json', |
| 92 | + <<<JSON |
| 93 | + { |
| 94 | + "issue_categories": [ |
| 95 | + { |
| 96 | + "id": 1, |
| 97 | + "name": "IssueCategory 1" |
| 98 | + } |
| 99 | + ] |
| 100 | + } |
| 101 | + JSON, |
| 102 | + ], |
| 103 | + ); |
| 104 | + |
| 105 | + // Create the object under test |
| 106 | + $api = new IssueCategory($client); |
| 107 | + |
| 108 | + // Perform the tests |
| 109 | + $this->assertSame([1 => 'IssueCategory 1'], $api->listNamesByProject(5)); |
| 110 | + $this->assertSame([1 => 'IssueCategory 1'], $api->listNamesByProject(5)); |
| 111 | + $this->assertSame([1 => 'IssueCategory 1'], $api->listNamesByProject(5)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @dataProvider getInvalidProjectIdentifiers |
| 116 | + */ |
| 117 | + #[DataProvider('getInvalidProjectIdentifiers')] |
| 118 | + public function testListNamesByProjectWithWrongProjectIdentifierThrowsException($projectIdentifier) |
| 119 | + { |
| 120 | + $api = new IssueCategory($this->createMock(HttpClient::class)); |
| 121 | + |
| 122 | + $this->expectException(InvalidParameterException::class); |
| 123 | + $this->expectExceptionMessage('Redmine\Api\IssueCategory::listNamesByProject(): Argument #1 ($projectIdentifier) must be of type int or string'); |
| 124 | + |
| 125 | + $api->listNamesByProject($projectIdentifier); |
| 126 | + } |
| 127 | + |
| 128 | + public static function getInvalidProjectIdentifiers(): array |
| 129 | + { |
| 130 | + return [ |
| 131 | + 'null' => [null], |
| 132 | + 'true' => [true], |
| 133 | + 'false' => [false], |
| 134 | + 'float' => [0.0], |
| 135 | + 'array' => [[]], |
| 136 | + 'object' => [new stdClass()], |
| 137 | + ]; |
| 138 | + } |
| 139 | +} |
0 commit comments