Skip to content

Commit

Permalink
Merge pull request #142 from dschoenbauer/feature/139-http-persistenc…
Browse files Browse the repository at this point in the history
…e-visitor

Update the HTTP Persistance to use visitor client
  • Loading branch information
dschoenbauer committed Jan 17, 2018
2 parents e211d36 + 9560fc2 commit 457b16d
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 75 deletions.
10 changes: 5 additions & 5 deletions src/Orm/Builder/Component/RestPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ public function visitModel(ModelInterface $model)
$client = $this->getClient();
$entity = $model->getEntity();
if ($entity instanceof HasUriCollection) {
$post = new Post([ModelEvents::CREATE], $entity->getUriCollectionMask(), $entity->getHeaders());
$post = new Post([ModelEvents::CREATE], $entity->getUriCollectionMask());
$model->accept($post->setClient($client));

$get = new Get([ModelEvents::FETCH_ALL], $entity->getUriCollectionMask(), $entity->getHeaders());
$get = new Get([ModelEvents::FETCH_ALL], $entity->getUriCollectionMask());
$model->accept($get->setClient($client));
}

if ($entity instanceof HasUriEntity) {
$get = new Get([ModelEvents::FETCH], $entity->getUriEntityMask(), $entity->getHeaders());
$get = new Get([ModelEvents::FETCH], $entity->getUriEntityMask());
$model->accept($get->setClient($client));

$put = new Put([ModelEvents::UPDATE], $entity->getUriEntityMask(), $entity->getHeaders());
$put = new Put([ModelEvents::UPDATE], $entity->getUriEntityMask());
$model->accept($put->setClient($client));

$delete = new Delete([ModelEvents::DELETE], $entity->getUriEntityMask(), $entity->getHeaders());
$delete = new Delete([ModelEvents::DELETE], $entity->getUriEntityMask());
$model->accept($delete->setClient($client));
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Orm/Entity/IsHttpInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
*/
namespace DSchoenbauer\Orm\Entity;

use DSchoenbauer\Orm\Events\Persistence\Http\Client\ClientVisitorInterface;

/**
*
* @author David Schoenbauer
*/
interface IsHttpInterface extends EntityInterface
interface IsHttpInterface extends EntityInterface, ClientVisitorInterface
{
public function getHeaders();

}
35 changes: 35 additions & 0 deletions src/Orm/Events/Persistence/Http/Client/ClientVisiteeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* The MIT License
*
* Copyright 2018 David Schoenbauer.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace DSchoenbauer\Orm\Events\Persistence\Http\Client;

/**
*
* @author David Schoenbauer
*/
interface ClientVisiteeInterface
{

public function accept(ClientVisitorInterface $visitor);
}
36 changes: 36 additions & 0 deletions src/Orm/Events/Persistence/Http/Client/ClientVisitorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* The MIT License
*
* Copyright 2018 David Schoenbauer.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace DSchoenbauer\Orm\Events\Persistence\Http\Client;

use Zend\Http\Client;

/**
*
* @author David Schoenbauer
*/
interface ClientVisitorInterface
{
public function visitClient(Client $client);
}
49 changes: 26 additions & 23 deletions src/Orm/Events/Persistence/Http/Methods/AbstractHttpMethodEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use DSchoenbauer\Orm\Entity\IsHttpInterface;
use DSchoenbauer\Orm\Enum\EventPriorities;
use DSchoenbauer\Orm\Events\AbstractEvent;
use DSchoenbauer\Orm\Events\Persistence\Http\Client\ClientVisiteeInterface;
use DSchoenbauer\Orm\Events\Persistence\Http\Client\ClientVisitorInterface;
use DSchoenbauer\Orm\Events\Persistence\Http\DataExtract\DataExtractorFactory;
use DSchoenbauer\Orm\Exception\HttpErrorException;
use DSchoenbauer\Orm\Framework\InterpolateTrait;
Expand All @@ -40,33 +42,51 @@
*
* @author David Schoenbauer
*/
abstract class AbstractHttpMethodEvent extends AbstractEvent
abstract class AbstractHttpMethodEvent extends AbstractEvent implements ClientVisiteeInterface
{

use InterpolateTrait;

private $dataExtractorFactory;
private $client;
private $uriMask;
private $headers = [];

public function __construct(array $events, $uriMask, array $headers = [], $priority = EventPriorities::ON_TIME)
public function __construct(array $events, $uriMask, $priority = EventPriorities::ON_TIME)
{
$this->setUriMask($uriMask)->setHeaders($headers);
$this->setUriMask($uriMask);
parent::__construct($events, $priority);
}

public function onExecute(EventInterface $event)
{
/* @var $model ModelInterface */
$model = $event->getTarget();
if (!$this->validateModel($model, IsHttpInterface::class)) {
return;
}
$this->setHeaders($model->getEntity()->getHeaders())->applyHeaders($this->getClient());
return $this->send($model);

if ($model->getEntity() instanceof ClientVisitorInterface) {
$this->accept($model->getEntity());
}
$this->setUp($model);
return $this-> send($model);
}

public function accept(ClientVisitorInterface $visitor)
{
$visitor->visitClient($this->getClient());
}

protected function setUp(ModelInterface $model)
{
$this->getClient()
->setUri($this->getUri($model->getData()))
->setMethod($this->getMethod());
}

abstract public function send(ModelInterface $model);

abstract public function getMethod();

public function getDataExtractorFactory()
{
Expand Down Expand Up @@ -125,21 +145,4 @@ public function checkForError(Response $response)
}
throw new HttpErrorException($response->getBody(), $response->getStatusCode());
}

public function getHeaders()
{
return $this->headers;
}

public function setHeaders(array $headers)
{
$this->headers = $headers;
return $this;
}

public function applyHeaders(Client $client)
{
$client->setHeaders($this->getHeaders());
return $this;
}
}
11 changes: 6 additions & 5 deletions src/Orm/Events/Persistence/Http/Methods/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ class Delete extends AbstractHttpMethodEvent

public function send(ModelInterface $model)
{

$response = $this->checkForError($this->getClient()
->setUri($this->getUri($model->getData()))
->setMethod(Request::METHOD_DELETE)
->send());
$response = $this->checkForError($this->getClient()->send());
return $response->isSuccess();
}

public function getMethod()
{
return Request::METHOD_DELETE;
}
}
8 changes: 6 additions & 2 deletions src/Orm/Events/Persistence/Http/Methods/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@
*/
class Get extends AbstractHttpMethodEvent
{

public function send(ModelInterface $model)
{
$this->getClient()->setMethod(Request::METHOD_GET)->setUri($this->getUri($model->getData()));
$model->setData($this->getDataExtractorFactory()->getData($this->checkForError($this->getClient()->send())));
}

public function getMethod()
{
return Request::METHOD_GET;
}
}
7 changes: 2 additions & 5 deletions src/Orm/Events/Persistence/Http/Methods/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ class Post extends AbstractHttpMethodEvent

public function send(ModelInterface $model)
{
$data = $model->getData();
$response = $this->checkForError($this->getClient()->setMethod($this->getMethod())
->setParameterPost($data)
->setUri($this->getUri($data))
->send());
$this->getClient()->setParameterPost($model->getData());
$response = $this->checkForError($this->getClient()->send());
$model->setData($this->getDataExtractorFactory()->getData($response));
}

Expand Down
2 changes: 0 additions & 2 deletions tests/src/Orm/Builder/Component/RestPersistenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public function testVisitModelHasUriEntity()
public function evaluateVisitModel($entityClass, $calls, $maskMethod)
{
$entity = $this->getMockBuilder($entityClass)->getMock();
$entity->expects($this->any())->method('getHeaders')->willReturn([]);
$entity->expects($this->exactly($calls))->method($maskMethod);
$entity->expects($this->exactly($calls))->method('getHeaders');

$model = $this->getMockBuilder(ModelInterface::class)->getMock();
$model->expects($this->exactly($calls))->method('accept')->willReturnSelf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use DSchoenbauer\Orm\Entity\IsHttpInterface;
use DSchoenbauer\Orm\Events\AbstractEvent;
use DSchoenbauer\Orm\Events\Persistence\Http\Client\ClientVisitorInterface;
use DSchoenbauer\Orm\Events\Persistence\Http\DataExtract\DataExtractorFactory;
use DSchoenbauer\Orm\Exception\HttpErrorException;
use DSchoenbauer\Tests\Orm\Events\Persistence\Http\DataExtract\TestResponseTrait;
Expand Down Expand Up @@ -63,16 +64,22 @@ public function testOnExecuteNoGood(){

public function testOnExecuteGood(){
$entity = $this->getMockBuilder(IsHttpInterface::class)->getMock();
$entity->expects($this->any())->method('getHeaders')->willReturn([]);
$model = $this->getModel(0,[],$entity);

$event = $this->getMockBuilder(EventInterface::class)->getMock();
$event->expects($this->any())->method('getTarget')->willReturn($model);

$this->object->expects($this->exactly(1))->method('send')->with($model);
$this->object->expects($this->once())->method('getMethod')->willReturn('GET');
$this->assertNull($this->object->onExecute($event));
}


public function testAccept(){
$client = $this->getMockBuilder(Client::class)->getMock();
$visitor = $this->getMockBuilder(ClientVisitorInterface::class)->getMock();
$visitor->expects($this->once())->method('visitClient')->with($client);
$this->object->setClient($client)->accept($visitor);
}

public function testIsEvent()
{
Expand Down Expand Up @@ -143,17 +150,4 @@ public function testCheckForErrorNoError()
$this->expectExceptionMessage("some body");
$this->assertTrue($this->object->checkForError($this->getResponse("","some body", 500)));
}

public function testHeaders(){
$data = ['test'=>'value'];
$this->assertEquals([],$this->object->getHeaders());
$this->assertEquals($data,$this->object->setHeaders($data)->getHeaders());
}

public function testApplyHeaders(){
$data = ['test'=>'value'];
$client = $this->getMockBuilder(Client::class)->getMock();
$client->expects($this->any())->method('setHeaders')->with($data);
$client = $this->object->setHeaders($data)->applyHeaders($client);
}
}
8 changes: 4 additions & 4 deletions tests/src/Orm/Events/Persistence/Http/Methods/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public function testRun()
$model = $this->getModel(1998, [], $this->getIsHttp(null, 'bobsYouUncle', 'bobsYourAunt'));

$client = $this->getMockBuilder(Client::class)->getMock();
$client->expects($this->once())->method('setUri')->with('bobsYouUncle')->willReturnSelf();
$client->expects($this->once())->method('setMethod')->with(Request::METHOD_DELETE)->willReturnSelf();
$client->expects($this->once())->method('send')->willReturn($response);

$this->object->setUriMask('bobsYouUncle')->setClient($client)->send($model);
Expand All @@ -75,10 +73,12 @@ public function testRunFail()
$model = $this->getModel(1998, [], $this->getIsHttp(null, 'bobsYouUncle', 'bobsYourAunt'));

$client = $this->getMockBuilder(Client::class)->getMock();
$client->expects($this->once())->method('setUri')->with('bobsYouUncle')->willReturnSelf();
$client->expects($this->once())->method('setMethod')->with(Request::METHOD_DELETE)->willReturnSelf();
$client->expects($this->once())->method('send')->willReturn($response);

$this->object->setUriMask('bobsYouUncle')->setClient($client)->send($model);
}

public function testMethod(){
$this->assertEquals(Request::METHOD_DELETE, $this->object->getMethod());
}
}
9 changes: 5 additions & 4 deletions tests/src/Orm/Events/Persistence/Http/Methods/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public function testRun()
$model->expects($this->once())->method('setData')->with($data);

$client = $this->getMockBuilder(Client::class)->getMock();
$client->expects($this->any())->method('setMethod')->with(Request::METHOD_GET)->willReturnSelf();
$client->expects($this->any())->method('setUri')->with('entity')->willReturnSelf();
$client->expects($this->any())->method('send')->willReturn($this->getResponse('somethingJson', json_encode($data)));

$this->object->setUriMask('entity')->setClient($client)->send($model);
Expand All @@ -76,10 +74,13 @@ public function testRunFail()
$model = $this->getModel(1999, $data, $this->getIsHttp('id', 'entity', 'collection'));

$client = $this->getMockBuilder(Client::class)->getMock();
$client->expects($this->any())->method('setMethod')->with(Request::METHOD_GET)->willReturnSelf();
$client->expects($this->any())->method('setUri')->with('entity')->willReturnSelf();
$client->expects($this->any())->method('send')->willReturn($this->getResponse('somethingJson', json_encode($data), 500));

$this->object->setUriMask('entity')->setClient($client)->send($model);
}


public function testMethod(){
$this->assertEquals(Request::METHOD_GET, $this->object->getMethod());
}
}
8 changes: 4 additions & 4 deletions tests/src/Orm/Events/Persistence/Http/Methods/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public function testSend()
$response = $this->getResponse('something/json', \json_encode($data));

$client = $this->getMockBuilder(Client::class)->getMock();
$client->expects($this->once())->method('setUri')->with('bobsYourUncle')->willReturnSelf();
$client->expects($this->once())->method('setParameterPost')->with($data)->willReturnSelf();
$client->expects($this->once())->method('setMethod')->with(Request::METHOD_POST)->willReturnSelf();
$client->expects($this->once())->method('send')->willReturn($response);

$model = $this->getModel(0, $data, $this->getIsHttp('id', 'bobsYourAunt', 'bobsYourUncle'));
Expand All @@ -81,13 +79,15 @@ public function testSendFail()
$response = $this->getResponse('something/json', \json_encode($data), 500);

$client = $this->getMockBuilder(Client::class)->getMock();
$client->expects($this->once())->method('setUri')->with('bobsYourUncle')->willReturnSelf();
$client->expects($this->once())->method('setParameterPost')->with($data)->willReturnSelf();
$client->expects($this->once())->method('setMethod')->with(Request::METHOD_POST)->willReturnSelf();
$client->expects($this->once())->method('send')->willReturn($response);

$model = $this->getModel(0, $data, $this->getIsHttp('id', 'bobsYourAunt', 'bobsYourUncle'));

$this->object->setUriMask('bobsYourUncle')->setClient($client)->send($model);
}

public function testMethod(){
$this->assertEquals(Request::METHOD_POST, $this->object->getMethod());
}
}

0 comments on commit 457b16d

Please sign in to comment.