|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Redmine\Tests\Unit\Api\Tracker; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Redmine\Api\Tracker; |
| 11 | +use Redmine\Tests\Fixtures\AssertingHttpClient; |
| 12 | + |
| 13 | +#[CoversClass(Tracker::class)] |
| 14 | +class ListNamesTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @dataProvider getListNamesData |
| 18 | + */ |
| 19 | + #[DataProvider('getListNamesData')] |
| 20 | + public function testListNamesReturnsCorrectResponse($expectedPath, $responseCode, $response, $expectedResponse) |
| 21 | + { |
| 22 | + $client = AssertingHttpClient::create( |
| 23 | + $this, |
| 24 | + [ |
| 25 | + 'GET', |
| 26 | + $expectedPath, |
| 27 | + 'application/json', |
| 28 | + '', |
| 29 | + $responseCode, |
| 30 | + 'application/json', |
| 31 | + $response, |
| 32 | + ], |
| 33 | + ); |
| 34 | + |
| 35 | + // Create the object under test |
| 36 | + $api = new Tracker($client); |
| 37 | + |
| 38 | + // Perform the tests |
| 39 | + $this->assertSame($expectedResponse, $api->listNames()); |
| 40 | + } |
| 41 | + |
| 42 | + public static function getListNamesData(): array |
| 43 | + { |
| 44 | + return [ |
| 45 | + 'test without trackers' => [ |
| 46 | + '/trackers.json', |
| 47 | + 201, |
| 48 | + <<<JSON |
| 49 | + { |
| 50 | + "trackers": [] |
| 51 | + } |
| 52 | + JSON, |
| 53 | + [], |
| 54 | + ], |
| 55 | + 'test with multiple trackers' => [ |
| 56 | + '/trackers.json', |
| 57 | + 201, |
| 58 | + <<<JSON |
| 59 | + { |
| 60 | + "trackers": [ |
| 61 | + {"id": 7, "name": "Tracker 3"}, |
| 62 | + {"id": 8, "name": "Tracker 2"}, |
| 63 | + {"id": 9, "name": "Tracker 1"} |
| 64 | + ] |
| 65 | + } |
| 66 | + JSON, |
| 67 | + [ |
| 68 | + 7 => "Tracker 3", |
| 69 | + 8 => "Tracker 2", |
| 70 | + 9 => "Tracker 1", |
| 71 | + ], |
| 72 | + ], |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + public function testListNamesCallsHttpClientOnlyOnce() |
| 77 | + { |
| 78 | + $client = AssertingHttpClient::create( |
| 79 | + $this, |
| 80 | + [ |
| 81 | + 'GET', |
| 82 | + '/trackers.json', |
| 83 | + 'application/json', |
| 84 | + '', |
| 85 | + 200, |
| 86 | + 'application/json', |
| 87 | + <<<JSON |
| 88 | + { |
| 89 | + "trackers": [ |
| 90 | + { |
| 91 | + "id": 1, |
| 92 | + "name": "Tracker 1" |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | + JSON, |
| 97 | + ], |
| 98 | + ); |
| 99 | + |
| 100 | + // Create the object under test |
| 101 | + $api = new Tracker($client); |
| 102 | + |
| 103 | + // Perform the tests |
| 104 | + $this->assertSame([1 => 'Tracker 1'], $api->listNames()); |
| 105 | + $this->assertSame([1 => 'Tracker 1'], $api->listNames()); |
| 106 | + $this->assertSame([1 => 'Tracker 1'], $api->listNames()); |
| 107 | + } |
| 108 | +} |
0 commit comments