-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreTest.php
More file actions
81 lines (73 loc) · 2.79 KB
/
Copy pathCoreTest.php
File metadata and controls
81 lines (73 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Tests\Unit;
use Coding\Exceptions\ApiError;
use Coding\Core;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Tests\TestCase;
class CoreTest extends TestCase
{
private Client $clientMock;
protected bool $needCoreMock = false;
protected function setUp(): void
{
parent::setUp();
$this->clientMock = $this->getMockBuilder(Client::class)->getMock();
}
public function testRequestSuccess()
{
$responseBody = file_get_contents($this->dataPath('CreateIterationResponse.json'));
$action = $this->faker->word;
$number = ($this->faker->randomDigitNotZero());
$data = array_combine($this->faker->words($number), $this->faker->words($number));
$this->clientMock->expects($this->once())
->method('request')
->with(
'POST',
'https://e.coding.net/open-api',
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token $this->token",
'Content-Type' => 'application/json'
],
'json' => array_merge([
'Action' => $action,
], $data)
]
)
->willReturn(new Response(200, [], $responseBody));
$core = new Core($this->token, $this->clientMock);
$result = $core->request($action, $data);
$this->assertEquals(json_decode($responseBody, true)['Response'], $result);
}
public function testRequestFailed()
{
$responseBody = file_get_contents($this->dataPath('CreateIterationResponseError.json'));
$action = $this->faker->word;
$number = ($this->faker->randomDigitNotZero());
$data = array_combine($this->faker->words($number), $this->faker->words($number));
$this->clientMock->expects($this->once())
->method('request')
->with(
'POST',
'https://e.coding.net/open-api',
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token $this->token",
'Content-Type' => 'application/json'
],
'json' => array_merge([
'Action' => $action,
], $data)
]
)
->willReturn(new Response(200, [], $responseBody));
$core = new Core('', $this->clientMock);
$core->setToken($this->token);
$this->expectException(ApiError::class);
$this->expectExceptionMessage(json_decode($responseBody, true)['Response']['Error']['Message']);
$core->request($action, $data);
}
}