Skip to content

Commit

Permalink
Merge pull request #28 from MichaelJ2324/2.0
Browse files Browse the repository at this point in the history
Updated Rest-Client Library + Unit Tests
  • Loading branch information
geraldclark committed Aug 15, 2017
2 parents 4645290 + 676a420 commit 789e48f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"require": {
"php": ">=5.3.0",
"michaelj2324/php-rest-client": "~1.0"
"michaelj2324/php-rest-client": ">=1.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0|~5.0"
Expand Down
12 changes: 9 additions & 3 deletions src/Auth/SugarOAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

class SugarOAuthController extends AbstractOAuth2Controller
{
protected static $_OAUTH_HEADER = 'OAuth-Token';

protected static $_TOKEN_VALUE = '%s';
protected static $_AUTH_HEADER = 'OAuth-Token';

protected static $_DEFAULT_GRANT_TYPE = self::OAUTH_RESOURCE_OWNER_GRANT;

Expand All @@ -26,6 +24,14 @@ class SugarOAuthController extends AbstractOAuth2Controller
'platform' => 'api'
);

/**
* @inheritdoc
*/
protected function getAuthHeaderValue()
{
return $this->token['access_token'];
}

/**
* @inheritdoc
* Load Stored Token based on Credentials
Expand Down
60 changes: 48 additions & 12 deletions src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,31 +160,52 @@ public function getModule(){
}

/**
* Auto configure uploads
* @param RequestInterface $Request
* @return RequestInterface
* Configure Uploads on Request
* @inheritdoc
* @codeCoverageIgnore
*/
protected function configureRequest(RequestInterface $Request)
{
$Request = parent::configureRequest($Request);
return $this->configureUploads($Request);
}

/**
* Configure the Uploads Data on the Request Object
* @param RequestInterface $Request
* @return RequestInterface
*/
protected function configureUploads(RequestInterface $Request)
{
$Request->setUpload($this->upload);
if ($Request->getUpload()){
if (!empty($this->_files)){
foreach($this->_files as $key => $properties){
$Request->addFile($key,$properties['path'],$properties['mimeType'],$properties['filename']);
}
}
return $Request;
}

/**
* Add a reset for Upload Settings
* @inheritdoc
* @codeCoverageIgnore
*/
protected function configureResponse(ResponseInterface $Response)
{
$this->resetUploads();
return parent::configureResponse($Response);
}

/**
* Reset the Upload Settings back to defaults
*/
protected function resetUploads(){
if ($this->upload){
//Reset file uploads after uploading
$this->upload = FALSE;
$this->_files = array();
$this->getData()->reset();
}
return parent::configureResponse($Response);
$this->upload = FALSE;
$this->_files = array();
}

/**
Expand Down Expand Up @@ -368,11 +389,11 @@ public function attachFile($fileField,$filePath,$deleteOnFail = false,$mimeType
{
$this->setCurrentAction(self::BEAN_ACTION_ATTACH_FILE,array($fileField));
$this->configureFileUploadData($deleteOnFail);
$this->_files[$fileField] = array(
$this->addFile($fileField,array(
'path' => $filePath,
'mimeType' => $mimeType,
'filename' => $uploadName
);
));
return $this->execute();
}

Expand All @@ -395,11 +416,11 @@ public function tempFile($fileField,$filePath,$deleteOnFail = false,$mimeType =
$this->set($idKey,'temp');
$this->setCurrentAction(self::BEAN_ACTION_TEMP_FILE_UPLOAD,array($fileField));
$this->configureFileUploadData($deleteOnFail);
$this->_files[$fileField] = array(
$this->addFile($fileField,array(
'path' => $filePath,
'mimeType' => $mimeType,
'filename' => $uploadName
);
));
return $this->execute();
}

Expand All @@ -419,4 +440,19 @@ protected function configureFileUploadData($deleteOnFail = FALSE){
$this->setData($data);
}

/**
* Add a file to the internal Files array to be added to the Request
* @param $name
* @param array $properties
* @return $this
*/
protected function addFile($name,array $properties)
{
if (isset($properties['path'])){
$this->upload = TRUE;
$this->_files[$name] = $properties;
}
return $this;
}

}
14 changes: 14 additions & 0 deletions tests/Auth/SugarOAuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Sugarcrm\REST\Tests\Auth;

use MRussell\Http\Request\JSON;
use Sugarcrm\REST\Auth\SugarOAuthController;
use Sugarcrm\REST\Storage\SugarStaticStorage;
use Sugarcrm\REST\Tests\Stubs\Auth\SugarOAuthStub;


/**
Expand Down Expand Up @@ -111,4 +113,16 @@ public function testUpdateCredentials(){
'platform' => array()
),$Auth->getCredentials());
}

/**
* @covers ::getAuthHeaderValue
*/
public function testAuthHeader()
{
$Auth = new SugarOAuthStub();
$Request = new JSON();
$this->assertEquals($Auth,$Auth->configureRequest($Request));
$headers = $Request->getHeaders();
$this->assertEquals('bar',$headers['OAuth-Token']);
}
}
37 changes: 37 additions & 0 deletions tests/Endpoint/AbstractSugarBeanEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,5 +379,42 @@ public function testConfigureFileUploadData(){
'oauth_token' => 'bar'
),$Bean->getData()->asArray());
}

/**
* @covers ::attachFile
* @covers ::tempFile
* @covers ::addFile
* @covers ::resetUploads
* @covers ::configureUploads
*/
public function testFileAttachments()
{
$Bean = new Module();
$Bean->setBaseUrl('http://localhost/rest/v10/');
$Bean->set('id','12345a');
$Bean->setModule('Accounts');
$Reflection = new \ReflectionClass('Sugarcrm\REST\Endpoint\Abstracts\AbstractSugarBeanEndpoint');
$upload = $Reflection->getProperty('upload');
$upload->setAccessible(TRUE);
$_files = $Reflection->getProperty('_files');
$_files->setAccessible(TRUE);
$Auth = new SugarOAuthStub();
$Bean->setAuth($Auth);
$this->assertEquals($Bean,$Bean->attachFile('uploadfile',__FILE__));
$this->assertEquals(Module::BEAN_ACTION_ATTACH_FILE,$Bean->getCurrentAction());
$this->assertEquals(array(),$_files->getValue($Bean));
$this->assertEquals(FALSE,$upload->getValue($Bean));
$this->assertEmpty($Bean->getData()->asArray());
$rBody = $Bean->getRequest()->getBody();
$this->assertNotEmpty($rBody['uploadfile']);
$this->assertEquals($Bean,$Bean->tempFile('uploadfile',__FILE__));
$this->assertEquals(Module::BEAN_ACTION_TEMP_FILE_UPLOAD,$Bean->getCurrentAction());
$this->assertEquals(array(),$_files->getValue($Bean));
$this->assertEquals(FALSE,$upload->getValue($Bean));
$this->assertEmpty($Bean->getData()->asArray());
$this->assertEquals('temp',$Bean['id']);
$rBody = $Bean->getRequest()->getBody();
$this->assertNotEmpty($rBody['uploadfile']);
}
}

0 comments on commit 789e48f

Please sign in to comment.