Skip to content

Commit

Permalink
initial feature set with bulletins groups and media
Browse files Browse the repository at this point in the history
  • Loading branch information
sethphillips committed Sep 15, 2017
1 parent ccd0979 commit f9ec20d
Show file tree
Hide file tree
Showing 43 changed files with 1,558 additions and 152 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +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.
16 changes: 13 additions & 3 deletions Tests/APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use TRMS\Carousel\Models\Bulletin;
use TRMS\Carousel\Models\Group;

use TRMS\Carousel\Requests\BulletinRequest;
use TRMS\Carousel\Requests\GroupRequest;

use TRMS\Carousel\Requests\ModelRequest;

use CarouselTests\MockData\MockResponder;

use GuzzleHttp\Handler\MockHandler;
Expand Down Expand Up @@ -63,6 +68,7 @@ function test_the_class_will_connect_to_a_carousel_server_and_get_the_current_us
$mockResponder = new MockResponder;
$mock = new MockHandler([
new Response(200,[],$mockResponder->whoAmI()),
new Response(200,[],$mockResponder->user()),
]);
$handler = HandlerStack::create($mock);

Expand All @@ -75,6 +81,7 @@ function test_the_class_will_connect_to_a_carousel_server_and_get_the_current_us

$this->assertInstanceOf(User::class, $loggedInUser);
$this->assertEquals('admin', $loggedInUser->id);
$this->assertEquals('Seth', $loggedInUser->FirstName);
$this->assertArraySubset(['id'=>'admin'],$loggedInUser->toArray());
}

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

$server = new API();
$request = new ModelRequest(Bulletin::class);
$bulletins = $server
->addMockHandler($handler)
->connect('my_server','username','password')
->getBulletins();
->get($request);

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

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

$server = new API();
$request = new ModelRequest(Bulletin::class,['id'=>'1']);
$bulletin = $server
->addMockHandler($handler)
->connect('my_server','username','password')
->getBulletin('1');
->get($request);

$group = $bulletin->getGroup();

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

$server = new API();
$request = new ModelRequest(Group::class,['id'=>'1']);
$group = $server
->addMockHandler($handler)
->connect('my_server','username','password')
->getGroup('1');
->get($request);

$this->assertInstanceOf(Group::class, $group);
$this->assertEquals(1, $group->id);
Expand Down
12 changes: 0 additions & 12 deletions Tests/BulletinRequestTest.php

This file was deleted.

24 changes: 21 additions & 3 deletions Tests/BulletinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
use CarouselTests\MockData\MockResponder;

use TRMS\Carousel\Models\Bulletin;
use TRMS\Carousel\Models\Template;
use TRMS\Carousel\Models\Group;
use TRMS\Carousel\Models\BulletinBlock;
use TRMS\Carousel\Models\Media;


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

Expand All @@ -16,6 +21,21 @@

class BulletinTest extends PHPUnit_Framework_TestCase
{

function test_bulletin_blocks_serialize_correctly()
{
$bulletin = new Bulletin();

$block1 = new BulletinBlock(['BlockType'=>'Text','Text'=>'foobarbaz']);
$block2 = new BulletinBlock(['BlockType'=>'Rectangle']);

$bulletin->Blocks = [$block1, $block2];

$this->assertEquals(['BlockType'=>'Text','Text'=>'foobarbaz'], $bulletin->toArray()['Blocks'][0]);
$this->assertEquals(['BlockType'=>'Rectangle'], $bulletin->toArray()['Blocks'][1]);
}


function test_you_can_add_a_group_relationship_after_instantiation()
{
$bulletin = new Bulletin();
Expand All @@ -28,7 +48,6 @@ function test_you_can_add_a_group_relationship_after_instantiation()

function test_if_the_group_object_has_not_been_resolved_an_api_request_will_be_made()
{
$mockResponder = new MockResponder;
$mock = new MockHandler([
new Response(200,[],json_encode(['id'=>'12'])),
]);
Expand Down Expand Up @@ -59,8 +78,7 @@ function test_getting_the_group_on_new_bulletins_with_no_group_set_will_throw_an

function test_an_array_of_bulletin_params_passed_to_the_constructor_sets_them_on_the_bulletin()
{
$mockResponder = new MockResponder;
$mockBulletins = json_decode($mockResponder->bulletins(),true);
$mockBulletins = json_decode(MockResponder::bulletins(),true);
$bulletinProps = $mockBulletins[0];

$bulletin = new Bulletin($bulletinProps);
Expand Down
80 changes: 80 additions & 0 deletions Tests/DeleteModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

use TRMS\Carousel\Server\API;
use TRMS\Carousel\Exceptions\CarouselAPIException;
use TRMS\Carousel\Models\Bulletin;

use CarouselTests\MockData\MockResponder;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;

class DeleteModelTest extends PHPUnit_Framework_TestCase
{
function test_you_can_delete_a_bulletin()
{
$mock = new MockHandler([
new Response(200,[],json_encode(['IsDeleted'=>true]))
]);
$handler = HandlerStack::create($mock);

$mockResponder = new MockResponder;
$bulletinProps = json_decode($mockResponder->bulletin(),true);
$bulletin = new Bulletin($bulletinProps);

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

$this->assertTrue($bulletin->IsDeleted);
$this->assertEquals("server/carouselapi/v1/bulletins/$bulletin->id", (string) $mock->getLastRequest()->getUri());
$this->assertEquals('DELETE', (string) $mock->getLastRequest()->getMethod());
}

function test_failed_deletions_throw_an_exception()
{
$mock = new MockHandler([
new Response(500),
new Response(404),
new Response(401)
]);
$handler = HandlerStack::create($mock);

$mockResponder = new MockResponder;
$bulletinProps = json_decode($mockResponder->bulletin(),true);
$bulletin = new Bulletin($bulletinProps);

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


try{
$server->delete($bulletin);
} catch (CarouselAPIException $e){
$this->assertTrue(true,"500 exception caught");
}

try{
$server->delete($bulletin);
} catch (CarouselAPIException $e){
$this->assertTrue(true,"404 exception caught");
}

try{
$server->delete($bulletin);
} catch (CarouselAPIException $e){
$this->assertTrue(true,"401 exception caught");
return;
}

$this->fail('the exceptions did not throw');
}
}

102 changes: 102 additions & 0 deletions Tests/FileUploadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

use TRMS\Carousel\Server\API;
use TRMS\Carousel\Requests\FileUploadRequest;
use TRMS\Carousel\Models\Bulletin;

use TRMS\Carousel\Exceptions\CarouselAPIException;

use CarouselTests\MockData\MockResponder;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;

class TestCase extends PHPUnit_Framework_TestCase
{
function test_you_can_upload_a_bulletin_from_a_file()
{
$mockResponder = new MockResponder;
$mock = new MockHandler([
new Response(200,[],json_encode([
'Bulletins'=>[['id'=>'1']]
])),
]);

$handler = HandlerStack::create($mock);

$filepath = 'Tests/MockData/mock_file.png';
$fileUpload = new FileUploadRequest(Bulletin::class,['ZoneID'=>1]);
$fileUpload->addFile($filepath);

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

$bulletins = $server->upload($fileUpload);

$this->assertInstanceOf(Bulletin::class, $bulletins->first());
$this->assertEquals($bulletins->first()->id, '1');
$this->assertEquals('server/carouselapi/v1/bulletins', (string) $mock->getLastRequest()->getUri());
$this->assertEquals('POST', (string) $mock->getLastRequest()->getMethod());
$this->assertGreaterThan(150000,$mock->getLastRequest()->getBody()->stream->getSize(), 'the file is big');
}

function test_you_can_upload_multiple_bulletins_at_once()
{
$mockResponder = new MockResponder;
$mock = new MockHandler([
new Response(200,[],json_encode([
'Bulletins'=>[['id'=>'1']]
])),
new Response(200,[],json_encode([
'Bulletins'=>[['id'=>'2']]
])),
]);

$handler = HandlerStack::create($mock);

$filepath = 'Tests/MockData/mock_file.png';
$fileUpload = new FileUploadRequest(Bulletin::class,['ZoneID'=>1]);
$fileUpload->addFile($filepath)->addFile($filepath);

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

$bulletins = $server->upload($fileUpload);

$this->assertInstanceOf(Bulletin::class, $bulletins[0]);
$this->assertInstanceOf(Bulletin::class, $bulletins[1]);
$this->assertEquals($bulletins[0]->id, '1');
$this->assertEquals($bulletins[1]->id, '2');
}

function test_failed_uploads_will_return_an_exception()
{
$mockResponder = new MockResponder;
$mock = new MockHandler([
new Response(500,[],json_encode(['Message'=>'File Upload Failed'])),
]);

$handler = HandlerStack::create($mock);

$filepath = 'Tests/MockData/mock_file.png';
$fileUpload = new FileUploadRequest(Bulletin::class,['ZoneID'=>1]);
$fileUpload->addFile($filepath);

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

$bulletins = $server->upload($fileUpload);

$this->assertInstanceOf('TRMS\Carousel\Exceptions\CarouselAPIException', $bulletins->first());
}

}
51 changes: 51 additions & 0 deletions Tests/HasBackgroundTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use TRMS\Carousel\Models\Bulletin;
use TRMS\Carousel\Models\Media;
use TRMS\Carousel\Models\Template;

use TRMS\Carousel\Server\API;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;

class HasBackgroundTraitTest extends PHPUnit_Framework_TestCase
{
function test_you_can_add_a_background_to_a_bulletin()
{
$bulletin = new Bulletin();
$background = new Media(['id'=>'1']);
$bulletin->setBackground($background);

$this->assertEquals($background->id, $bulletin->BackgroundID);
}

function test_you_can_add_a_background_to_a_template()
{
$template = new Template();
$background = new Media(['id'=>'1']);
$template->setBackground($background);

$this->assertEquals($background->id, $template->BackgroundID);
}

function test_if_the_background_object_has_not_been_resolved_an_api_request_will_be_made()
{
$mock = new MockHandler([
new Response(200,[],json_encode(['id'=>'12'])),
]);
$handler = HandlerStack::create($mock);

$api = new API();
$api->addMockHandler($handler);

$bulletin = new Bulletin(['BackgroundID'=>'12']);
$bulletin->setApi($api);
$background = $bulletin->getBackground();

$this->assertInstanceOf(Media::class, $background);
$this->assertEquals('12',$background->id);
}
}
Loading

0 comments on commit f9ec20d

Please sign in to comment.