View source
<?php
namespace Drupal\Tests\salesforce\Unit;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\UnitTestCase;
use Drupal\salesforce\Rest\RestClient;
use Drupal\salesforce\Rest\RestResponse;
use Drupal\salesforce\Rest\RestResponseDescribe;
use Drupal\salesforce\Rest\RestResponseResources;
use Drupal\salesforce\SFID;
use Drupal\salesforce\SObject;
use Drupal\salesforce\SelectQueryResult;
use Drupal\salesforce\SelectQuery;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use GuzzleHttp\Exception\RequestException;
use Drupal\Component\Datetime\TimeInterface;
class RestClientTest extends UnitTestCase {
protected static $modules = [
'salesforce',
];
public function setUp() {
parent::setUp();
$this->salesforce_id = '1234567890abcde';
$this->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');
$this->json = $this
->getMock(Json::CLASS);
$this->time = $this
->getMock(TimeInterface::CLASS);
}
private function initClient($methods = NULL) {
if (empty($methods)) {
$methods = $this->methods;
}
$args = [
$this->httpClient,
$this->configFactory,
$this->state,
$this->cache,
$this->json,
$this->time,
];
$this->client = $this
->getMock(RestClient::CLASS, $methods, $args);
if (in_array('getApiEndPoint', $methods)) {
$this->client
->expects($this
->any())
->method('getApiEndPoint')
->willReturn('https://example.com');
}
if (in_array('getAccessToken', $methods)) {
$this->client
->expects($this
->any())
->method('getAccessToken')
->willReturn(TRUE);
}
}
public function testAuthorized() {
$this
->initClient();
$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());
$this
->assertFalse($this->client
->isAuthorized());
}
public function testSimpleApiCall() {
$this
->initClient();
$body = [
'foo' => 'bar',
];
$response = new GuzzleResponse(200, [], json_encode($body));
$this->client
->expects($this
->any())
->method('httpRequest')
->willReturn($response);
$result = $this->client
->apiCall('');
$this
->assertEquals($result, $body);
}
public function testExceptionApiCall() {
$this
->initClient();
$response = new GuzzleResponse(456);
$this->client
->expects($this
->any())
->method('httpRequest')
->willReturn($response);
$this->client
->apiCall('');
}
public function testReauthApiCall() {
$this
->initClient();
$response_401 = new GuzzleResponse(401);
$response_200 = new GuzzleResponse(200);
$this->client
->expects($this
->at(3))
->method('httpRequest')
->willReturn($response_401);
$this->client
->expects($this
->at(4))
->method('httpRequest')
->willReturn($response_200);
$this->client
->apiCall('');
}
public function testObjects() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$objects = [
'sobjects' => [
'Test' => [
'name' => 'Test',
'updateable' => TRUE,
],
'NonUpdateable' => [
'name' => 'NonUpdateable',
'updateable' => FALSE,
],
],
];
$cache = (object) [
'created' => time(),
'data' => $objects,
];
unset($cache->data['sobjects']['NonUpdateable']);
$this->cache
->expects($this
->at(0))
->method('get')
->willReturn($cache);
$this->cache
->expects($this
->at(1))
->method('get')
->willReturn(FALSE);
$this->client
->expects($this
->once())
->method('apiCall')
->willReturn($objects);
$this
->assertEquals($cache->data['sobjects'], $this->client
->objects());
$this
->assertEquals($cache->data['sobjects'], $this->client
->objects());
}
public function testQuery() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$rawQueryResult = [
'totalSize' => 1,
'done' => TRUE,
'records' => [
0 => [
'attributes' => [
'type' => 'Foo',
'url' => 'Bar',
],
'Id' => $this->salesforce_id,
],
],
];
$this->client
->expects($this
->once())
->method('apiCall')
->willReturn($rawQueryResult);
$this
->assertEquals(new SelectQueryResult($rawQueryResult), $this->client
->query(new SelectQuery("")));
}
public function testObjectDescribe() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$name = $this
->randomMachineName();
$restResponse = new RestResponse(new GuzzleResponse('200', [], json_encode([
'name' => $name,
'fields' => [
[
'name' => $this
->randomMachineName(),
'label' => 'Foo Bar',
$this
->randomMachineName() => $this
->randomMachineName(),
$this
->randomMachineName() => [
$this
->randomMachineName() => $this
->randomMachineName(),
$this
->randomMachineName() => $this
->randomMachineName(),
],
],
[
'name' => $this
->randomMachineName(),
],
],
])));
$this->client
->expects($this
->once())
->method('apiCall')
->willReturn($restResponse);
$result = $this->client
->objectDescribe($name);
$expected = new RestResponseDescribe($restResponse);
$this
->assertEquals($expected, $result);
$this->cache
->expects($this
->any())
->method('get')
->willReturn((object) [
'data' => $expected,
'created' => time(),
]);
$this
->assertEquals($expected, $this->client
->objectDescribe($name));
$this->client
->objectDescribe('');
}
public function testObjectCreate() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$restResponse = new RestResponse(new GuzzleResponse('200', [], json_encode([
'id' => $this->salesforce_id,
])));
$sfid = new SFID($this->salesforce_id);
$this->client
->expects($this
->once())
->method('apiCall')
->willReturn($restResponse);
$this
->assertEquals($sfid, $this->client
->objectCreate('', []));
}
public function testObjectUpsert() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
'objectReadbyExternalId',
]));
$createResponse = new RestResponse(new GuzzleResponse('200', [], json_encode([
'id' => $this->salesforce_id,
])));
$updateResponse = new RestResponse(new GuzzleResponse('204', [], ''));
$sfid = new SFID($this->salesforce_id);
$sobject = new SObject([
'id' => $this->salesforce_id,
'attributes' => [
'type' => 'dummy',
],
]);
$this->client
->expects($this
->at(0))
->method('apiCall')
->willReturn($createResponse);
$this->client
->expects($this
->at(1))
->method('apiCall')
->willReturn($updateResponse);
$this->client
->expects($this
->once())
->method('objectReadbyExternalId')
->willReturn($sobject);
$this
->assertEquals($sfid, $this->client
->objectUpsert('', '', '', []));
$this
->assertEquals($sfid, $this->client
->objectUpsert('', '', '', []));
}
public function testObjectUpdate() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$this->client
->expects($this
->once())
->method('apiCall')
->willReturn(NULL);
$this
->assertNull($this->client
->objectUpdate('', '', []));
}
public function testObjectRead() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$rawData = [
'id' => $this->salesforce_id,
'attributes' => [
'type' => 'dummy',
],
];
$this->client
->expects($this
->once())
->method('apiCall')
->willReturn($rawData);
$this
->assertEquals(new SObject($rawData), $this->client
->objectRead('', ''));
}
public function testObjectReadbyExternalId() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$rawData = [
'id' => $this->salesforce_id,
'attributes' => [
'type' => 'dummy',
],
];
$this->client
->expects($this
->once())
->method('apiCall')
->willReturn($rawData);
$this
->assertEquals(new SObject($rawData), $this->client
->objectReadByExternalId('', '', ''));
}
public function testObjectDelete() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$this->client
->expects($this
->exactly(3))
->method('apiCall');
$this->client
->expects($this
->at(0))
->method('apiCall')
->willReturn(NULL);
$exception404 = new RequestException('', new GuzzleRequest('GET', ''), new GuzzleResponse(404, [], ''));
$this->client
->expects($this
->at(1))
->method('apiCall')
->will($this
->throwException($exception404));
$exceptionOther = new RequestException('', new GuzzleRequest('GET', ''), new GuzzleResponse(456, [], ''));
$this->client
->expects($this
->at(2))
->method('apiCall')
->will($this
->throwException($exceptionOther));
$this
->assertNull($this->client
->objectDelete('', ''));
$this
->assertNull($this->client
->objectDelete('', ''));
$this->client
->objectDelete('', '');
}
public function testListResources() {
$this
->initClient(array_merge($this->methods, [
'apiCall',
]));
$restResponse = new RestResponse(new GuzzleResponse('204', [], json_encode([
'foo' => 'bar',
'zee' => 'bang',
])));
$this->client
->expects($this
->once())
->method('apiCall')
->willReturn($restResponse);
$this
->assertEquals(new RestResponseResources($restResponse), $this->client
->listResources());
}
public function testGetRecordTypes() {
$this
->initClient(array_merge($this->methods, [
'query',
]));
$sObjectType = $this
->randomMachineName();
$developerName = $this
->randomMachineName();
$rawQueryResult = [
'totalSize' => 1,
'done' => TRUE,
'records' => [
0 => [
'attributes' => [
'type' => 'Foo',
'url' => 'Bar',
],
'SobjectType' => $sObjectType,
'DeveloperName' => $developerName,
'Id' => $this->salesforce_id,
],
],
];
$recordTypes = [
$sObjectType => [
$developerName => new SObject($rawQueryResult['records'][0]),
],
];
$cache = (object) [
'created' => time(),
'data' => $recordTypes,
];
$this->cache
->expects($this
->at(1))
->method('get')
->willReturn(FALSE);
$this->cache
->expects($this
->at(2))
->method('get')
->willReturn($cache);
$this->cache
->expects($this
->at(3))
->method('get')
->willReturn($cache);
$this->client
->expects($this
->once())
->method('query')
->willReturn(new SelectQueryResult($rawQueryResult));
$this
->assertEquals($recordTypes, $this->client
->getRecordTypes());
$this
->assertEquals($recordTypes[$sObjectType], $this->client
->getRecordTypes($sObjectType));
$this->client
->getRecordTypes('fail');
}
}