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

Commit 4be35d8

Browse files
author
Jean Carlos Farias
committed
configuring the settings in the constructor
1 parent fe55322 commit 4be35d8

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ $response->ok() : bool;
7777

7878
> Will throw an exception on >500 errors.
7979
80+
Use the wrapper for multiple shops, without needing to publish config or setting environment variables::
81+
82+
```php
83+
use Grayloon\Magento\Magento;
84+
85+
$magento = new Magento('https://myshop.url', 'token', 'V1', 'basePath', 'storeCode');
86+
$response = $magento->api('products')->all();
87+
88+
```
89+
8090
## Available Methods:
8191

8292
<a id="admin-token"></a>

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)