Skip to content

Commit 5f9701c

Browse files
committed
Bulletin order entries get and save
1 parent 685a9e7 commit 5f9701c

7 files changed

Lines changed: 214 additions & 2 deletions

File tree

Tests/BulletinOrderRequestTest.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
use TRMS\Carousel\Server\API;
4+
5+
use TRMS\Carousel\Exceptions\CarouselAPIException;
6+
use TRMS\Carousel\Exceptions\CarouselRequestException;
7+
use TRMS\Carousel\Exceptions\CarouselModelException;
8+
9+
use TRMS\Carousel\Models\Bulletin;
10+
use TRMS\Carousel\Models\Group;
11+
12+
use TRMS\Carousel\Requests\BulletinOrderRequest;
13+
use TRMS\Carousel\Models\BulletinOrder;
14+
use TRMS\Carousel\Models\BulletinOrderEntry;
15+
16+
use CarouselTests\MockData\MockResponder;
17+
18+
use GuzzleHttp\Handler\MockHandler;
19+
use GuzzleHttp\HandlerStack;
20+
use GuzzleHttp\Psr7\Response;
21+
use GuzzleHttp\Psr7\Request;
22+
use GuzzleHttp\Exception\RequestException;
23+
24+
class BulletinOrderRequestTest extends PHPUnit_Framework_TestCase
25+
{
26+
function setup()
27+
{
28+
$mockResponder = new MockResponder;
29+
$this->mock = new MockHandler([
30+
new Response(200,[],$mockResponder->orderEntries()),
31+
new Response(200,[],$mockResponder->orderEntries()),
32+
]);
33+
$this->handler = HandlerStack::create($this->mock);
34+
}
35+
36+
function test_you_can_get_order_entries_for_a_zone()
37+
{
38+
$request = new BulletinOrderRequest(['ZoneID'=>1]);
39+
$server = new API();
40+
$server
41+
->addMockHandler($this->handler)
42+
->connect('server','username','password');
43+
44+
$bulletinOrder = $server->get($request);
45+
46+
$this->assertEquals('server/carouselapi/v1/orderentries?ZoneID=1', (string) $this->mock->getLastRequest()->getUri());
47+
$this->assertEquals('GET', (string) $this->mock->getLastRequest()->getMethod());
48+
$this->assertInstanceOf(BulletinOrder::class, $bulletinOrder);
49+
$this->assertEquals(1, $bulletinOrder->ZoneID);
50+
$this->assertEquals(true, is_array($bulletinOrder->OrderEntries));
51+
$this->assertInstanceOf(BulletinOrderEntry::class, $bulletinOrder->OrderEntries[0]);
52+
}
53+
54+
function test_you_must_supply_a_zone_id_when_requesting_order_entries_or_have_an_exception_thrown()
55+
{
56+
try{
57+
$request = new BulletinOrderRequest([]);
58+
} catch (CarouselRequestException $e){
59+
return;
60+
}
61+
$this->fail('The exception was not thrown');
62+
}
63+
64+
function test_you_can_save_the_order_back_to_the_server()
65+
{
66+
$request = new BulletinOrderRequest(['ZoneID'=>1]);
67+
$server = new API();
68+
$server
69+
->addMockHandler($this->handler)
70+
->connect('server','username','password');
71+
72+
$bulletinOrder = $server->get($request);
73+
74+
$this->assertInstanceOf(BulletinOrder::class, $bulletinOrder);
75+
76+
$server->save($bulletinOrder);
77+
78+
$this->assertEquals('server/carouselapi/v1/orderentries', (string) $this->mock->getLastRequest()->getUri());
79+
$this->assertEquals('POST', (string) $this->mock->getLastRequest()->getMethod());
80+
$this->assertArraySubset($bulletinOrder->toArray(), json_decode((string) $this->mock->getLastRequest()->getBody(),true));
81+
}
82+
83+
function test_trying_to_save_an_order_entry_will_throw_an_exception()
84+
{
85+
$server = new API();
86+
$server
87+
->addMockHandler($this->handler)
88+
->connect('server','username','password');
89+
90+
$orderEntry = new BulletinOrderEntry(['id'=>'1']);
91+
92+
try{
93+
$server->save($orderEntry);
94+
} catch(CarouselModelException $e){
95+
return;
96+
}
97+
98+
$this->fail('The exception was not thrown');
99+
}
100+
101+
function test_trying_to_create_a_bulletin_order_without_OrderEntries_on_the_props_will_throw_an_exception()
102+
{
103+
try{
104+
$fail = new BulletinOrder('1',['prop'=>'not an array']);
105+
} catch(CarouselModelException $e){
106+
return;
107+
}
108+
109+
$this->fail('an exception was not thrown');
110+
}
111+
112+
function test_trying_to_create_a_bulletin_order_without_an_id_on_each_entry_will_throw_an_exception()
113+
{
114+
try{
115+
$fail = new BulletinOrder('1',[['foo'=>'I dont have an id']]);
116+
} catch(CarouselModelException $e){
117+
return;
118+
}
119+
120+
$this->fail('an exception was not thrown');
121+
}
122+
}

Tests/MockData/MockResponder.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,25 @@ static function user()
2929
return json_encode($data);
3030
}
3131

32+
static function orderEntries()
33+
{
34+
$data = [
35+
[
36+
"id"=> "144a801f-8967-420a-9eac-777ca3e6b990",
37+
"GroupID"=> "1",
38+
"Order"=> 1,
39+
"Bulletins"=> ["1"]
40+
],
41+
[
42+
"id"=> "18b1075b-65cb-4193-a6c7-0eba0a1c2f55",
43+
"GroupID"=> "2",
44+
"Order"=> 2,
45+
"Bulletins"=> ["2"]
46+
]
47+
];
3248

49+
return json_encode($data);
50+
}
3351

3452
static function bulletins()
3553
{

src/Models/BulletinOrder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php namespace TRMS\Carousel\Models;
2+
3+
use TRMS\Carousel\Exceptions\CarouselModelException;
4+
use TRMS\Carousel\Models\BulletinOrderEntry;
5+
6+
class BulletinOrder extends CarouselModel
7+
{
8+
protected $endpoint = 'orderentries';
9+
10+
public function __construct(string $ZoneID, Array $props)
11+
{
12+
$this->ZoneID = $ZoneID;
13+
$this->setOrderEntries($props);
14+
}
15+
16+
private function setOrderEntries($props)
17+
{
18+
$this->OrderEntries = collect($props)->map(function($entry){
19+
if(is_array($entry) === false){
20+
throw new CarouselModelException('The properties passed to the the BulletinOrder constructor must be an associative array of OrderEntry properties. This value almost always comes from the Carousel Server and should not be instantiated by a consumer.');
21+
}
22+
return new BulletinOrderEntry($entry);
23+
})->toArray();
24+
}
25+
}

src/Models/BulletinOrderEntry.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace TRMS\Carousel\Models;
2+
3+
use TRMS\Carousel\Exceptions\CarouselModelException;
4+
use TRMS\Carousel\Models\BulletinOrderEntry;
5+
6+
class BulletinOrderEntry extends CarouselModel
7+
{
8+
public function __construct(Array $props)
9+
{
10+
if(isset($props['id']) === false){
11+
throw new CarouselModelException('BulletinOrderEntry must have an id in its props. This value will always come from the Carousel Server and should not be instantiated by a consumer');
12+
}
13+
parent::__construct($props);
14+
}
15+
16+
public function getSaveEndpoint()
17+
{
18+
throw new CarouselModelException('BulletinOrderEntry is not a saveable entity');
19+
}
20+
}

src/Models/CarouselModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ public function setProps(Array $props)
2929
}
3030

3131
public function getSaveMethod(){
32-
if($this->id){
32+
if(isset($this->id)){
3333
return "put";
3434
}
3535
return "post";
3636
}
3737

3838
public function getSaveEndpoint()
3939
{
40-
if($this->id){
40+
if(isset($this->id)){
4141
return "$this->endpoint/$this->id";
4242
}
4343
return $this->endpoint;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php namespace TRMS\Carousel\Requests;
2+
3+
use TRMS\Carousel\Exceptions\CarouselRequestException;
4+
use TRMS\Carousel\Models\BulletinOrder;
5+
use TRMS\Carousel\Requests\Traits\RequestTrait;
6+
7+
class BulletinOrderRequest extends ModelRequest
8+
{
9+
10+
public function __construct(Array $params=[])
11+
{
12+
$this->responseClassName = BulletinOrder::class;
13+
$this->baseUrl = 'orderentries';
14+
15+
if(isset($params['ZoneID']) === false){
16+
throw new CarouselRequestException('You must specify a ZoneID when requesting Bulletin Order');
17+
}
18+
19+
$this->queryParams = $params;
20+
}
21+
}

src/Server/API.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use TRMS\Carousel\Models\User;
77
use TRMS\Carousel\Models\Bulletin;
88
use TRMS\Carousel\Models\Group;
9+
use TRMS\Carousel\Models\BulletinOrder;
910
use TRMS\Carousel\Models\CarouselModel;
1011
use TRMS\Carousel\Exceptions\CarouselAPIException;
1112

@@ -63,6 +64,11 @@ public function get(ModelRequest $request)
6364
return new $responseClass($response,$this);
6465
} else {
6566
$response = $apiRequest->get($request->url(),$request->queryParams);
67+
68+
if($responseClass === BulletinOrder::class){
69+
return new BulletinOrder($request->queryParams['ZoneID'], $response);
70+
}
71+
6672
return collect($response)->filter()->map(function($properties) use ($responseClass){
6773
return new $responseClass($properties,$this);
6874
});

0 commit comments

Comments
 (0)