Skip to content

Commit

Permalink
Add availabilities to the REST API client (#20)
Browse files Browse the repository at this point in the history
* add support for the availability endpoints
  • Loading branch information
m-timmermann committed Apr 5, 2024
1 parent c63dcbe commit bc98a29
Show file tree
Hide file tree
Showing 9 changed files with 688 additions and 0 deletions.
405 changes: 405 additions & 0 deletions src/availabilities/Service.php

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions tests/availabilities/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php namespace Meplato\Store2\Availabilities\Tests;
// Copyright (c) 2015 Meplato GmbH, Switzerland.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

/**
* Base test for the Availabilities service.
*/
abstract class BaseTest extends \Meplato\Store2\Tests\BaseTest
{
public function __construct()
{
parent::__construct();
}

public function getService()
{
$client = $this->getHttpClient();
$this->service = new \Meplato\Store2\Availabilities\Service($client);
$this->service->setBaseURL("http://store2.go/api/v2");
return $this->service;
}

protected function setUp(): void
{
}

protected function tearDown(): void
{
}
}
?>
40 changes: 40 additions & 0 deletions tests/availabilities/DeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php namespace Meplato\Store2\Availabilities\Tests;
// Copyright (c) 2015 Meplato GmbH, Switzerland.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

use Meplato\Store2\HttpClient;
use Meplato\Store2\Availabilities\Service;

/**
* Tests deleting availabilities.
*/
class DeleteTest extends BaseTest
{
/**
* Tests a successful call to delete availabilities.
*
* @group availabilities
* @group availabilities.delete
*/
public function testDelete()
{

$service = $this->getService();
$this->mockResponseFromFile('availabilities.delete.success');
$response = $service->delete()->spn('1234')->execute();

$this->assertIsArray($response);
$this->assertArrayHasKey('kind', $response);
$this->assertEquals('store#availabilities/deleteResponse', $response['kind']);
}
}
?>
64 changes: 64 additions & 0 deletions tests/availabilities/GetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php namespace Meplato\Store2\Availabilities\Tests;
// Copyright (c) 2015 Meplato GmbH, Switzerland.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

use Meplato\Store2\HttpClient;
use Meplato\Store2\Availabilities\Service;

/**
* Tests retrieving of availabilities.
*/
class GetTest extends BaseTest
{
/**
* Tests a successful call to retrieve availabilities.
*
* @group availabilities
* @group availabilities.get
*/
public function testGet()
{
$service = $this->getService();
$this->mockResponseFromFile('availabilities.get.success');
$response = $service->get()->spn("1234")->execute();
$this->assertIsArray($response);
$this->assertArrayHasKey('kind', $response);
$this->assertEquals('store#availabilities/getResponse', $response['kind']);

$this->assertArrayHasKey('items', $response);
$this->assertIsArray($response['items']);
$this->assertCount(3, $response['items']);
$this->assertEquals('1234', $response['items'][0]['spn']);
}

/**
* Test what happens when availabilities are not found.
*
* @group availabilities
* @group availabilities.get
*/
public function testGetNotFound()
{
$service = $this->getService();
$this->mockResponseFromFile('availabilities.get.not_found');

$response = $service->get()->spn('no-such-product')->execute();

$this->assertIsArray($response);
$this->assertArrayHasKey('kind', $response);
$this->assertEquals('store#availabilities/getResponse', $response['kind']);

$this->assertArrayHasKey('items', $response);
$this->assertNull($response['items']);
}
}
?>
46 changes: 46 additions & 0 deletions tests/availabilities/UpsertTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php namespace Meplato\Store2\Availabilities\Tests;
// Copyright (c) 2015 Meplato GmbH, Switzerland.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

use Meplato\Store2\HttpClient;
use Meplato\Store2\Availabilities\Service;

class UpsertTest extends BaseTest
{
/**
* Tests a successful call to upsert of availabilities.
*
* @group availabilities
* @group availabilities.upsert
*/
public function testUpsert()
{
$availability = [
'Message'=> "not in stock",
'Quantity'=> 0,
'Region'=> "AQ",
'Updated'=> "Q1/2024",
'ZipCode'=> "1234",
];

$service = $this->getService();

// Upsert availabilities, i.e. either create or update.
$this->mockResponseFromFile('availabilities.upsert.success');
$response = $service->upsert()->spn(1234)->availability($availability)->execute();
$this->assertIsArray($response);
$this->assertArrayHasKey('kind', $response);
$this->assertEquals('store#availabilities/upsertResponse', $response['kind']);
$this->assertArrayHasKey('link', $response);
}
}
?>
15 changes: 15 additions & 0 deletions tests/mock/responses/availabilities.delete.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
HTTP/1.1 202 OK
Cache-Control: private, no-cache
Content-Type: application/json; charset=utf-8
Last-Modified: Tue, 31 Mar 2015 14:18:15 GMT
P3p: CP="This is not a P3P policy!"
Vary: Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Ua-Compatible: IE=edge
X-Xss-Protection: 1; mode=block
Date: Tue, 25 Mar 2024 14:18:15 GMT

{
"kind": "store#availabilities/deleteResponse"
}
17 changes: 17 additions & 0 deletions tests/mock/responses/availabilities.get.not_found
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 200
Cache-Control: private, no-cache
Content-Type: application/json; charset=utf-8
Last-Modified: Tue, 31 Mar 2015 14:15:25 GMT
P3p: CP="This is not a P3P policy!"
Vary: Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Ua-Compatible: IE=edge
X-Xss-Protection: 1; mode=block
Date: Tue, 31 Mar 2015 14:15:25 GMT

{
"kind": "store#availabilities/getResponse",
"items": null,
"Error": null
}
45 changes: 45 additions & 0 deletions tests/mock/responses/availabilities.get.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
HTTP/1.1 200 OK
Cache-Control: private, no-cache
Content-Type: application/json; charset=utf-8
Last-Modified: Tue, 31 Mar 2015 14:18:15 GMT
P3p: CP="This is not a P3P policy!"
Vary: Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Ua-Compatible: IE=edge
X-Xss-Protection: 1; mode=block
Date: Tue, 25 Mar 2024 14:18:15 GMT

{
"kind": "store#availabilities/getResponse",
"Error": null,
"items": [
{
"message": "in stock",
"mpcc": "meplato",
"quantity": 15.3,
"region": "UK",
"spn": "1234",
"updated": "Q4/2022",
"zipCode": "04109"
},
{
"message": "in stock",
"mpcc": "meplato",
"quantity": 35.0,
"region": "DK",
"spn": "1234",
"updated": "Q4/2022",
"zipCode": "05109"
},
{
"message": "in stock",
"mpcc": "meplato",
"quantity": 20.4,
"region": "DE",
"spn": "1234",
"updated": "Q4/2022",
"zipCode": "06109"
}
]
}
16 changes: 16 additions & 0 deletions tests/mock/responses/availabilities.upsert.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
HTTP/1.1 202 OK
Cache-Control: private, no-cache
Content-Type: application/json; charset=utf-8
Last-Modified: Tue, 31 Mar 2015 14:18:15 GMT
P3p: CP="This is not a P3P policy!"
Vary: Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Ua-Compatible: IE=edge
X-Xss-Protection: 1; mode=block
Date: Tue, 25 Mar 2024 14:18:15 GMT

{
"kind": "store#availabilities/upsertResponse",
"link": "string"
}

0 comments on commit bc98a29

Please sign in to comment.