|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Coding\Tests; |
| 4 | + |
| 5 | +use Coding\ApiError; |
| 6 | +use Coding\Core; |
| 7 | +use GuzzleHttp\Psr7\Response; |
| 8 | + |
| 9 | +class CoreTest extends TestCase |
| 10 | +{ |
| 11 | + public function testRequestSuccess() |
| 12 | + { |
| 13 | + $responseBody = file_get_contents($this->dataPath('CreateIterationResponse.json')); |
| 14 | + $action = $this->faker->word; |
| 15 | + $number = ($this->faker->randomDigitNotZero()); |
| 16 | + $data = array_combine($this->faker->words($number), $this->faker->words($number)); |
| 17 | + |
| 18 | + $this->clientMock->expects($this->once()) |
| 19 | + ->method('request') |
| 20 | + ->with( |
| 21 | + 'POST', |
| 22 | + 'https://e.coding.net/open-api', |
| 23 | + [ |
| 24 | + 'headers' => [ |
| 25 | + 'Accept' => 'application/json', |
| 26 | + 'Authorization' => "token $this->token", |
| 27 | + 'Content-Type' => 'application/json' |
| 28 | + ], |
| 29 | + 'json' => array_merge([ |
| 30 | + 'Action' => $action, |
| 31 | + ], $data) |
| 32 | + ] |
| 33 | + ) |
| 34 | + ->willReturn(new Response(200, [], $responseBody)); |
| 35 | + $core = new Core($this->token, $this->clientMock); |
| 36 | + $result = $core->request($action, $data); |
| 37 | + $this->assertEquals(json_decode($responseBody, true)['Response'], $result); |
| 38 | + } |
| 39 | + |
| 40 | + public function testRequestFailed() |
| 41 | + { |
| 42 | + $responseBody = file_get_contents($this->dataPath('CreateIterationResponseError.json')); |
| 43 | + $action = $this->faker->word; |
| 44 | + $number = ($this->faker->randomDigitNotZero()); |
| 45 | + $data = array_combine($this->faker->words($number), $this->faker->words($number)); |
| 46 | + |
| 47 | + $this->clientMock->expects($this->once()) |
| 48 | + ->method('request') |
| 49 | + ->with( |
| 50 | + 'POST', |
| 51 | + 'https://e.coding.net/open-api', |
| 52 | + [ |
| 53 | + 'headers' => [ |
| 54 | + 'Accept' => 'application/json', |
| 55 | + 'Authorization' => "token $this->token", |
| 56 | + 'Content-Type' => 'application/json' |
| 57 | + ], |
| 58 | + 'json' => array_merge([ |
| 59 | + 'Action' => $action, |
| 60 | + ], $data) |
| 61 | + ] |
| 62 | + ) |
| 63 | + ->willReturn(new Response(200, [], $responseBody)); |
| 64 | + $core = new Core($this->token, $this->clientMock); |
| 65 | + $this->expectException(ApiError::class); |
| 66 | + $this->expectExceptionMessage(json_decode($responseBody, true)['Response']['Error']['Message']); |
| 67 | + $core->request($action, $data); |
| 68 | + } |
| 69 | +} |
0 commit comments