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

Commit d2c6dc9

Browse files
authored
Merge pull request #38 from shoeyn/master
Use Magento facade accessor everywhere
2 parents e6314ee + 7d8c090 commit d2c6dc9

16 files changed

+172
-198
lines changed

README.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ MAGENTO_API_VERSION="V1"
6060

6161
Example:
6262
```php
63-
use Grayloon\Magento\Magento;
63+
use Magento;
6464

65-
$magento = new Magento();
66-
$response = $magento->api('products')->all();
65+
$response = Magento::api('products')->all();
6766

6867
$response->body() : string;
6968
$response->json() : array|mixed;
@@ -82,7 +81,7 @@ $response->ok() : bool;
8281

8382
Generate a admin token:
8483
```php
85-
$magento->api('integration')->adminToken($username, $password);
84+
Magento::api('integration')->adminToken($username, $password);
8685
```
8786

8887
#### Carts
@@ -91,7 +90,7 @@ $magento->api('integration')->adminToken($username, $password);
9190

9291
Returns information for the cart for the authenticated customer. Must use a single store code.
9392
```php
94-
$magento->api('carts')->mine();
93+
Magento::api('carts')->mine();
9594
```
9695

9796
#### Cart Items (quoteCartItemRepositoryV1)
@@ -100,14 +99,14 @@ $magento->api('carts')->mine();
10099

101100
Lists items that are assigned to a specified customer cart. Must have a store code.
102101
```php
103-
$magento->api('cartItems')->mine();
102+
Magento::api('cartItems')->mine();
104103
```
105104

106105
`/V1/carts/mine/items/`
107106

108107
Add/update the specified cart item with a customer token. Must have a store code.
109108
```php
110-
$magento->api('cartItems')->addItem($cartId, $sku, $quantity);
109+
Magento::api('cartItems')->addItem($cartId, $sku, $quantity);
111110
```
112111

113112
#### Cart Totals (quoteCartTotalRepositoryV1)
@@ -116,7 +115,7 @@ $magento->api('cartItems')->addItem($cartId, $sku, $quantity);
116115

117116
Returns information for the cart totals for the authenticated customer. Must use a single store code.
118117
```php
119-
$magento->api('cartTotals')->mine();
118+
Magento::api('cartTotals')->mine();
120119
```
121120

122121
<a id="categories"></a>
@@ -126,7 +125,7 @@ $magento->api('cartTotals')->mine();
126125

127126
Get a list of all categories:
128127
```php
129-
$magento->api('categories')->all($pageSize = 50, $currentPage = 1, $filters = []);
128+
Magento::api('categories')->all($pageSize = 50, $currentPage = 1, $filters = []);
130129
```
131130

132131
<a id="customer-token"></a>
@@ -136,7 +135,7 @@ $magento->api('categories')->all($pageSize = 50, $currentPage = 1, $filters = []
136135

137136
Generate a customer token:
138137
```php
139-
$magento->api('integration')->customerToken($username, $password);
138+
Magento::api('integration')->customerToken($username, $password);
140139
```
141140

142141
<a id="customers"></a>
@@ -146,7 +145,7 @@ $magento->api('integration')->customerToken($username, $password);
146145

147146
Get a list of customers:
148147
```php
149-
$magento->api('customers')->all($pageSize = 50, $currentPage = 1, $filters = []);
148+
Magento::api('customers')->all($pageSize = 50, $currentPage = 1, $filters = []);
150149
```
151150

152151
<a id="guest-cart"></a>
@@ -156,35 +155,35 @@ $magento->api('customers')->all($pageSize = 50, $currentPage = 1, $filters = [])
156155

157156
Enable customer or guest user to create an empty cart and quote for an anonymous customer.
158157
```php
159-
$magento->api('guestCarts')->create();
158+
Magento::api('guestCarts')->create();
160159
```
161160

162161
`/V1/guest-carts/{cartId}`
163162

164163
Return information for a specified cart.
165164
```php
166-
$magento->api('guestCarts')->cart($cartId);
165+
Magento::api('guestCarts')->cart($cartId);
167166
```
168167

169168
`/V1/guest-carts/{cartId}/items`
170169

171170
List items that are assigned to a specified cart.
172171
```php
173-
$magento->api('guestCarts')->items($cartId);
172+
Magento::api('guestCarts')->items($cartId);
174173
```
175174

176175
`/V1/guest-carts/{cartId}/items`
177176

178177
Add/update the specified cart item.
179178
```php
180-
$magento->api('guestCarts')->addItem($cartId, $sku, $quantity);
179+
Magento::api('guestCarts')->addItem($cartId, $sku, $quantity);
181180
```
182181

183182
`/V1/guest-carts/{cartId}/estimate-shipping-methods`
184183

185184
Estimate shipping by address and return list of available shipping methods.
186185
```php
187-
$magento->api('guestCarts')->estimateShippingMethods($cartId);
186+
Magento::api('guestCarts')->estimateShippingMethods($cartId);
188187
```
189188

190189
<a id="product-attributes"></a>
@@ -194,7 +193,7 @@ $magento->api('guestCarts')->estimateShippingMethods($cartId);
194193

195194
Retrieve specific product attribute information:
196195
```php
197-
$magento->api('productAttributes')->show($attributeCode);
196+
Magento::api('productAttributes')->show($attributeCode);
198197
```
199198

200199
<a id="product-link-types"></a>
@@ -204,7 +203,7 @@ $magento->api('productAttributes')->show($attributeCode);
204203

205204
Retrieve information about available product link types:
206205
```php
207-
$magento->api('productLinkType')->types();
206+
Magento::api('productLinkType')->types();
208207
```
209208

210209
<a id="products"></a>
@@ -214,14 +213,14 @@ $magento->api('productLinkType')->types();
214213

215214
Get a list of products:
216215
```php
217-
$magento->api('products')->all($pageSize = 50, $currentPage = 1, $filters = []);
216+
Magento::api('products')->all($pageSize = 50, $currentPage = 1, $filters = []);
218217
```
219218

220219
`/V1/products/{sku}`
221220

222221
Get info about a product by the product SKU:
223222
```php
224-
$magento->api('products')->show($sku);
223+
Magento::api('products')->show($sku);
225224
```
226225

227226
### Custom modules
@@ -237,18 +236,18 @@ For example:
237236
```
238237
To use these you can directly use get/post methods:
239238
```php
240-
$magento->api('my-custom-endpoint')->post('save', [...]);
239+
Magento::api('my-custom-endpoint')->post('save', [...]);
241240
```
242241
```php
243-
$magento->api('my-custom-endpoint')->get('get/1');
242+
Magento::api('my-custom-endpoint')->get('get/1');
244243
```
245244

246245
<a id="schema"></a>
247246
### Schema
248247

249248
Get a schema blueprint of the Magento 2 REST API:
250249
```php
251-
$magento->api('schema')->show();
250+
Magento::api('schema')->show();
252251
```
253252

254253
### Source Items (inventoryApiSourceItemRepositoryV1)
@@ -257,7 +256,7 @@ $magento->api('schema')->show();
257256

258257
Get a list of paginated sort items (typically used for quantity retrieval):
259258
```php
260-
$magento->api('sourceItems')->all($pageSize = 50, $currentPage = 1, $filters = []);
259+
Magento::api('sourceItems')->all($pageSize = 50, $currentPage = 1, $filters = []);
261260
```
262261

263262
## Testing

src/Magento.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,76 @@ public function api($name)
8484

8585
return new $className($this);
8686
}
87+
88+
/**
89+
* @param string $baseUrl
90+
*
91+
* @return Magento
92+
*/
93+
public function setBaseUrl(string $baseUrl): Magento
94+
{
95+
$this->baseUrl = $baseUrl;
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* @param string|null $token
102+
*
103+
* @return Magento
104+
*/
105+
public function setToken(?string $token): Magento
106+
{
107+
$this->token = $token;
108+
109+
return $this;
110+
}
111+
112+
/**
113+
* @param bool $versionIncluded
114+
*
115+
* @return Magento
116+
*/
117+
public function setVersionIncluded(bool $versionIncluded): Magento
118+
{
119+
$this->versionIncluded = $versionIncluded;
120+
121+
return $this;
122+
}
123+
124+
/**
125+
* @param string $version
126+
*
127+
* @return Magento
128+
*/
129+
public function setVersion(string $version): Magento
130+
{
131+
$this->version = $version;
132+
133+
return $this;
134+
}
135+
136+
/**
137+
* @param string $basePath
138+
*
139+
* @return Magento
140+
*/
141+
public function setBasePath(string $basePath): Magento
142+
{
143+
$this->basePath = $basePath;
144+
145+
return $this;
146+
}
147+
148+
/**
149+
* @param string $storeCode
150+
*
151+
* @return Magento
152+
*/
153+
public function setStoreCode(string $storeCode): Magento
154+
{
155+
$this->storeCode = $storeCode;
156+
157+
return $this;
158+
}
87159
}

tests/Api/AbstractApiTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
namespace Grayloon\Magento\Tests;
44

55
use Grayloon\Magento\Api\AbstractApi;
6-
use Grayloon\Magento\Magento;
6+
use Grayloon\Magento\MagentoFacade;
77
use Illuminate\Support\Facades\Http;
88

99
class AbstractApiTest extends TestCase
1010
{
1111
public function test_can_properly_construct_api_request()
1212
{
13-
$api = new FakeApiClass((new Magento()));
13+
$api = new FakeApiClass(MagentoFacade::getFacadeRoot());
1414

1515
$this->assertEquals('/rest/all/V1', $api->apiRequest);
1616
}
1717

1818
public function test_adds_provided_base_url_to_api_request()
1919
{
20-
$api = new FakeApiClass((new Magento('example.com')));
20+
$api = new FakeApiClass(MagentoFacade::setBaseUrl('example.com'));
2121

2222
$this->assertEquals('example.com/rest/all/V1', $api->apiRequest);
2323
}
@@ -31,7 +31,7 @@ public function test_expect_throw_exception_on_get_api_error()
3131
], 500),
3232
]);
3333

34-
(new FakeApiClass((new Magento('example.com'))))->fakeGetEndpoint();
34+
(new FakeApiClass(MagentoFacade::setBaseUrl('example.com')))->fakeGetEndpoint();
3535
}
3636

3737
public function test_expect_throw_exception_on_post_api_error()
@@ -43,7 +43,7 @@ public function test_expect_throw_exception_on_post_api_error()
4343
], 500),
4444
]);
4545

46-
(new FakeApiClass((new Magento('example.com'))))->fakePostEndpoint();
46+
(new FakeApiClass(MagentoFacade::setBaseUrl('example.com')))->fakePostEndpoint();
4747
}
4848

4949
public function test_expect_message_body_from_array_on_exception_throw()
@@ -56,7 +56,7 @@ public function test_expect_message_body_from_array_on_exception_throw()
5656
], 500),
5757
]);
5858

59-
(new FakeApiClass((new Magento('example.com'))))->fakePostEndpoint();
59+
(new FakeApiClass(MagentoFacade::setBaseUrl('example.com')))->fakePostEndpoint();
6060
}
6161

6262
public function test_400_error_does_not_throw_exception()
@@ -67,7 +67,7 @@ public function test_400_error_does_not_throw_exception()
6767
], 401),
6868
]);
6969

70-
$this->assertNull((new FakeApiClass((new Magento('foo.com'))))->fakeGetEndpoint());
70+
$this->assertNull((new FakeApiClass(MagentoFacade::setBaseUrl('foo.com')))->fakeGetEndpoint());
7171
}
7272

7373
public function test_200_error_does_not_throw_exception()
@@ -78,7 +78,7 @@ public function test_200_error_does_not_throw_exception()
7878
], 200),
7979
]);
8080

81-
$this->assertNull((new FakeApiClass((new Magento('foo.com'))))->fakeGetEndpoint());
81+
$this->assertNull((new FakeApiClass(MagentoFacade::setBaseUrl('foo.com')))->fakeGetEndpoint());
8282
}
8383
}
8484

tests/Api/CartItemsTest.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
namespace Grayloon\Magento\Tests;
44

55
use Grayloon\Magento\Api\CartItems;
6-
use Grayloon\Magento\Magento;
6+
use Grayloon\Magento\MagentoFacade;
77
use Illuminate\Support\Facades\Http;
88

99
class CartItemsTest extends TestCase
1010
{
1111
public function test_can_call_cart_items()
1212
{
13-
$this->assertInstanceOf(CartItems::class, (new Magento())->api('CartItems'));
13+
$this->assertInstanceOf(CartItems::class, MagentoFacade::api('CartItems'));
1414
}
1515

1616
public function test_can_call_cart_item_mine()
1717
{
1818
Http::fake();
1919

20-
$magento = new Magento();
21-
$magento->storeCode = 'default';
22-
23-
$api = $magento->api('cartItems')->mine();
20+
$api = MagentoFacade::setStoreCode('default')->api('cartItems')->mine();
2421

2522
$this->assertTrue($api->ok());
2623
}
@@ -29,18 +26,14 @@ public function test_must_pass_a_single_store_code_to_cart_items_mine()
2926
{
3027
$this->expectException('exception');
3128

32-
$magento = new Magento();
33-
$magento->api('cartItems')->mine();
29+
MagentoFacade::api('cartItems')->mine();
3430
}
3531

3632
public function test_can_call_cart_item_add_item()
3733
{
3834
Http::fake();
3935

40-
$magento = new Magento();
41-
$magento->storeCode = 'default';
42-
43-
$api = $magento->api('cartItems')->addItem('foo', 'bar', 1);
36+
$api = MagentoFacade::setStoreCode('default')->api('cartItems')->addItem('foo', 'bar', 1);
4437

4538
$this->assertTrue($api->ok());
4639
}
@@ -49,7 +42,6 @@ public function test_must_pass_a_single_store_code_to_cart_items_add_item()
4942
{
5043
$this->expectException('exception');
5144

52-
$magento = new Magento();
53-
$magento->api('cartItems')->addItem('foo', 'bar', 1);
45+
MagentoFacade::api('cartItems')->addItem('foo', 'bar', 1);
5446
}
5547
}

0 commit comments

Comments
 (0)