Skip to content

Commit 0c29854

Browse files
committed
provide a method of resolving partial bulletins
the server’s save method will call this if it detects a partial bulletin attempting to be saved.
1 parent ff8f259 commit 0c29854

7 files changed

Lines changed: 153 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ A Bulletin is a piece of content displayed in Carousel. The closest analogy wou
162162
|------|----------|-------|-----------|
163163
|constructor|associative array|Zone Object|Constructor for the class, properties passed to it will be used to define the Bulletin.|
164164
|fromTemplate (static)|Template Object|Bulletin Object|Create a new unsaved bulletin from a template.|
165+
|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.|
165166
|**Relationships**|
166167
|setBackground|Media Object|self - chainable|Sets the background Media to be used in this model.|
167168
|getBackground|none|Media Object|Gets the related background Media model.|
@@ -207,7 +208,7 @@ A Bulletin is a piece of content displayed in Carousel. The closest analogy wou
207208
|RepeatInterval|int|How often a repeating bulletin repeats.|
208209
|LastUpdate|string|Date and time of the last rendering of this bulletin.|
209210
|LastError|string|The last error encountered when rendering this bulletin|
210-
|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.|
211+
|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.|
211212
|Status|enumerable (read only)|One of the following values: Current, Queued, Hold, Old, Saved, Current-Null (no dynamic content), Current-Error|
212213
|Tags|array |An Array of Tag Objects|
213214
|TrackImpressions|boolean|Should the service track each time this bulletin is played on a player|

Tests/APITest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,72 @@ function test_boolean_values_are_converted_to_strings_on_get()
212212

213213
$this->assertEquals('server/carouselapi/v1/bulletins?IsDeleted=true&ZoneID=5', (string) $mock->getLastRequest()->getUri());
214214
}
215+
216+
function test_the_api_will_resolve_a_PartialBulletin_before_trying_to_save_it()
217+
{
218+
$mockResponder = new MockResponder;
219+
$mock = new MockHandler([
220+
new Response(200,[],$mockResponder->bulletin()),
221+
new Response(200,[],$mockResponder->bulletin()),
222+
]);
223+
$handler = HandlerStack::create($mock);
224+
225+
$bulletinMock = \Mockery::mock(Bulletin::class)->makePartial();
226+
$bulletinMock->PartialBulletin = true;
227+
$bulletinMock->setApi(new API());
228+
229+
$bulletinMock->shouldReceive('resolvePartial')
230+
->once()
231+
->andReturn($bulletinMock);
232+
233+
$server = new API();
234+
$server
235+
->addMockHandler($handler)
236+
->connect('server','username','password')
237+
->save($bulletinMock);
238+
239+
\Mockery::close();
240+
}
241+
242+
function test_the_api_will_not_resolve_a_non_PartialBulletin_before_trying_to_save_it()
243+
{
244+
$mockResponder = new MockResponder;
245+
$mock = new MockHandler([
246+
new Response(200,[],$mockResponder->bulletin()),
247+
new Response(200,[],$mockResponder->bulletin()),
248+
]);
249+
$handler = HandlerStack::create($mock);
250+
251+
$bulletinMock = \Mockery::mock(Bulletin::class)->makePartial();
252+
253+
$bulletinMock->shouldNotReceive('resolvePartial')
254+
->andReturn($bulletinMock);
255+
256+
$server = new API();
257+
$server
258+
->addMockHandler($handler)
259+
->connect('server','username','password')
260+
->save($bulletinMock);
261+
262+
\Mockery::close();
263+
}
264+
265+
function test_the_api_sets_the_blocks_correctly_when_saving_a_bulletin()
266+
{
267+
$mockResponder = new MockResponder;
268+
$mock = new MockHandler([
269+
new Response(200,[],json_encode(['Blocks'=>[[],[]]])),
270+
]);
271+
$handler = HandlerStack::create($mock);
272+
273+
$server = new API();
274+
$bulletin = new Bulletin(['id'=>'1','Blocks'=>[[],[],[]]]);
275+
$server
276+
->addMockHandler($handler)
277+
->connect('server','username','password')
278+
->save($bulletin);
279+
280+
$this->assertEquals(2, count($bulletin->Blocks));
281+
282+
}
215283
}

Tests/BulletinTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use TRMS\Carousel\Server\API;
1313
use TRMS\Carousel\Exceptions\CarouselModelException;
14+
use TRMS\Carousel\Requests\ModelRequest;
1415

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

39+
function test_the_resolvePartial_method_will_get_partial_bulletins_from_the_api()
40+
{
41+
$mockApi = \Mockery::mock(API::class);
42+
$mockApi->shouldReceive('get')
43+
->once()
44+
->andReturn(new Bulletin(['Blocks'=>[[],[]]]));
45+
46+
$bulletin = new Bulletin(['id'=>'1','PartialBulletin'=>true],$mockApi);
47+
$bulletin->resolvePartial();
48+
49+
$this->assertEquals(2, count($bulletin->Blocks));
50+
$this->assertInstanceOf(BulletinBlock::class, $bulletin->Blocks[0]);
51+
$this->assertEquals(false, $bulletin->PartialBulletin);
52+
\Mockery::close();
53+
}
54+
55+
function test_calling_resolvePartial_on_a_non_partial_will_not_use_the_api()
56+
{
57+
$mockApi = \Mockery::mock(API::class);
58+
$mockApi->shouldNotReceive('get')
59+
->andReturn(new Bulletin(['Blocks'=>[[],[]]]));
60+
61+
$bulletin = new Bulletin(['id'=>'1','PartialBulletin'=>false],$mockApi);
62+
63+
$bulletin->resolvePartial();
64+
65+
\Mockery::close();
66+
}
67+
68+
function test_calling_resolvePartial_on_a_new_bulletin_will_result_in_an_exception()
69+
{
70+
71+
$bulletin = new Bulletin(['id'=>'1','PartialBulletin'=>true]);
72+
73+
try{
74+
$bulletin->resolvePartial();
75+
} catch(CarouselModelException $e){
76+
return;
77+
}
78+
79+
$this->fail('the exception was not called');
80+
}
3881

3982
function test_you_can_add_a_group_relationship_after_instantiation()
4083
{

src/Models/Bulletin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use TRMS\Carousel\Models\Traits\HasBackground;
88
use TRMS\Carousel\Models\Traits\HasUser;
99
use TRMS\Carousel\Models\Traits\HasTags;
10+
use TRMS\Carousel\Models\Traits\ResolvesPartial;
1011

1112
use TRMS\Carousel\Server\API;
1213
use TRMS\Carousel\Exceptions\CarouselModelException;
14+
use TRMS\Carousel\Requests\ModelRequest;
1315

1416
class Bulletin extends CarouselModel
1517
{
16-
18+
use ResolvesPartial;
1719
use HasBlocks;
1820
use HasBackground;
1921
use HasUser;

src/Models/Traits/HasBlocks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ trait HasBlocks
99

1010
private function setBlocksFromProps($props)
1111
{
12+
$this->Blocks = [];
1213
if(isset($props['Blocks'])){
1314
foreach ($props['Blocks'] as $blockprops) {
1415
$this->addBlock(new BulletinBlock($blockprops));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php namespace TRMS\Carousel\Models\Traits;
2+
3+
use TRMS\Carousel\Exceptions\CarouselModelException;
4+
use TRMS\Carousel\Requests\ModelRequest;
5+
use TRMS\Carousel\Models\Bulletin;
6+
7+
trait ResolvesPartial
8+
{
9+
public function resolvePartial()
10+
{
11+
if(!$this->PartialBulletin){
12+
return $this;
13+
}
14+
if(!$this->api){
15+
throw new CarouselModelException('Calling resolvePartial on unsaved bulletins is not allowed. Use the API save method to save this bulletin on the server.');
16+
}
17+
$request = new ModelRequest(Bulletin::class, ['id'=>$this->id]);
18+
$result = $this->api->get($request);
19+
$this->applyPartialProps($result);
20+
$this->PartialBulletin = false;
21+
return $this;
22+
}
23+
24+
private function applyPartialProps($serverResponse)
25+
{
26+
$props = collect($serverResponse->toArray())->filter(function($value, $key){
27+
return empty($this->$key) && $this->$key !== false && $this->$key !== 0;
28+
})->toArray();
29+
30+
$this->setProps($props);
31+
$this->setBlocksFromProps($serverResponse->toArray());
32+
}
33+
}

src/Server/API.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public function save(CarouselModel $model)
7979
{
8080
$endpoint = $model->getSaveEndpoint();
8181
$method = $model->getSaveMethod();
82-
82+
if($model->getApi() && $model->PartialBulletin){
83+
$model->resolvePartial();
84+
}
8385
$options = [
8486
'headers'=>[
8587
'Content-Type'=>'application/json'

0 commit comments

Comments
 (0)