From 7f10e840a291e998bc077f6ba6fc720e2c8c6eac Mon Sep 17 00:00:00 2001 From: Rapolas Gruzdys Date: Tue, 14 May 2024 08:23:46 +0000 Subject: [PATCH] Add ability to send headers through repositories --- src/Actions/Create.php | 6 ++++-- src/Actions/Delete.php | 5 +++-- src/Actions/FetchMany.php | 5 +++-- src/Actions/FetchOne.php | 5 +++-- src/Actions/Save.php | 7 ++++--- src/Actions/TakeOne.php | 5 +++-- src/Actions/Update.php | 6 ++++-- tests/RepositoryTest.php | 24 ++++++++++++------------ 8 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/Actions/Create.php b/src/Actions/Create.php index aa2599c..a56d3c9 100644 --- a/src/Actions/Create.php +++ b/src/Actions/Create.php @@ -11,14 +11,16 @@ trait Create /** * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item * @param array $parameters + * @param array $headers * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function create(ItemInterface $item, array $parameters = []) + public function create(ItemInterface $item, array $parameters = [], array $headers = []) { return $this->getClient()->post( $this->getEndpoint().'?'.http_build_query($parameters), - $this->documentFactory->make($item) + $this->documentFactory->make($item), + $headers, ); } } diff --git a/src/Actions/Delete.php b/src/Actions/Delete.php index b55908d..3aafa63 100644 --- a/src/Actions/Delete.php +++ b/src/Actions/Delete.php @@ -9,11 +9,12 @@ trait Delete /** * @param string $id * @param array $parameters + * @param array $headers * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function delete(string $id, array $parameters = []) + public function delete(string $id, array $parameters = [], array $headers = []) { - return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters)); + return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters), $headers); } } diff --git a/src/Actions/FetchMany.php b/src/Actions/FetchMany.php index 2af56e8..383b01b 100644 --- a/src/Actions/FetchMany.php +++ b/src/Actions/FetchMany.php @@ -8,11 +8,12 @@ trait FetchMany { /** * @param array $parameters + * @param array $headers * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function all(array $parameters = []) + public function all(array $parameters = [], array $headers = []) { - return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters)); + return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters), $headers); } } diff --git a/src/Actions/FetchOne.php b/src/Actions/FetchOne.php index 05b673f..f710583 100644 --- a/src/Actions/FetchOne.php +++ b/src/Actions/FetchOne.php @@ -9,11 +9,12 @@ trait FetchOne /** * @param string $id * @param array $parameters + * @param array $headers * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function find(string $id, array $parameters = []) + public function find(string $id, array $parameters = [], array $headers = []) { - return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters)); + return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters), $headers); } } diff --git a/src/Actions/Save.php b/src/Actions/Save.php index 00d14c8..c7cc50e 100644 --- a/src/Actions/Save.php +++ b/src/Actions/Save.php @@ -14,15 +14,16 @@ trait Save /** * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item * @param array $parameters + * @param array $headers * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function save(ItemInterface $item, array $parameters = []) + public function save(ItemInterface $item, array $parameters = [], array $headers = []) { if ($item->isNew()) { - return $this->saveNew($item, $parameters); + return $this->saveNew($item, $parameters, $headers); } - return $this->saveExisting($item, $parameters); + return $this->saveExisting($item, $parameters, $headers); } } diff --git a/src/Actions/TakeOne.php b/src/Actions/TakeOne.php index 171eac8..2ec36e7 100644 --- a/src/Actions/TakeOne.php +++ b/src/Actions/TakeOne.php @@ -8,11 +8,12 @@ trait TakeOne { /** * @param array $parameters + * @param array $headers * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function take(array $parameters = []) + public function take(array $parameters = [], array $headers = []) { - return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters)); + return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters), $headers); } } diff --git a/src/Actions/Update.php b/src/Actions/Update.php index 0aa775b..83b7ac4 100644 --- a/src/Actions/Update.php +++ b/src/Actions/Update.php @@ -11,14 +11,16 @@ trait Update /** * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item * @param array $parameters + * @param array $headers * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function update(ItemInterface $item, array $parameters = []) + public function update(ItemInterface $item, array $parameters = [], array $headers = []) { return $this->getClient()->patch( $this->getEndpoint().'/'.urlencode($item->getId()).'?'.http_build_query($parameters), - $this->documentFactory->make($item) + $this->documentFactory->make($item), + $headers, ); } } diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index a62cc2c..b50b9fb 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -50,12 +50,12 @@ public function itCanGetAll() $client->expects($this->once()) ->method('get') - ->with('mocks?foo=bar') + ->with('mocks?foo=bar', ['Test-Header' => 'Foo-Bar']) ->willReturn($document); $repository = new MockRepository($client, new DocumentFactory()); - $this->assertSame($document, $repository->all(['foo' => 'bar'])); + $this->assertSame($document, $repository->all(['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** @@ -70,12 +70,12 @@ public function itCanTakeOne() $client->expects($this->once()) ->method('get') - ->with('mocks?foo=bar') + ->with('mocks?foo=bar', ['Test-Header' => 'Foo-Bar']) ->willReturn($document); $repository = new MockRepository($client, new DocumentFactory()); - $this->assertSame($document, $repository->take(['foo' => 'bar'])); + $this->assertSame($document, $repository->take(['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** @@ -90,12 +90,12 @@ public function itCanFindOne() $client->expects($this->once()) ->method('get') - ->with('mocks/1?foo=bar') + ->with('mocks/1?foo=bar', ['Test-Header' => 'Foo-Bar']) ->willReturn($document); $repository = new MockRepository($client, new DocumentFactory()); - $this->assertSame($document, $repository->find('1', ['foo' => 'bar'])); + $this->assertSame($document, $repository->find('1', ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** @@ -111,12 +111,12 @@ public function itCanSaveNew() $client->expects($this->once()) ->method('post') - ->with('mocks?foo=bar') + ->with('mocks?foo=bar', $document, ['Test-Header' => 'Foo-Bar']) ->willReturn($document); $repository = new MockRepository($client, new DocumentFactory()); - $this->assertSame($document, $repository->save(new Item(), ['foo' => 'bar'])); + $this->assertSame($document, $repository->save(new Item(), ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** @@ -132,12 +132,12 @@ public function itCanSaveExisting() $client->expects($this->once()) ->method('patch') - ->with('mocks/1?foo=bar') + ->with('mocks/1?foo=bar', $document, ['Test-Header' => 'Foo-Bar']) ->willReturn($document); $repository = new MockRepository($client, new DocumentFactory()); - $this->assertSame($document, $repository->save((new Item())->setId('1'), ['foo' => 'bar'])); + $this->assertSame($document, $repository->save((new Item())->setId('1'), ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** @@ -152,11 +152,11 @@ public function itCanDelete() $client->expects($this->once()) ->method('delete') - ->with('mocks/1?foo=bar') + ->with('mocks/1?foo=bar', ['Test-Header' => 'Foo-Bar']) ->willReturn($document); $repository = new MockRepository($client, new DocumentFactory()); - $this->assertSame($document, $repository->delete('1', ['foo' => 'bar'])); + $this->assertSame($document, $repository->delete('1', ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } }