View source
<?php
namespace Drupal\Tests\salesforce\Unit;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\State\State;
use Drupal\salesforce\Entity\SalesforceAuthConfig;
use Drupal\salesforce\SalesforceAuthProviderInterface;
use Drupal\salesforce\SalesforceAuthProviderPluginManager;
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 OAuth\OAuth2\Token\TokenInterface;
use GuzzleHttp\Client;
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 = [
'httpRequest',
];
$this->httpClient = $this
->getMockBuilder(Client::CLASS)
->getMock();
$this->configFactory = $this
->getMockBuilder(ConfigFactory::CLASS)
->disableOriginalConstructor()
->getMock();
$this->state = $this
->getMockBuilder(State::CLASS)
->disableOriginalConstructor()
->getMock();
$this->cache = $this
->getMockBuilder(CacheBackendInterface::CLASS)
->getMock();
$this->json = $this
->getMockBuilder(Json::CLASS)
->getMock();
$this->time = $this
->getMockBuilder(TimeInterface::CLASS)
->getMock();
$this->authToken = $this
->getMockBuilder(TokenInterface::CLASS)
->getMock();
$this->authProvider = $this
->getMockBuilder(SalesforceAuthProviderInterface::CLASS)
->disableOriginalConstructor()
->getMock();
$this->authProvider
->expects($this
->any())
->method('getApiEndpoint')
->willReturn('https://example.com');
$this->authConfig = $this
->getMockBuilder(SalesforceAuthConfig::CLASS)
->disableOriginalConstructor()
->getMock();
$this->authMan = $this
->getMockBuilder(SalesforceAuthProviderPluginManager::CLASS)
->disableOriginalConstructor()
->getMock();
$this->authMan
->expects($this
->any())
->method('getToken')
->willReturn($this->authToken);
$this->authMan
->expects($this
->any())
->method('getProvider')
->willReturn($this->authProvider);
$this->authMan
->expects($this
->any())
->method('getConfig')
->willReturn($this->authConfig);
$this->authMan
->expects($this
->any())
->method('refreshToken')
->willReturn($this->authToken);
}
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->authMan,
];
$this->client = $this
->getMockBuilder(RestClient::CLASS)
->setMethods($methods)
->setConstructorArgs($args)
->getMock();
}
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
->expectException(\Exception::class);
$this->client
->apiCall('');
}
public function testReauthApiCall() {
$this
->initClient();
$response_401 = new GuzzleResponse(401);
$response_200 = new GuzzleResponse(200);
$this->client
->expects($this
->at(0))
->method('httpRequest')
->willReturn($response_401);
$this->client
->expects($this
->at(1))
->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
->expectException(\Exception::class);
$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
->expectException(RequestException::class);
$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
->assertFalse($this->client
->getRecordTypes('fail'));
}
}