Skip to content

Commit

Permalink
Merge pull request #8 from trms/resolve-partial-bulletins
Browse files Browse the repository at this point in the history
provide a method of resolving partial bulletins
  • Loading branch information
sethphillips authored Sep 27, 2017
2 parents ff8f259 + 0c29854 commit a376092
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ A Bulletin is a piece of content displayed in Carousel. The closest analogy wou
|------|----------|-------|-----------|
|constructor|associative array|Zone Object|Constructor for the class, properties passed to it will be used to define the Bulletin.|
|fromTemplate (static)|Template Object|Bulletin Object|Create a new unsaved bulletin from a template.|
|resolvePartial|none|none|Resolves partial bulletins by getting them by id from the server. See the property `PartialBulletin` for more information. This function is called when trying to save partial bulletins.|
|**Relationships**|
|setBackground|Media Object|self - chainable|Sets the background Media to be used in this model.|
|getBackground|none|Media Object|Gets the related background Media model.|
Expand Down Expand Up @@ -207,7 +208,7 @@ A Bulletin is a piece of content displayed in Carousel. The closest analogy wou
|RepeatInterval|int|How often a repeating bulletin repeats.|
|LastUpdate|string|Date and time of the last rendering of this bulletin.|
|LastError|string|The last error encountered when rendering this bulletin|
|PartialBulletin|boolean (read only)|Getting bulletins from the server via any query other than `id` will result in a partial bulletin. Partial bulletins do not contain Blocks and are not saveable entities.|
|PartialBulletin|boolean (read only)|Getting bulletins from the server via any query other than `id` will result in a partial bulletin. Partial bulletins do not contain some relationship data including Blocks and many of the properties for dynamic bulletins, if you need to act on these properties call `resolvePartial()` in order to get those properties from the server.|
|Status|enumerable (read only)|One of the following values: Current, Queued, Hold, Old, Saved, Current-Null (no dynamic content), Current-Error|
|Tags|array |An Array of Tag Objects|
|TrackImpressions|boolean|Should the service track each time this bulletin is played on a player|
Expand Down
68 changes: 68 additions & 0 deletions Tests/APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,72 @@ function test_boolean_values_are_converted_to_strings_on_get()

$this->assertEquals('server/carouselapi/v1/bulletins?IsDeleted=true&ZoneID=5', (string) $mock->getLastRequest()->getUri());
}

function test_the_api_will_resolve_a_PartialBulletin_before_trying_to_save_it()
{
$mockResponder = new MockResponder;
$mock = new MockHandler([
new Response(200,[],$mockResponder->bulletin()),
new Response(200,[],$mockResponder->bulletin()),
]);
$handler = HandlerStack::create($mock);

$bulletinMock = \Mockery::mock(Bulletin::class)->makePartial();
$bulletinMock->PartialBulletin = true;
$bulletinMock->setApi(new API());

$bulletinMock->shouldReceive('resolvePartial')
->once()
->andReturn($bulletinMock);

$server = new API();
$server
->addMockHandler($handler)
->connect('server','username','password')
->save($bulletinMock);

\Mockery::close();
}

function test_the_api_will_not_resolve_a_non_PartialBulletin_before_trying_to_save_it()
{
$mockResponder = new MockResponder;
$mock = new MockHandler([
new Response(200,[],$mockResponder->bulletin()),
new Response(200,[],$mockResponder->bulletin()),
]);
$handler = HandlerStack::create($mock);

$bulletinMock = \Mockery::mock(Bulletin::class)->makePartial();

$bulletinMock->shouldNotReceive('resolvePartial')
->andReturn($bulletinMock);

$server = new API();
$server
->addMockHandler($handler)
->connect('server','username','password')
->save($bulletinMock);

\Mockery::close();
}

function test_the_api_sets_the_blocks_correctly_when_saving_a_bulletin()
{
$mockResponder = new MockResponder;
$mock = new MockHandler([
new Response(200,[],json_encode(['Blocks'=>[[],[]]])),
]);
$handler = HandlerStack::create($mock);

$server = new API();
$bulletin = new Bulletin(['id'=>'1','Blocks'=>[[],[],[]]]);
$server
->addMockHandler($handler)
->connect('server','username','password')
->save($bulletin);

$this->assertEquals(2, count($bulletin->Blocks));

}
}
43 changes: 43 additions & 0 deletions Tests/BulletinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use TRMS\Carousel\Server\API;
use TRMS\Carousel\Exceptions\CarouselModelException;
use TRMS\Carousel\Requests\ModelRequest;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
Expand All @@ -35,6 +36,48 @@ function test_bulletin_blocks_serialize_correctly()
$this->assertEquals(['BlockType'=>'Rectangle'], $bulletin->toArray()['Blocks'][1]);
}

function test_the_resolvePartial_method_will_get_partial_bulletins_from_the_api()
{
$mockApi = \Mockery::mock(API::class);
$mockApi->shouldReceive('get')
->once()
->andReturn(new Bulletin(['Blocks'=>[[],[]]]));

$bulletin = new Bulletin(['id'=>'1','PartialBulletin'=>true],$mockApi);
$bulletin->resolvePartial();

$this->assertEquals(2, count($bulletin->Blocks));
$this->assertInstanceOf(BulletinBlock::class, $bulletin->Blocks[0]);
$this->assertEquals(false, $bulletin->PartialBulletin);
\Mockery::close();
}

function test_calling_resolvePartial_on_a_non_partial_will_not_use_the_api()
{
$mockApi = \Mockery::mock(API::class);
$mockApi->shouldNotReceive('get')
->andReturn(new Bulletin(['Blocks'=>[[],[]]]));

$bulletin = new Bulletin(['id'=>'1','PartialBulletin'=>false],$mockApi);

$bulletin->resolvePartial();

\Mockery::close();
}

function test_calling_resolvePartial_on_a_new_bulletin_will_result_in_an_exception()
{

$bulletin = new Bulletin(['id'=>'1','PartialBulletin'=>true]);

try{
$bulletin->resolvePartial();
} catch(CarouselModelException $e){
return;
}

$this->fail('the exception was not called');
}

function test_you_can_add_a_group_relationship_after_instantiation()
{
Expand Down
4 changes: 3 additions & 1 deletion src/Models/Bulletin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use TRMS\Carousel\Models\Traits\HasBackground;
use TRMS\Carousel\Models\Traits\HasUser;
use TRMS\Carousel\Models\Traits\HasTags;
use TRMS\Carousel\Models\Traits\ResolvesPartial;

use TRMS\Carousel\Server\API;
use TRMS\Carousel\Exceptions\CarouselModelException;
use TRMS\Carousel\Requests\ModelRequest;

class Bulletin extends CarouselModel
{

use ResolvesPartial;
use HasBlocks;
use HasBackground;
use HasUser;
Expand Down
1 change: 1 addition & 0 deletions src/Models/Traits/HasBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ trait HasBlocks

private function setBlocksFromProps($props)
{
$this->Blocks = [];
if(isset($props['Blocks'])){
foreach ($props['Blocks'] as $blockprops) {
$this->addBlock(new BulletinBlock($blockprops));
Expand Down
33 changes: 33 additions & 0 deletions src/Models/Traits/ResolvesPartial.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php namespace TRMS\Carousel\Models\Traits;

use TRMS\Carousel\Exceptions\CarouselModelException;
use TRMS\Carousel\Requests\ModelRequest;
use TRMS\Carousel\Models\Bulletin;

trait ResolvesPartial
{
public function resolvePartial()
{
if(!$this->PartialBulletin){
return $this;
}
if(!$this->api){
throw new CarouselModelException('Calling resolvePartial on unsaved bulletins is not allowed. Use the API save method to save this bulletin on the server.');
}
$request = new ModelRequest(Bulletin::class, ['id'=>$this->id]);
$result = $this->api->get($request);
$this->applyPartialProps($result);
$this->PartialBulletin = false;
return $this;
}

private function applyPartialProps($serverResponse)
{
$props = collect($serverResponse->toArray())->filter(function($value, $key){
return empty($this->$key) && $this->$key !== false && $this->$key !== 0;
})->toArray();

$this->setProps($props);
$this->setBlocksFromProps($serverResponse->toArray());
}
}
4 changes: 3 additions & 1 deletion src/Server/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public function save(CarouselModel $model)
{
$endpoint = $model->getSaveEndpoint();
$method = $model->getSaveMethod();

if($model->getApi() && $model->PartialBulletin){
$model->resolvePartial();
}
$options = [
'headers'=>[
'Content-Type'=>'application/json'
Expand Down

0 comments on commit a376092

Please sign in to comment.