Skip to content

Commit

Permalink
refactor singleton trait and adding more provider unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrique Perez committed Aug 3, 2017
1 parent c0e3e1e commit deeee29
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 57 deletions.
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit bootstrap="vendor/autoload.php"
<phpunit bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand All @@ -9,4 +9,4 @@
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
11 changes: 10 additions & 1 deletion src/Entities/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
class Tag extends JsonSerializableObject
{
protected $id;
protected $uid;
protected $uuid;
protected $name;
protected $description;
protected $slug;
protected $hidden;
protected $visibility;
protected $parent;
protected $image;
protected $metaTitle;
Expand All @@ -20,4 +22,11 @@ class Tag extends JsonSerializableObject
protected $updatedAt;
protected $updatedBy;

/**
* @return integer
*/
public function getId()
{
return $this->id;
}
}
7 changes: 7 additions & 0 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ class User extends JsonSerializableObject
protected $updatedBy;
protected $website;

/**
* @return integer
*/
public function getId()
{
return $this->id;
}
}
1 change: 1 addition & 0 deletions src/GhostIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GhostIO;

use GhostIO\Providers\PostProvider;
use GhostIO\Providers\TagProvider;

class GhostIO
{
Expand Down
4 changes: 3 additions & 1 deletion src/Providers/PostProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use GhostIO\Entities\Post;
use GhostIO\Utils\Collection;
use GhostIO\Providers\Traits\Singleton;

class PostProvider extends AbstractProvider
class PostProvider
{
use Singleton;

/**
* Method to get all the posts of the account. Be carefull with this
Expand Down
8 changes: 5 additions & 3 deletions src/Providers/TagProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use GhostIO\Entities\Tag;
use GhostIO\Utils\Collection;
use GhostIO\Providers\Traits\Singleton;

class TagProvider extends AbstractProvider
class TagProvider
{
use Singleton;

/**
* Method to get all the tags of the account.
Expand All @@ -26,7 +28,7 @@ public function getAll(array $fields = [])

// Make sure we are getting some tags
$response_data = json_decode($response->getBody()->getContents());
if (!$response_data->tags) {
if (!isset($response_data->tags)) {
throw new \Exception('Unable to get the tags.');
}

Expand All @@ -50,7 +52,7 @@ public function getById($tagId)

// Make sure we are getting some tags
$response_data = json_decode($response->getBody()->getContents());
if (!$response_data->tags) {
if (!isset($response_data->tags)) {
throw new \Exception('Unable to get the tags.');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<?php

namespace GhostIO\Providers;
namespace GhostIO\Providers\Traits;

use GhostIO\Client;

class AbstractProvider
/*
|--------------------------------------------------------------------------
| Singleton Provider Trait
|--------------------------------------------------------------------------
|
| This trait need to be added to all the provider classes so they work
| as singleton instances.
|
*/

trait Singleton
{
protected static $instance;

Expand Down
12 changes: 7 additions & 5 deletions src/Providers/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use GhostIO\Entities\User;
use GhostIO\Utils\Collection;
use GhostIO\Providers\Traits\Singleton;

class UserProvider extends AbstractProvider
class UserProvider
{
use Singleton;

/**
* Method to get all the users of the account.
Expand All @@ -26,14 +28,14 @@ public function getAll(array $fields = [])

// Make sure we are getting some users
$response_data = json_decode($response->getBody()->getContents());
if (!$response_data->users) {
if (!isset($response_data->users)) {
throw new \Exception('Unable to get the users.');
}

// Cleanup the data into objects
$userCollection = new Collection();
foreach ($response_data->users as $userData) {
$user = new Tag($userData);
$user = new User($userData);
$userCollection->add($user);
}

Expand All @@ -50,11 +52,11 @@ public function getById($userId)

// Make sure we are getting some users
$response_data = json_decode($response->getBody()->getContents());
if (!$response_data->users) {
if (!isset($response_data->users)) {
throw new \Exception('Unable to get the users.');
}

$user = new Tag($response_data->users[0]);
$user = new User($response_data->users[0]);

return $user;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Mocks/ResponseBodyDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace GhostIO\Tests\Mocks;

/*
|--------------------------------------------------------------------------
| Dummy Classes
|--------------------------------------------------------------------------
|
| This classes will serve as helpers for the API response so we mock it
| and we dont actually go and send a request to the real server.
|
*/

class ResponseBodyDummy
{
private $contents;

public function getContents()
{
return $this->contents;
}

public function setContents(array $contents)
{
$this->contents = json_encode($contents);
}
}
30 changes: 30 additions & 0 deletions tests/Mocks/ResponseDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace GhostIO\Tests\Mocks;

/*
|--------------------------------------------------------------------------
| Dummy Classes
|--------------------------------------------------------------------------
|
| This classes will serve as helpers for the API response so we mock it
| and we dont actually go and send a request to the real server.
|
*/

class ResponseDummy
{
private $body;

public function getBody()
{
return $this->body;
}

public function setResponseBody(array $body)
{
$responseBody = new ResponseBodyDummy();
$responseBody->setContents($body);
$this->body = $responseBody;
}
}
45 changes: 2 additions & 43 deletions tests/Providers/PostProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PHPUnit_Framework_TestCase;
use GhostIO\Providers\PostProvider;
use GhostIO\Tests\Mocks\ResponseDummy;
use GhostIO\Tests\Mocks\ResponseBodyDummy;

/**
* Corresponding Class to test PostProvider class
Expand Down Expand Up @@ -125,46 +127,3 @@ public function testProviderCanFindAPostById()

}



/*
|--------------------------------------------------------------------------
| Dummy Classes
|--------------------------------------------------------------------------
|
| This classes will serve as helpers for the API response so we mock it
| and we dont actually go and send a request to the real server.
|
*/

class ResponseDummy
{
private $body;

public function getBody()
{
return $this->body;
}

public function setResponseBody(array $body)
{
$responseBody = new ResponseBodyDummy();
$responseBody->setContents($body);
$this->body = $responseBody;
}
}

class ResponseBodyDummy
{
private $contents;

public function getContents()
{
return $this->contents;
}

public function setContents(array $contents)
{
$this->contents = json_encode($contents);
}
}
Loading

0 comments on commit deeee29

Please sign in to comment.