Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ $response->ok() : bool;

> Will throw an exception on >500 errors.

You may also utilize the constructor directly without having to configure environment variables:

```php
use Grayloon\Magento\Magento;

$magento = new Magento(
$baseUrl = 'https://my-magneto-shop.com',
$token = 'client_access_token',
$version = 'V1',
$basePath = 'rest',
$storeCode = 'default'
);

$response = $magento->api('products')->all();
```

## Available Methods:

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

Get a schema blueprint of the Magento 2 REST API:
```php
Magento::api('schema')->show();
Magento::api('schema')->show();
```

### Source Items (inventoryApiSourceItemRepositoryV1)
Expand Down
8 changes: 4 additions & 4 deletions src/Magento.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ class Magento
* @param string $baseUrl
* @param string $token
*/
public function __construct($baseUrl = null, $token = null)
public function __construct($baseUrl = null, $token = null, $version = null, $basePath = null, $storeCode = null)
{
$this->baseUrl = $baseUrl ?: config('magento.base_url');
$this->token = $token ?: config('magento.token');
$this->version = config('magento.version') ?? 'V1';
$this->basePath = config('magento.base_path') ?? 'rest';
$this->storeCode = config('magento.store_code') ?? 'all';
$this->version = $version ?: config('magento.version') ?: 'V1';
$this->basePath = $basePath ?: config('magento.base_path') ?: 'rest';
$this->storeCode = $storeCode ?: config('magento.store_code') ?: 'all';
}

/**
Expand Down
91 changes: 91 additions & 0 deletions tests/MagentoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Grayloon\Magento\Tests;

use Grayloon\Magento\Magento;
use Illuminate\Support\Facades\Config;

class MagentoTest extends TestCase
{
/**
* Test by configuring the settings in the constructor.
*
* @return void
**/
public function test_by_configuring_the_settings_in_the_constructor()
{
$api = new Magento('baseUrl-constructor', 'token-constructor', 'version-constructor', 'basePath-constructor', 'storeCode-constructor');

$this->assertEquals($api->baseUrl, 'baseUrl-constructor');
$this->assertEquals($api->token, 'token-constructor');
$this->assertEquals($api->version, 'version-constructor');
$this->assertEquals($api->basePath, 'basePath-constructor');
$this->assertEquals($api->storeCode, 'storeCode-constructor');
}

/**
* Test by configuring the settings in the laravel configs.
*
* @return void
**/
public function test_by_configuring_the_settings_in_the_laravel_configs()
{
Config::set('magento.base_url', 'baseUrl-config');
Config::set('magento.token', 'token-config');
Config::set('magento.version', 'version-config');
Config::set('magento.base_path', 'basePath-config');
Config::set('magento.store_code', 'storeCode-config');

$api = new Magento();

$this->assertEquals($api->baseUrl, 'baseUrl-config');
$this->assertEquals($api->token, 'token-config');
$this->assertEquals($api->version, 'version-config');
$this->assertEquals($api->basePath, 'basePath-config');
$this->assertEquals($api->storeCode, 'storeCode-config');
}

/**
* Test without configuring the settings.
*
* @return void
**/
public function test_without_configuring_the_settings()
{
$api = new Magento();

$this->assertNull($api->baseUrl);
$this->assertNull($api->token);

$defaultVersion = 'V1';
$this->assertEquals($api->version, $defaultVersion);

$defaultBasePath = 'rest';
$this->assertEquals($api->basePath, $defaultBasePath);

$defaultStoreCode = 'all';
$this->assertEquals($api->storeCode, $defaultStoreCode);
}

/**
* Test by configuring the settings in the constructor have priority.
*
* @return void
**/
public function test_by_configuring_the_settings_in_the_constructor_have_priority()
{
Config::set('magento.base_url', 'baseUrl-config');
Config::set('magento.token', 'token-config');
Config::set('magento.version', 'version-config');
Config::set('magento.base_path', 'basePath-config');
Config::set('magento.store_code', 'storeCode-config');

$api = new Magento('baseUrl-constructor', 'token-constructor', 'version-constructor', 'basePath-constructor', 'storeCode-constructor');

$this->assertEquals($api->baseUrl, 'baseUrl-constructor');
$this->assertEquals($api->token, 'token-constructor');
$this->assertEquals($api->version, 'version-constructor');
$this->assertEquals($api->basePath, 'basePath-constructor');
$this->assertEquals($api->storeCode, 'storeCode-constructor');
}
}