Skip to content

Commit a3be831

Browse files
Generic api added
1 parent 3e3f85f commit a3be831

File tree

4 files changed

+239
-1
lines changed

4 files changed

+239
-1
lines changed

phpspec.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ suites:
22
main:
33
namespace: Sylius\Api
44
psr4_prefix: Sylius\Api
5-
src_path: src
5+
src_path: src

spec/GenericApiSpec.php

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Lakion package.
5+
*
6+
* (c) Lakion
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace spec\Sylius\Api;
13+
14+
use GuzzleHttp\Message\ResponseInterface;
15+
use PhpSpec\ObjectBehavior;
16+
use Sylius\Api\ClientInterface;
17+
18+
/**
19+
* @author Michał Marcinkowski <[email protected]>
20+
*/
21+
class GenericApiSpec extends ObjectBehavior
22+
{
23+
function let(ClientInterface $client)
24+
{
25+
$this->beConstructedWith($client, 'uri');
26+
}
27+
28+
function it_validates_that_uri_is_given($client)
29+
{
30+
$this->shouldThrow('InvalidArgumentException')->during('__construct', [$client, null]);
31+
}
32+
33+
function it_validates_that_uri_is_a_string($client)
34+
{
35+
$this->shouldThrow('InvalidArgumentException')->during('__construct', [$client, 1]);
36+
}
37+
38+
function it_is_initializable()
39+
{
40+
$this->shouldHaveType('Sylius\Api\GenericApi');
41+
}
42+
43+
function it_implements_api_interface()
44+
{
45+
$this->shouldHaveType('Sylius\Api\ApiInterface');
46+
}
47+
48+
function it_gets_resource_by_id($client)
49+
{
50+
$client->get('uri/1')->shouldBeCalled();
51+
$this->get(1);
52+
}
53+
54+
function it_creates_resource_with_body($client)
55+
{
56+
$client->post('uri/', ['field1' => 'field1Value', 'field2' => 'field2Value'], [])->shouldBeCalled();
57+
$this->create(['field1' => 'field1Value', 'field2' => 'field2Value']);
58+
}
59+
60+
function it_creates_resource_with_body_and_files($client)
61+
{
62+
$client->post('uri/', ['field1' => 'field1Value', 'field2' => 'field2Value'], ['images[0][file]' => 'path/to/file1.jpg'])->shouldBeCalled();
63+
$this->create(['field1' => 'field1Value', 'field2' => 'field2Value'], ['images[0][file]' => 'path/to/file1.jpg']);
64+
}
65+
66+
function it_updates_resource_with_body($client, ResponseInterface $response)
67+
{
68+
$response->getStatusCode()->willReturn(204);
69+
$client->patch('uri/', ['field1' => 'field1Value', 'field2' => 'field2Value'])->willReturn($response);
70+
$client->patch('uri/', ['field1' => 'field1Value', 'field2' => 'field2Value'])->shouldBeCalled();
71+
$this->update(['field1' => 'field1Value', 'field2' => 'field2Value'])->shouldReturn(true);
72+
}
73+
74+
function it_returns_false_if_resource_update_was_not_successful($client, ResponseInterface $response)
75+
{
76+
$response->getStatusCode()->willReturn(400);
77+
$client->patch('uri/', ['field1' => 'field1Value', 'field2' => 'field2Value'])->willReturn($response);
78+
$client->patch('uri/', ['field1' => 'field1Value', 'field2' => 'field2Value'])->shouldBeCalled();
79+
$this->update(['field1' => 'field1Value', 'field2' => 'field2Value'])->shouldReturn(false);
80+
}
81+
82+
function it_deletes_resource_by_id($client, ResponseInterface $response)
83+
{
84+
$response->getStatusCode()->willReturn(204);
85+
$client->delete('uri/1')->willReturn($response);
86+
$client->delete('uri/1')->shouldBeCalled();
87+
$this->delete(1)->shouldReturn(true);
88+
}
89+
90+
function it_returns_false_if_resource_deletion_was_not_successful($client, ResponseInterface $response)
91+
{
92+
$response->getStatusCode()->willReturn(400);
93+
$client->delete('uri/1')->willReturn($response);
94+
$client->delete('uri/1')->shouldBeCalled();
95+
$this->delete(1)->shouldReturn(false);
96+
}
97+
}

src/ApiInterface.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Lakion package.
5+
*
6+
* (c) Lakion
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Sylius\Api;
13+
14+
/**
15+
* @author Michał Marcinkowski <[email protected]>
16+
*/
17+
interface ApiInterface
18+
{
19+
/**
20+
* @param string|int $id Resource ID
21+
* @return array
22+
*/
23+
public function get($id);
24+
25+
/**
26+
* @param array $body Array of fields to be sent to api
27+
* @param array $files Array of files to upload. Key = field key, Value = file path.
28+
* @return array
29+
*/
30+
public function create(array $body, array $files = []);
31+
32+
/**
33+
* @param array $body Array of fields to be sent to api
34+
* @param array $files Array of files to upload. Key = field key, Value = file path.
35+
* @return bool
36+
*/
37+
public function update(array $body, array $files = []);
38+
39+
/**
40+
* @param string|int $id Resource ID
41+
* @return bool
42+
*/
43+
public function delete($id);
44+
}

src/GenericApi.php

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Lakion package.
5+
*
6+
* (c) Lakion
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Sylius\Api;
13+
14+
/**
15+
* @author Michał Marcinkowski <[email protected]>
16+
*/
17+
class GenericApi implements ApiInterface
18+
{
19+
/**
20+
* @var ClientInterface $client
21+
*/
22+
private $client;
23+
/**
24+
* @var string $uri
25+
*/
26+
private $uri;
27+
28+
/**
29+
* @param ClientInterface $client
30+
* @param string $uri
31+
* @throws \InvalidArgumentException
32+
*/
33+
public function __construct(ClientInterface $client, $uri)
34+
{
35+
$this->setUri($uri);
36+
$this->client = $client;
37+
}
38+
39+
/**
40+
* @param string $uri
41+
* @throws \InvalidArgumentException
42+
*/
43+
private function setUri($uri)
44+
{
45+
if (empty($uri) || !is_string($uri)) {
46+
throw new \InvalidArgumentException('You must specify uri for Api');
47+
}
48+
if (($uri[strlen($uri) - 1]) != '/') {
49+
$uri = sprintf('%s/', $uri);
50+
}
51+
$this->uri = $uri;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getUri()
58+
{
59+
return $this->uri;
60+
}
61+
62+
/**
63+
* {@inheritdoc }
64+
*/
65+
public function get($id)
66+
{
67+
return $this->client->get(sprintf('%s%s', $this->getUri(), $id));
68+
}
69+
70+
/**
71+
* {@inheritdoc }
72+
*/
73+
public function create(array $body, array $files = [])
74+
{
75+
return $this->client->post($this->getUri(), $body, $files);
76+
}
77+
78+
/**
79+
* {@inheritdoc }
80+
*/
81+
public function update(array $body, array $files = [])
82+
{
83+
$response = $this->client->patch($this->getUri(), $body);
84+
85+
return (204 === $response->getStatusCode());
86+
}
87+
88+
/**
89+
* {@inheritdoc }
90+
*/
91+
public function delete($id)
92+
{
93+
$response = $this->client->delete(sprintf('%s%s', $this->getUri(), $id));
94+
95+
return (204 === $response->getStatusCode());
96+
}
97+
}

0 commit comments

Comments
 (0)