Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
salesforce-3257058
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
salesforce-3257058
Commits
1becd5bf
Commit
1becd5bf
authored
8 years ago
by
Aaron Bauman
Browse files
Options
Downloads
Patches
Plain Diff
RestClientTest - basic setup and stubs
parent
283b74c0
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Rest/RestClient.php
+3
-2
3 additions, 2 deletions
src/Rest/RestClient.php
tests/src/Unit/RestClientTest.php
+241
-0
241 additions, 0 deletions
tests/src/Unit/RestClientTest.php
with
244 additions
and
2 deletions
src/Rest/RestClient.php
+
3
−
2
View file @
1becd5bf
...
...
@@ -95,7 +95,9 @@ class RestClient {
if
(
$this
->
response
->
getStatusCode
()
!=
401
)
{
throw
$e
;
}
}
if
(
$this
->
response
->
getStatusCode
()
==
401
)
{
// The session ID or OAuth token used has expired or is invalid: refresh
// token. If refreshToken() throws an exception, or if apiHttpRequest()
// throws anything but a RequestException, let it bubble up.
...
...
@@ -170,8 +172,7 @@ class RestClient {
*/
protected
function
httpRequest
(
$url
,
$data
=
NULL
,
array
$headers
=
[],
$method
=
'GET'
)
{
// Build the request, including path and headers. Internal use.
$response
=
$this
->
httpClient
->
$method
(
$url
,
[
'headers'
=>
$headers
,
'body'
=>
$data
]);
return
$response
;
return
$this
->
httpClient
->
$method
(
$url
,
[
'headers'
=>
$headers
,
'body'
=>
$data
]);
}
/**
...
...
This diff is collapsed.
Click to expand it.
tests/src/Unit/RestClientTest.php
0 → 100644
+
241
−
0
View file @
1becd5bf
<?php
namespace
Drupal\Tests\salesforce\Unit
;
use
Drupal\Tests\UnitTestCase
;
use
Drupal\salesforce\SFID
;
use
Drupal\salesforce\Exception
;
use
Drupal\salesforce\Rest\RestClient
;
use
GuzzleHttp\Psr7\Response
as
GuzzleResponse
;
use
Drupal\salesforce\Rest\RestResponse
as
RestResponse
;
/**
* @coversDefaultClass \Drupal\salesforce\Rest\RestClient
* @group salesforce
*/
class
RestClientTest
extends
UnitTestCase
{
static
$modules
=
[
'salesforce'
];
public
function
setUp
()
{
parent
::
setUp
();
$methods
=
[
'getConsumerKey'
,
'getConsumerSecret'
,
'getRefreshToken'
,
'getAccessToken'
,
'refreshToken'
,
'getApiEndPoint'
,
'httpRequest'
,
];
$this
->
httpClient
=
$this
->
getMock
(
'\GuzzleHttp\Client'
);
$this
->
configFactory
=
$this
->
getMockBuilder
(
'\Drupal\Core\Config\ConfigFactory'
)
->
disableOriginalConstructor
()
->
getMock
();
$this
->
state
=
$this
->
getMockBuilder
(
'\Drupal\Core\State\State'
)
->
disableOriginalConstructor
()
->
getMock
();
$this
->
cache
=
$this
->
getMock
(
'\Drupal\Core\Cache\CacheBackendInterface'
);
$args
=
[
$this
->
httpClient
,
$this
->
configFactory
,
$this
->
state
,
$this
->
cache
];
$this
->
client
=
$this
->
getMock
(
RestClient
::
CLASS
,
$methods
,
$args
);
$this
->
client
->
expects
(
$this
->
any
())
->
method
(
'getApiEndPoint'
)
->
willReturn
(
'https://example.com'
);
}
/**
* @covers ::isAuthorized
*/
public
function
testAuthorized
()
{
$this
->
client
->
expects
(
$this
->
at
(
0
))
->
method
(
'getConsumerKey'
)
->
willReturn
(
$this
->
randomMachineName
());
$this
->
client
->
expects
(
$this
->
at
(
1
))
->
method
(
'getConsumerSecret'
)
->
willReturn
(
$this
->
randomMachineName
());
$this
->
client
->
expects
(
$this
->
at
(
2
))
->
method
(
'getRefreshToken'
)
->
willReturn
(
$this
->
randomMachineName
());
$this
->
assertTrue
(
$this
->
client
->
isAuthorized
());
// Next one will fail because mocks only return for specific invocations.
$this
->
assertFalse
(
$this
->
client
->
isAuthorized
());
}
/**
* @covers ::apiCall
*/
public
function
testSimpleApiCall
()
{
// Test that an apiCall returns a json-decoded value.
$body
=
array
(
'foo'
=>
'bar'
);
$response
=
new
GuzzleResponse
(
200
,
[],
json_encode
(
$body
));
$this
->
client
->
expects
(
$this
->
any
())
->
method
(
'getAccessToken'
)
->
willReturn
(
TRUE
);
$this
->
client
->
expects
(
$this
->
any
())
->
method
(
'httpRequest'
)
->
willReturn
(
$response
);
$result
=
$this
->
client
->
apiCall
(
''
);
$this
->
assertEquals
(
$result
,
$body
);
}
/**
* @covers ::apiCall
* @expectedException Exception
*/
public
function
testExceptionApiCall
()
{
// Test that SF client throws an exception for non-200 response
$response
=
new
GuzzleResponse
(
456
);
$this
->
client
->
expects
(
$this
->
any
())
->
method
(
'getAccessToken'
)
->
willReturn
(
TRUE
);
$this
->
client
->
expects
(
$this
->
any
())
->
method
(
'httpRequest'
)
->
willReturn
(
$response
);
$result
=
$this
->
client
->
apiCall
(
''
);
}
/**
* @covers ::apiCall
*/
public
function
testReauthApiCall
()
{
// Test that apiCall does auto-re-auth after 401 response
$response_401
=
new
GuzzleResponse
(
401
);
$response_200
=
new
GuzzleResponse
(
200
);
$this
->
client
->
expects
(
$this
->
any
())
->
method
(
'getAccessToken'
)
->
willReturn
(
TRUE
);
// First httpRequest() is position 4.
// @TODO this is extremely brittle, exposes complexity in underlying client. Refactor this.
$this
->
client
->
expects
(
$this
->
at
(
3
))
->
method
(
'httpRequest'
)
->
willReturn
(
$response_401
);
$this
->
client
->
expects
(
$this
->
at
(
4
))
->
method
(
'httpRequest'
)
->
willReturn
(
$response_200
);
$result
=
$this
->
client
->
apiCall
(
''
);
}
/**
* @covers ::objects
*/
public
function
testObjects
()
{
}
/**
* @covers ::query
*/
public
function
testQuery
()
{
}
/**
* @covers ::objectDescribe
*/
public
function
testObjectDescribe
()
{
}
/**
* @covers ::objectCreate
*/
public
function
testObjectCreate
()
{
}
/**
* @covers ::objectUpsert
*/
public
function
testObjectUpsert
()
{
}
/**
* @covers ::objectUpdate
*/
public
function
testObjectUpdate
()
{
}
/**
* @covers ::objectRead
*/
public
function
testObjectRead
()
{
}
/**
* @covers ::objectReadbyExternalId
*
* @return void
* @author Aaron Bauman
*/
public
function
testObjectReadbyExternalId
()
{
}
/**
* @covers ::objectDelete
*/
public
function
testObjectDelete
()
{
}
/**
* @covers ::getDeleted
*/
public
function
getDeleted
()
{
}
/**
* @covers ::listResources
*/
public
function
testListResources
()
{
}
/**
* @covers ::getUpdated
*/
public
function
testGetUpdated
()
{
}
/**
* @covers ::getRecordTypes
*/
public
function
testGetRecordTypes
()
{
}
/**
* @covers ::getRecordTypeIdByDeveloperName
*/
public
function
testGetRecordTypeIdByDeveloperName
()
{
}
/**
* @covers ::getObjectTypeName
*/
public
static
function
testGetObjectTypeName
()
{
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment