|
7 | 7 | use PHPUnit\Framework\TestCase; |
8 | 8 | use Redmine\Api\Attachment; |
9 | 9 | use Redmine\Client\Client; |
| 10 | +use Redmine\Http\HttpClient; |
| 11 | +use Redmine\Tests\Fixtures\AssertingHttpClient; |
10 | 12 |
|
11 | 13 | /** |
12 | 14 | * @author Malte Gerth <mail@malte-gerth.de> |
13 | 15 | */ |
14 | 16 | #[CoversClass(Attachment::class)] |
15 | 17 | class AttachmentTest extends TestCase |
16 | 18 | { |
| 19 | + public function testExtendingTheClassTriggersDeprecationWarning(): void |
| 20 | + { |
| 21 | + // PHPUnit 10 compatible way to test trigger_error(). |
| 22 | + set_error_handler( |
| 23 | + function ($errno, $errstr): bool { |
| 24 | + $this->assertSame( |
| 25 | + 'Class `Redmine\Api\Attachment` will declared as final in v3.0.0, stop extending it.', |
| 26 | + $errstr, |
| 27 | + ); |
| 28 | + |
| 29 | + restore_error_handler(); |
| 30 | + return true; |
| 31 | + }, |
| 32 | + E_USER_DEPRECATED, |
| 33 | + ); |
| 34 | + |
| 35 | + new class ($this->createStub(HttpClient::class)) extends Attachment {}; |
| 36 | + } |
| 37 | + |
| 38 | + public function testConstructorTriggersDeprecationWarning(): void |
| 39 | + { |
| 40 | + // PHPUnit 10 compatible way to test trigger_error(). |
| 41 | + set_error_handler( |
| 42 | + function ($errno, $errstr): bool { |
| 43 | + $this->assertSame( |
| 44 | + 'Method `Redmine\Api\Attachment::__construct()` is deprecated since v2.9.0 and will declared as private in v3.0.0, use `Redmine\Api\Attachment::fromHttpClient()` instead.', |
| 45 | + $errstr, |
| 46 | + ); |
| 47 | + |
| 48 | + restore_error_handler(); |
| 49 | + return true; |
| 50 | + }, |
| 51 | + E_USER_DEPRECATED, |
| 52 | + ); |
| 53 | + |
| 54 | + new Attachment($this->createStub(HttpClient::class)); |
| 55 | + } |
| 56 | + |
| 57 | + public function testLastCallFailedWithoutPreviousRequestReturnsTrue(): void |
| 58 | + { |
| 59 | + $api = Attachment::fromHttpClient($this->createStub(HttpClient::class)); |
| 60 | + |
| 61 | + // Perform the tests |
| 62 | + $this->assertTrue($api->lastCallFailed()); |
| 63 | + } |
| 64 | + |
17 | 65 | /** |
18 | 66 | * Test lastCallFailed(). |
19 | 67 | * |
20 | 68 | * @dataProvider responseCodeProvider |
21 | 69 | */ |
22 | 70 | #[DataProvider('responseCodeProvider')] |
23 | | - public function testLastCallFailedTrue(int $responseCode, bool $hasFailed): void |
| 71 | + public function testLastCallFailedReturnsCorrectValue(int $responseCode, bool $hasFailed): void |
24 | 72 | { |
25 | | - // Create the used mock objects |
26 | | - $client = $this->createMock(Client::class); |
27 | | - $client->expects($this->once()) |
28 | | - ->method('getLastResponseStatusCode') |
29 | | - ->willReturn($responseCode); |
| 73 | + $client = AssertingHttpClient::create( |
| 74 | + $this, |
| 75 | + [ |
| 76 | + 'GET', |
| 77 | + '/attachments/1.json', |
| 78 | + 'application/json', |
| 79 | + '', |
| 80 | + $responseCode, |
| 81 | + '', |
| 82 | + '', |
| 83 | + ], |
| 84 | + ); |
30 | 85 |
|
31 | 86 | // Create the object under test |
32 | | - $api = new Attachment($client); |
| 87 | + $api = Attachment::fromHttpClient($client); |
| 88 | + $api->show(1); |
33 | 89 |
|
34 | 90 | // Perform the tests |
35 | 91 | $this->assertSame($hasFailed, $api->lastCallFailed()); |
|
0 commit comments