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

Commit 6480544

Browse files
authored
Merge pull request #65 from jeancx/settings-in-the-constructor
configuring the settings in the constructor for multiple shops
2 parents 9884604 + d9898c0 commit 6480544

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ $response->ok() : bool;
7777

7878
> Will throw an exception on >500 errors.
7979
80+
You may also utilize the constructor directly without having to configure environment variables:
81+
82+
```php
83+
use Grayloon\Magento\Magento;
84+
85+
$magento = new Magento(
86+
$baseUrl = 'https://my-magneto-shop.com',
87+
$token = 'client_access_token',
88+
$version = 'V1',
89+
$basePath = 'rest',
90+
$storeCode = 'default'
91+
);
92+
93+
$response = $magento->api('products')->all();
94+
```
95+
8096
## Available Methods:
8197

8298
<a id="admin-token"></a>
@@ -342,7 +358,7 @@ Magento::api('my-custom-endpoint')->get('get/1');
342358

343359
Get a schema blueprint of the Magento 2 REST API:
344360
```php
345-
Magento::api('schema')->show();
361+
Magento::api('schema')->show();
346362
```
347363

348364
### Source Items (inventoryApiSourceItemRepositoryV1)

src/Magento.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ class Magento
5656
* @param string $baseUrl
5757
* @param string $token
5858
*/
59-
public function __construct($baseUrl = null, $token = null)
59+
public function __construct($baseUrl = null, $token = null, $version = null, $basePath = null, $storeCode = null)
6060
{
6161
$this->baseUrl = $baseUrl ?: config('magento.base_url');
6262
$this->token = $token ?: config('magento.token');
63-
$this->version = config('magento.version') ?? 'V1';
64-
$this->basePath = config('magento.base_path') ?? 'rest';
65-
$this->storeCode = config('magento.store_code') ?? 'all';
63+
$this->version = $version ?: config('magento.version') ?: 'V1';
64+
$this->basePath = $basePath ?: config('magento.base_path') ?: 'rest';
65+
$this->storeCode = $storeCode ?: config('magento.store_code') ?: 'all';
6666
}
6767

6868
/**

tests/MagentoTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Grayloon\Magento\Tests;
4+
5+
use Grayloon\Magento\Magento;
6+
use Illuminate\Support\Facades\Config;
7+
8+
class MagentoTest extends TestCase
9+
{
10+
/**
11+
* Test by configuring the settings in the constructor.
12+
*
13+
* @return void
14+
**/
15+
public function test_by_configuring_the_settings_in_the_constructor()
16+
{
17+
$api = new Magento('baseUrl-constructor', 'token-constructor', 'version-constructor', 'basePath-constructor', 'storeCode-constructor');
18+
19+
$this->assertEquals($api->baseUrl, 'baseUrl-constructor');
20+
$this->assertEquals($api->token, 'token-constructor');
21+
$this->assertEquals($api->version, 'version-constructor');
22+
$this->assertEquals($api->basePath, 'basePath-constructor');
23+
$this->assertEquals($api->storeCode, 'storeCode-constructor');
24+
}
25+
26+
/**
27+
* Test by configuring the settings in the laravel configs.
28+
*
29+
* @return void
30+
**/
31+
public function test_by_configuring_the_settings_in_the_laravel_configs()
32+
{
33+
Config::set('magento.base_url', 'baseUrl-config');
34+
Config::set('magento.token', 'token-config');
35+
Config::set('magento.version', 'version-config');
36+
Config::set('magento.base_path', 'basePath-config');
37+
Config::set('magento.store_code', 'storeCode-config');
38+
39+
$api = new Magento();
40+
41+
$this->assertEquals($api->baseUrl, 'baseUrl-config');
42+
$this->assertEquals($api->token, 'token-config');
43+
$this->assertEquals($api->version, 'version-config');
44+
$this->assertEquals($api->basePath, 'basePath-config');
45+
$this->assertEquals($api->storeCode, 'storeCode-config');
46+
}
47+
48+
/**
49+
* Test without configuring the settings.
50+
*
51+
* @return void
52+
**/
53+
public function test_without_configuring_the_settings()
54+
{
55+
$api = new Magento();
56+
57+
$this->assertNull($api->baseUrl);
58+
$this->assertNull($api->token);
59+
60+
$defaultVersion = 'V1';
61+
$this->assertEquals($api->version, $defaultVersion);
62+
63+
$defaultBasePath = 'rest';
64+
$this->assertEquals($api->basePath, $defaultBasePath);
65+
66+
$defaultStoreCode = 'all';
67+
$this->assertEquals($api->storeCode, $defaultStoreCode);
68+
}
69+
70+
/**
71+
* Test by configuring the settings in the constructor have priority.
72+
*
73+
* @return void
74+
**/
75+
public function test_by_configuring_the_settings_in_the_constructor_have_priority()
76+
{
77+
Config::set('magento.base_url', 'baseUrl-config');
78+
Config::set('magento.token', 'token-config');
79+
Config::set('magento.version', 'version-config');
80+
Config::set('magento.base_path', 'basePath-config');
81+
Config::set('magento.store_code', 'storeCode-config');
82+
83+
$api = new Magento('baseUrl-constructor', 'token-constructor', 'version-constructor', 'basePath-constructor', 'storeCode-constructor');
84+
85+
$this->assertEquals($api->baseUrl, 'baseUrl-constructor');
86+
$this->assertEquals($api->token, 'token-constructor');
87+
$this->assertEquals($api->version, 'version-constructor');
88+
$this->assertEquals($api->basePath, 'basePath-constructor');
89+
$this->assertEquals($api->storeCode, 'storeCode-constructor');
90+
}
91+
}

0 commit comments

Comments
 (0)