Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 9b68e9e

Browse files
author
Mateusz Gostański
committed
Refactored ApiClient
Changed make to static method, added possibility of passing just name (or path) in configuration to fill CallApi constructor with those data. Or passing it manually directly to make() method. Using WrongConfigException when wrong number of parameters will be passed to make(). And of course updated test.
1 parent 1b59890 commit 9b68e9e

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

src/ApiClient.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace Grixu\ApiClient;
44

5+
use Grixu\ApiClient\Exceptions\WrongConfigException;
6+
57
class ApiClient
68
{
7-
private CallApiAction $callApiAction;
8-
9-
public function __construct()
9+
public static function make(...$config): CallApi
1010
{
11-
$this->callApiAction = new CallApiAction();
12-
}
11+
if (count($config) === 1 && is_array(config($config[0]))) {
12+
$config = config($config[0]);
13+
}
1314

14-
public function make(string $url)
15-
{
16-
return $this->callApiAction->execute($url);
15+
if (empty($config) || count($config) < 4) {
16+
throw new WrongConfigException();
17+
}
18+
19+
return new CallApi(...$config);
1720
}
1821
}

tests/ApiClientTest.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Grixu\ApiClient\ApiClient;
66
use Grixu\ApiClient\ApiClientServiceProvider;
7+
use Grixu\ApiClient\Exceptions\WrongConfigException;
78
use Illuminate\Support\Facades\Http;
89
use Orchestra\Testbench\TestCase;
910

@@ -16,6 +17,26 @@ protected function getPackageProviders($app)
1617

1718
/** @test */
1819
public function normal_pass()
20+
{
21+
$this->createHttpFake();
22+
$obj = $this->createTestObj();
23+
$result = $obj->call(env('TEST_BASE_URL'));
24+
25+
$this->assertNotEmpty($result);
26+
}
27+
28+
protected function createTestObj()
29+
{
30+
return ApiClient::make(
31+
env('TEST_BASE_URL'),
32+
env('TEST_OAUTH'),
33+
env('TEST_CLIENT_ID'),
34+
env('TEST_CLIENT_KEY'),
35+
'test'
36+
);
37+
}
38+
39+
protected function createHttpFake()
1940
{
2041
Http::fake(
2142
[
@@ -35,10 +56,47 @@ public function normal_pass()
3556

3657
]
3758
);
59+
}
60+
61+
/**
62+
* @test
63+
* @environment-setup apiConfig
64+
*/
65+
public function with_config_name()
66+
{
67+
$this->createHttpFake();
3868

39-
$obj = new ApiClient();
40-
$result = $obj->make(config('api-client.base_url'));
69+
$obj = ApiClient::make('project.api');
70+
$result = $obj->call(env('TEST_BASE_URL'));
4171

4272
$this->assertNotEmpty($result);
4373
}
74+
75+
protected function apiConfig($app)
76+
{
77+
$app->config->set(
78+
'project',
79+
[
80+
'api' => [
81+
'baseUrl',
82+
'oAuthUrl',
83+
'id',
84+
'key',
85+
'cache'
86+
]
87+
]
88+
);
89+
}
90+
91+
/** @test */
92+
public function wrong_options_passed()
93+
{
94+
try {
95+
ApiClient::make('lol', 'lol');
96+
97+
$this->assertTrue(false);
98+
} catch (WrongConfigException $e) {
99+
$this->assertTrue(true);
100+
}
101+
}
44102
}

0 commit comments

Comments
 (0)