Skip to content

Commit

Permalink
Mock SifterCurl
Browse files Browse the repository at this point in the history
  • Loading branch information
jdoyle65 committed Feb 25, 2016
1 parent 533b02b commit 84f6723
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 108 deletions.
59 changes: 14 additions & 45 deletions src/Sifter/Sifter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,35 @@

class Sifter
{
private $apiBaseUrl;
private $curl;
static protected $curl = null;

private $PROJECTS_URL = 'projects/';
const PROJECTS_URL = 'projects';


public function __construct($apiKey, $sifterSubdomain)
public function __construct(SifterCurl $curl = null)
{
$this->apiKey = $apiKey;
$this->subdomain = $sifterSubdomain;
$this->apiBaseUrl = 'https://' . $sifterSubdomain . '.sifterapp.com/api/';
$sifterCurl = SifterCurl::instance();
$sifterCurl->setApiInformation($apiKey, $sifterSubdomain, $this->apiBaseUrl);
$this->curl = $sifterCurl->getCurl();
self::$curl = $curl;
}

private function allProjectsUrl()
{
return $this->apiBaseUrl . $this->PROJECTS_URL;
public static function curl() {
return self::$curl;
}

private function projectUrl($projectId)
{
return $this->apiBaseUrl . $this->PROJECTS_URL . $projectId;
}

/**
* @return string
*/
public function getApiBaseUrl()
{
return $this->apiBaseUrl;
}

/**
* @return array
*/
public function getHeaders()
{
return $this->apiHeaders;
}

public function getHeader($header)
{
if (isset($this->apiHeaders[$header])) {
return $this->apiHeaders[$header];
} else {
return null;
}
return Sifter::curl()->getBaseUrl().self::PROJECTS_URL . $projectId;
}

public function allProjects()
public function allProjects($all = false)
{
$url = $this->allProjectsUrl();
$this->curl->get($url);
$url = Sifter::curl()->getBaseUrl().self::PROJECTS_URL;
if($all) { $url.'?all=true'; }
Sifter::curl()->get(Sifter::curl()->getBaseUrl().self::PROJECTS_URL);

if ($this->curl->error) {
throw new \Exception('cURL GET failed with code ' . $this->curl->error_code);
if (Sifter::curl()->error) {
throw new \Exception('cURL GET failed with code ' . Sifter::curl()->error_code);
} else {
$projects = $this->jsonToProjects(json_decode($this->curl->response));
$projects = $this->jsonToProjects(json_decode(Sifter::curl()->response));
return $projects;
}
}
Expand Down
48 changes: 9 additions & 39 deletions src/Sifter/SifterCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,19 @@

use Curl\Curl;

class SifterCurl {
private static $instance;
private $apiKey;
private $apiSubdomain;
private $apiBaseUrl;
private $curl;
class SifterCurl extends Curl {

public static function instance()
{
if (static::$instance === null) {
static::$instance = new static();
}

return static::$instance;
}

public function setApiInformation($apiKey, $apiSubdomain, $apiBaseUrl) {
$this->apiKey = $apiKey;
$this->apiSubdomain = $apiSubdomain;
$this->apiBaseUrl = $apiBaseUrl;
$this->curl = new Curl();
$this->curl->setHeader('X-Sifter-Token', $apiKey);
$this->curl->setHeader('Accept', 'application/json');
}

public function getCurl() {
return $this->curl;
}

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


protected function __construct()
{
}
private $baseUrl = '';

private function __clone()
public function __construct($apiKey, $apiSubdomain)
{
parent::__construct();
$this->baseUrl = 'https://' . $apiSubdomain . '.sifterapp.com/api/';
$this->setHeader('X-Sifter-Token', $apiKey);
$this->setHeader('Accept', 'application/json');
}

private function __wakeup()
{
public function getBaseUrl() {
return $this->baseUrl;
}
}
16 changes: 8 additions & 8 deletions src/Sifter/model/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Issue {
private $description;
private $milestoneName;
private $openerName;
private $openerNmail;
private $openeremail;
private $assigneeName;
private $assigneeNmail;
private $status;
Expand Down Expand Up @@ -44,7 +44,7 @@ class Issue {
* @param $url
* @param $apiUrl
*/
public function __construct($number, $categoryName, $priority, $subject, $description, $milestoneName, $openerName, $openerNmail, $assigneeName, $assigneeNmail, $status, $commentCount, $attachmentCount, $createdAt, $updatedAt, $url, $apiUrl)
public function __construct($number, $categoryName, $priority, $subject, $description, $milestoneName, $openerName, $openerEmail, $assigneeName, $assigneeEmail, $status, $commentCount, $attachmentCount, $createdAt, $updatedAt, $url, $apiUrl)
{
$this->number = $number;
$this->categoryName = $categoryName;
Expand All @@ -53,9 +53,9 @@ public function __construct($number, $categoryName, $priority, $subject, $descri
$this->description = $description;
$this->milestoneName = $milestoneName;
$this->openerName = $openerName;
$this->openerNmail = $openerNmail;
$this->openerEmail = $openerEmail;
$this->assigneeName = $assigneeName;
$this->assigneeNmail = $assigneeNmail;
$this->assigneeEmail = $assigneeEmail;
$this->status = $status;
$this->commentCount = $commentCount;
$this->attachmentCount = $attachmentCount;
Expand Down Expand Up @@ -124,9 +124,9 @@ public function getOpenerName()
/**
* @return mixed
*/
public function getOpenerNmail()
public function getOpenerEmail()
{
return $this->openerNmail;
return $this->openerEmail;
}

/**
Expand All @@ -140,9 +140,9 @@ public function getAssigneeName()
/**
* @return mixed
*/
public function getAssigneeNmail()
public function getAssigneeEmail()
{
return $this->assigneeNmail;
return $this->assigneeEmail;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Sifter/model/Project.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Sifter\Model;

use Sifter\Resource\IssuesResource;
use Sifter\Sifter;
use Sifter\SifterCurl;

class Project {
Expand Down Expand Up @@ -97,7 +98,7 @@ public function getMilestonesUrl()
}

public function issues() {
$curl = SifterCurl::instance()->getCurl();
$curl = Sifter::curl();
$curl->get($this->apiIssuesUrl);

if($curl->error) {
Expand Down
2 changes: 1 addition & 1 deletion src/Sifter/resource/IssuesResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static function issuesResourceFromJson($json) {
* @throws \Exception
*/
private function changePage($pageUrl) {
$curl = SifterCurl::instance()->getCurl();
$curl = Sifter::curl();
$curl->get($pageUrl);

if($curl->error) {
Expand Down
106 changes: 92 additions & 14 deletions tests/Sifter/SifterTest.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,135 @@
<?php namespace Tests\Sifter;


use Curl\Curl;
use josegonzalez\Dotenv\Loader;
use Sifter\Sifter;

class SifterTest extends \PHPUnit_Framework_TestCase
{
private static $apiKey;
private static $apiSubdomain;
private static $baseUrl;
private $sifterCurlMock;

public static function setUpBeforeClass()
{
$loader = new Loader(__DIR__.'/../../.env');
$loader->parse()->toEnv();
self::$apiKey = (isset($_ENV['SIFTER_API_KEY'])?$_ENV['SIFTER_API_KEY']:null);
self::$apiSubdomain = (isset($_ENV['SIFTER_API_SUBDOMAIN'])?$_ENV['SIFTER_API_SUBDOMAIN']:null);
self::$apiKey = '1234';
self::$apiSubdomain = 'example';
self::$baseUrl = 'https://example.sifterapp.com/api/';
}

public function testEnvironmentSet() {
public function setUp()
{
parent::setUp();

if ($this->sifterCurlMock === null) {
$this->setUpSifterCurlMock();
}
}

public function testEnvironmentSet()
{
$this->assertNotNull(self::$apiKey);
$this->assertNotNull(self::$apiSubdomain);
}

public function testConstruct() {
$sifter = new Sifter('1234', 'example');

$this->assertEquals('https://example.sifterapp.com/api/', $sifter->getApiBaseUrl());
public function testConstruct()
{
$sifter = new Sifter($this->sifterCurlMock);
}

public function testAllProjects() {
$sifter = new Sifter(self::$apiKey, self::$apiSubdomain);
public function testAllProjects()
{
$sifter = new Sifter($this->sifterCurlMock);
$projects = $sifter->allProjects();

$this->assertTrue(is_array($projects));
}

public function testProject() {
public function testProject()
{

}

public function testIssuesThroughProject() {
$sifter = new Sifter(self::$apiKey, self::$apiSubdomain);
public function testIssuesThroughProject()
{
$sifter = new Sifter($this->sifterCurlMock);
$projects = $sifter->allProjects();

$this->assertTrue(is_array($projects));
$this->assertNotEmpty($projects);

$issues = $projects[0]->issues();

/* @var $issue \Sifter\Model\Issue */
$issue = $issues->first();

$this->assertInstanceOf('Sifter\Resource\IssuesResource', $issues);
$this->assertInstanceOf('Sifter\Model\Issue', $issue);
$this->assertInstanceOf('Carbon\Carbon', $issue->getCreatedAt());
$this->assertInstanceOf('Carbon\Carbon', $issue->getUpdatedAt());
$this->assertEquals(57, $issue->getNumber());
$this->assertEquals('Enhancement', $issue->getCategoryName());
$this->assertEquals('Trivial', $issue->getPriority());
$this->assertEquals('Donec vel neque', $issue->getSubject());
$this->assertEquals('Nam id libero quis ipsum feugiat elementum. Vivamus vitae mauris ut ipsum mattis malesuada. Sed vel massa. Mauris lobortis. Nunc egestas, massa id scelerisque dapibus, urna mi accumsan purus, vel aliquam nunc sapien eget nisi. Quisque vitae nibh. Proin interdum mollis leo. Fusce sed nisi. Integer ut tortor.', $issue->getDescription());
$this->assertNull($issue->getMilestoneName());
$this->assertEquals('Adam Keys', $issue->getOpenerName());
$this->assertEquals('[email protected]', $issue->getOpenerEmail());
$this->assertEquals('Garrett Dimon', $issue->getAssigneeName());
$this->assertEquals('[email protected]', $issue->getAssigneeEmail());
$this->assertEquals(2, $issue->getCommentCount());
$this->assertEquals(1, $issue->getAttachmentCount());
$this->assertEquals('https://example.sifterapp.com/projects/1/issues/5', $issue->getUrl());
}

private function createSifterCallbackClosure() {
return function() {
$args = func_get_args();
$url = $args[0];
switch ($url) {

case self::$baseUrl . 'projects':
$this->sifterCurlMock->response = $this->jsonTestString('projects.json');
$this->sifterCurlMock->error = 0;
break;

case self::$baseUrl . 'projects/1/issues':
$this->sifterCurlMock->response = $this->jsonTestString('projects_1_issues.json');
$this->sifterCurlMock->error = 0;
break;

case self::$baseUrl . 'projects/1';
break;

default:
break;
}
};
}

private function setUpSifterCurlMock()
{
$this->sifterCurlMock = $this->getMockBuilder('Sifter\SifterCurl')
->setConstructorArgs([self::$apiKey, self::$apiSubdomain])
->getMock();
$this->sifterCurlMock
->method('getBaseUrl')
->willReturn(self::$baseUrl);

// Set up the routes that are going to be called
$sifterCurlCallback = $this->createSifterCallbackClosure();

$this->sifterCurlMock
->expects($this->any())
->method('get')
->will($this->returnCallback($sifterCurlCallback));
}

private function jsonTestString($filename)
{
return file_get_contents(__DIR__ . '/../json/' . $filename);
}

}
13 changes: 13 additions & 0 deletions tests/json/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Elephant",
"primary_company_name": "Apple",
"archived": false,
"url": "https://example.sifterapp.com/projects/1",
"issues_url": "https://example.sifterapp.com/projects/1/issues",
"milestones_url": "https://example.sifterapp.com/projects/1/milestones",
"api_url": "https://example.sifterapp.com/api/projects/1",
"api_issues_url": "https://example.sifterapp.com/api/projects/1/issues",
"api_milestones_url": "https://example.sifterapp.com/api/projects/1/milestones",
"api_categories_url": "https://example.sifterapp.com/api/projects/1/categories",
"api_people_url": "https://example.sifterapp.com/api/projects/1/people"
}
Loading

0 comments on commit 84f6723

Please sign in to comment.