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