|
| 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 | +} |
0 commit comments