Skip to content

Commit f9ec20d

Browse files
committed
initial feature set with bulletins groups and media
1 parent ccd0979 commit f9ec20d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1558
-152
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor
2+
.DS_Store

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This package is under development and will evolve. Documentation is soon to come so bear with us. Feel free to reach out to me directly in the meantime if you want to use the package and have questions.

Diff for: Tests/APITest.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
use TRMS\Carousel\Models\Bulletin;
77
use TRMS\Carousel\Models\Group;
88

9+
use TRMS\Carousel\Requests\BulletinRequest;
10+
use TRMS\Carousel\Requests\GroupRequest;
11+
12+
use TRMS\Carousel\Requests\ModelRequest;
13+
914
use CarouselTests\MockData\MockResponder;
1015

1116
use GuzzleHttp\Handler\MockHandler;
@@ -63,6 +68,7 @@ function test_the_class_will_connect_to_a_carousel_server_and_get_the_current_us
6368
$mockResponder = new MockResponder;
6469
$mock = new MockHandler([
6570
new Response(200,[],$mockResponder->whoAmI()),
71+
new Response(200,[],$mockResponder->user()),
6672
]);
6773
$handler = HandlerStack::create($mock);
6874

@@ -75,6 +81,7 @@ function test_the_class_will_connect_to_a_carousel_server_and_get_the_current_us
7581

7682
$this->assertInstanceOf(User::class, $loggedInUser);
7783
$this->assertEquals('admin', $loggedInUser->id);
84+
$this->assertEquals('Seth', $loggedInUser->FirstName);
7885
$this->assertArraySubset(['id'=>'admin'],$loggedInUser->toArray());
7986
}
8087

@@ -88,10 +95,11 @@ function test_you_can_get_a_list_of_bulletins()
8895
$handler = HandlerStack::create($mock);
8996

9097
$server = new API();
98+
$request = new ModelRequest(Bulletin::class);
9199
$bulletins = $server
92100
->addMockHandler($handler)
93101
->connect('my_server','username','password')
94-
->getBulletins();
102+
->get($request);
95103

96104
$group = $bulletins[0]->getGroup();
97105

@@ -111,10 +119,11 @@ function test_you_can_get_a_single_bulletin_by_id()
111119
$handler = HandlerStack::create($mock);
112120

113121
$server = new API();
122+
$request = new ModelRequest(Bulletin::class,['id'=>'1']);
114123
$bulletin = $server
115124
->addMockHandler($handler)
116125
->connect('my_server','username','password')
117-
->getBulletin('1');
126+
->get($request);
118127

119128
$group = $bulletin->getGroup();
120129

@@ -175,10 +184,11 @@ function test_you_can_get_an_individual_group()
175184
$handler = HandlerStack::create($mock);
176185

177186
$server = new API();
187+
$request = new ModelRequest(Group::class,['id'=>'1']);
178188
$group = $server
179189
->addMockHandler($handler)
180190
->connect('my_server','username','password')
181-
->getGroup('1');
191+
->get($request);
182192

183193
$this->assertInstanceOf(Group::class, $group);
184194
$this->assertEquals(1, $group->id);

Diff for: Tests/BulletinRequestTest.php

-12
This file was deleted.

Diff for: Tests/BulletinTest.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
use CarouselTests\MockData\MockResponder;
44

55
use TRMS\Carousel\Models\Bulletin;
6+
use TRMS\Carousel\Models\Template;
67
use TRMS\Carousel\Models\Group;
8+
use TRMS\Carousel\Models\BulletinBlock;
9+
use TRMS\Carousel\Models\Media;
10+
11+
712
use TRMS\Carousel\Server\API;
813
use TRMS\Carousel\Exceptions\CarouselModelException;
914

@@ -16,6 +21,21 @@
1621

1722
class BulletinTest extends PHPUnit_Framework_TestCase
1823
{
24+
25+
function test_bulletin_blocks_serialize_correctly()
26+
{
27+
$bulletin = new Bulletin();
28+
29+
$block1 = new BulletinBlock(['BlockType'=>'Text','Text'=>'foobarbaz']);
30+
$block2 = new BulletinBlock(['BlockType'=>'Rectangle']);
31+
32+
$bulletin->Blocks = [$block1, $block2];
33+
34+
$this->assertEquals(['BlockType'=>'Text','Text'=>'foobarbaz'], $bulletin->toArray()['Blocks'][0]);
35+
$this->assertEquals(['BlockType'=>'Rectangle'], $bulletin->toArray()['Blocks'][1]);
36+
}
37+
38+
1939
function test_you_can_add_a_group_relationship_after_instantiation()
2040
{
2141
$bulletin = new Bulletin();
@@ -28,7 +48,6 @@ function test_you_can_add_a_group_relationship_after_instantiation()
2848

2949
function test_if_the_group_object_has_not_been_resolved_an_api_request_will_be_made()
3050
{
31-
$mockResponder = new MockResponder;
3251
$mock = new MockHandler([
3352
new Response(200,[],json_encode(['id'=>'12'])),
3453
]);
@@ -59,8 +78,7 @@ function test_getting_the_group_on_new_bulletins_with_no_group_set_will_throw_an
5978

6079
function test_an_array_of_bulletin_params_passed_to_the_constructor_sets_them_on_the_bulletin()
6180
{
62-
$mockResponder = new MockResponder;
63-
$mockBulletins = json_decode($mockResponder->bulletins(),true);
81+
$mockBulletins = json_decode(MockResponder::bulletins(),true);
6482
$bulletinProps = $mockBulletins[0];
6583

6684
$bulletin = new Bulletin($bulletinProps);

Diff for: Tests/DeleteModelTest.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
use TRMS\Carousel\Server\API;
4+
use TRMS\Carousel\Exceptions\CarouselAPIException;
5+
use TRMS\Carousel\Models\Bulletin;
6+
7+
use CarouselTests\MockData\MockResponder;
8+
9+
use GuzzleHttp\Handler\MockHandler;
10+
use GuzzleHttp\HandlerStack;
11+
use GuzzleHttp\Psr7\Response;
12+
use GuzzleHttp\Psr7\Request;
13+
use GuzzleHttp\Exception\RequestException;
14+
15+
class DeleteModelTest extends PHPUnit_Framework_TestCase
16+
{
17+
function test_you_can_delete_a_bulletin()
18+
{
19+
$mock = new MockHandler([
20+
new Response(200,[],json_encode(['IsDeleted'=>true]))
21+
]);
22+
$handler = HandlerStack::create($mock);
23+
24+
$mockResponder = new MockResponder;
25+
$bulletinProps = json_decode($mockResponder->bulletin(),true);
26+
$bulletin = new Bulletin($bulletinProps);
27+
28+
$server = new API();
29+
$server
30+
->addMockHandler($handler)
31+
->connect('server','username','password')
32+
->delete($bulletin);
33+
34+
$this->assertTrue($bulletin->IsDeleted);
35+
$this->assertEquals("server/carouselapi/v1/bulletins/$bulletin->id", (string) $mock->getLastRequest()->getUri());
36+
$this->assertEquals('DELETE', (string) $mock->getLastRequest()->getMethod());
37+
}
38+
39+
function test_failed_deletions_throw_an_exception()
40+
{
41+
$mock = new MockHandler([
42+
new Response(500),
43+
new Response(404),
44+
new Response(401)
45+
]);
46+
$handler = HandlerStack::create($mock);
47+
48+
$mockResponder = new MockResponder;
49+
$bulletinProps = json_decode($mockResponder->bulletin(),true);
50+
$bulletin = new Bulletin($bulletinProps);
51+
52+
$server = new API();
53+
$server
54+
->addMockHandler($handler)
55+
->connect('server','username','password');
56+
57+
58+
try{
59+
$server->delete($bulletin);
60+
} catch (CarouselAPIException $e){
61+
$this->assertTrue(true,"500 exception caught");
62+
}
63+
64+
try{
65+
$server->delete($bulletin);
66+
} catch (CarouselAPIException $e){
67+
$this->assertTrue(true,"404 exception caught");
68+
}
69+
70+
try{
71+
$server->delete($bulletin);
72+
} catch (CarouselAPIException $e){
73+
$this->assertTrue(true,"401 exception caught");
74+
return;
75+
}
76+
77+
$this->fail('the exceptions did not throw');
78+
}
79+
}
80+

Diff for: Tests/FileUploadTest.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
use TRMS\Carousel\Server\API;
4+
use TRMS\Carousel\Requests\FileUploadRequest;
5+
use TRMS\Carousel\Models\Bulletin;
6+
7+
use TRMS\Carousel\Exceptions\CarouselAPIException;
8+
9+
use CarouselTests\MockData\MockResponder;
10+
11+
use GuzzleHttp\Handler\MockHandler;
12+
use GuzzleHttp\HandlerStack;
13+
use GuzzleHttp\Psr7\Response;
14+
use GuzzleHttp\Psr7\Request;
15+
use GuzzleHttp\Exception\RequestException;
16+
17+
class TestCase extends PHPUnit_Framework_TestCase
18+
{
19+
function test_you_can_upload_a_bulletin_from_a_file()
20+
{
21+
$mockResponder = new MockResponder;
22+
$mock = new MockHandler([
23+
new Response(200,[],json_encode([
24+
'Bulletins'=>[['id'=>'1']]
25+
])),
26+
]);
27+
28+
$handler = HandlerStack::create($mock);
29+
30+
$filepath = 'Tests/MockData/mock_file.png';
31+
$fileUpload = new FileUploadRequest(Bulletin::class,['ZoneID'=>1]);
32+
$fileUpload->addFile($filepath);
33+
34+
$server = new API();
35+
$server
36+
->addMockHandler($handler)
37+
->connect('server','username','password');
38+
39+
$bulletins = $server->upload($fileUpload);
40+
41+
$this->assertInstanceOf(Bulletin::class, $bulletins->first());
42+
$this->assertEquals($bulletins->first()->id, '1');
43+
$this->assertEquals('server/carouselapi/v1/bulletins', (string) $mock->getLastRequest()->getUri());
44+
$this->assertEquals('POST', (string) $mock->getLastRequest()->getMethod());
45+
$this->assertGreaterThan(150000,$mock->getLastRequest()->getBody()->stream->getSize(), 'the file is big');
46+
}
47+
48+
function test_you_can_upload_multiple_bulletins_at_once()
49+
{
50+
$mockResponder = new MockResponder;
51+
$mock = new MockHandler([
52+
new Response(200,[],json_encode([
53+
'Bulletins'=>[['id'=>'1']]
54+
])),
55+
new Response(200,[],json_encode([
56+
'Bulletins'=>[['id'=>'2']]
57+
])),
58+
]);
59+
60+
$handler = HandlerStack::create($mock);
61+
62+
$filepath = 'Tests/MockData/mock_file.png';
63+
$fileUpload = new FileUploadRequest(Bulletin::class,['ZoneID'=>1]);
64+
$fileUpload->addFile($filepath)->addFile($filepath);
65+
66+
$server = new API();
67+
$server
68+
->addMockHandler($handler)
69+
->connect('server','username','password');
70+
71+
$bulletins = $server->upload($fileUpload);
72+
73+
$this->assertInstanceOf(Bulletin::class, $bulletins[0]);
74+
$this->assertInstanceOf(Bulletin::class, $bulletins[1]);
75+
$this->assertEquals($bulletins[0]->id, '1');
76+
$this->assertEquals($bulletins[1]->id, '2');
77+
}
78+
79+
function test_failed_uploads_will_return_an_exception()
80+
{
81+
$mockResponder = new MockResponder;
82+
$mock = new MockHandler([
83+
new Response(500,[],json_encode(['Message'=>'File Upload Failed'])),
84+
]);
85+
86+
$handler = HandlerStack::create($mock);
87+
88+
$filepath = 'Tests/MockData/mock_file.png';
89+
$fileUpload = new FileUploadRequest(Bulletin::class,['ZoneID'=>1]);
90+
$fileUpload->addFile($filepath);
91+
92+
$server = new API();
93+
$server
94+
->addMockHandler($handler)
95+
->connect('server','username','password');
96+
97+
$bulletins = $server->upload($fileUpload);
98+
99+
$this->assertInstanceOf('TRMS\Carousel\Exceptions\CarouselAPIException', $bulletins->first());
100+
}
101+
102+
}

Diff for: Tests/HasBackgroundTraitTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
use TRMS\Carousel\Models\Bulletin;
4+
use TRMS\Carousel\Models\Media;
5+
use TRMS\Carousel\Models\Template;
6+
7+
use TRMS\Carousel\Server\API;
8+
9+
use GuzzleHttp\Handler\MockHandler;
10+
use GuzzleHttp\HandlerStack;
11+
use GuzzleHttp\Psr7\Response;
12+
use GuzzleHttp\Psr7\Request;
13+
14+
class HasBackgroundTraitTest extends PHPUnit_Framework_TestCase
15+
{
16+
function test_you_can_add_a_background_to_a_bulletin()
17+
{
18+
$bulletin = new Bulletin();
19+
$background = new Media(['id'=>'1']);
20+
$bulletin->setBackground($background);
21+
22+
$this->assertEquals($background->id, $bulletin->BackgroundID);
23+
}
24+
25+
function test_you_can_add_a_background_to_a_template()
26+
{
27+
$template = new Template();
28+
$background = new Media(['id'=>'1']);
29+
$template->setBackground($background);
30+
31+
$this->assertEquals($background->id, $template->BackgroundID);
32+
}
33+
34+
function test_if_the_background_object_has_not_been_resolved_an_api_request_will_be_made()
35+
{
36+
$mock = new MockHandler([
37+
new Response(200,[],json_encode(['id'=>'12'])),
38+
]);
39+
$handler = HandlerStack::create($mock);
40+
41+
$api = new API();
42+
$api->addMockHandler($handler);
43+
44+
$bulletin = new Bulletin(['BackgroundID'=>'12']);
45+
$bulletin->setApi($api);
46+
$background = $bulletin->getBackground();
47+
48+
$this->assertInstanceOf(Media::class, $background);
49+
$this->assertEquals('12',$background->id);
50+
}
51+
}

0 commit comments

Comments
 (0)