Skip to content

Commit

Permalink
Configure api version via parameter
Browse files Browse the repository at this point in the history
Reviewed-by: Alexandre Eher <[email protected]>
Signed-off-by: Jefersson Nathan <[email protected]>
  • Loading branch information
malukenho committed Jul 2, 2021
1 parent 155b1e9 commit 2e29aa9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 25 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ the right `driverClass`. The usual `user` and `password` are also required.
$config = new Configuration();
$connectionParams = [
'salesforceInstance' => 'https://[SALESFORCE INSTANCE].salesforce.com',
'user' => '[email protected]',
'password' => 'salesforce-password',
'consumerKey' => '...',
'consumerSecret' => '...',
'driverClass' => \Codelicia\Soql\SoqlDriver::class,
'wrapperClass' => \Codelicia\Soql\ConnectionWrapper::class,
'apiVersion' => 'v43.0',
'user' => '[email protected]',
'password' => 'salesforce-password',
'consumerKey' => '...',
'consumerSecret' => '...',
'driverClass' => \Codelicia\Soql\SoqlDriver::class,
'wrapperClass' => \Codelicia\Soql\ConnectionWrapper::class,
];

/** @var \Codelicia\Soql\ConnectionWrapper $conn */
Expand All @@ -46,6 +47,7 @@ $conn = DriverManager::getConnection($connectionParams, $config);
instance.
* `password` provides the corresponding password to the email provided on `user`.
* `salesforceInstance` points to the url of the Salesforce instance.
* `apiVersion` specify a salesforce API version to work with.
* `consumerKey` provides the integration consumer key
* `consumerSecret` provides the integration consumer secret
* `driverClass` should points to `\Codelicia\Soql\SoqlDriver::class`
Expand Down
17 changes: 11 additions & 6 deletions src/Soql/ConnectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

class ConnectionWrapper extends Connection
{
private const SERVICE_OBJECT_URL = '/services/data/v43.0/sobjects/%s';
private const SERVICE_OBJECT_ID_URL = '/services/data/v43.0/sobjects/%s/%s';
private const SERVICE_COMPOSITE_URL = '/services/data/v43.0/composite';
private const SERVICE_OBJECT_URL = '/services/data/%s/sobjects/%s';
private const SERVICE_OBJECT_ID_URL = '/services/data/%s/sobjects/%s/%s';
private const SERVICE_COMPOSITE_URL = '/services/data/%s/composite';

private int $transactionalLevel = 0;

Expand Down Expand Up @@ -65,7 +65,7 @@ public function delete($tableExpression, array $identifier, array $types = [], a
$param = $identifier['Id'] ?? (key($identifier) . '/' . $identifier[key($identifier)]);
$this->send(new Request(
'DELETE',
sprintf(self::SERVICE_OBJECT_ID_URL, $tableExpression, $param),
sprintf(self::SERVICE_OBJECT_ID_URL, $this->apiVersion(), $tableExpression, $param),
$headers
));
}
Expand All @@ -79,7 +79,7 @@ public function insert($tableExpression, array $data, array $refs = [], array $h
{
$request = new Request(
'POST',
sprintf(self::SERVICE_OBJECT_URL, $tableExpression),
sprintf(self::SERVICE_OBJECT_URL, $this->apiVersion(), $tableExpression),
$headers,
json_encode($data)
);
Expand Down Expand Up @@ -111,7 +111,7 @@ public function update($tableExpression, array $data, array $identifier = [], ar

$request = new Request(
'PATCH',
sprintf(self::SERVICE_OBJECT_ID_URL, $tableExpression, $param),
sprintf(self::SERVICE_OBJECT_ID_URL, $this->apiVersion(), $tableExpression, $param),
$headers,
json_encode($data)
);
Expand Down Expand Up @@ -261,6 +261,11 @@ private function send(RequestInterface $request): ResponseInterface
return $response;
}

private function apiVersion(): string
{
return $this->getParams()['apiVersion'];
}

private function getHttpClient(): ClientInterface
{
$this->connect();
Expand Down
14 changes: 10 additions & 4 deletions src/Soql/Factory/HttpAccessTokenFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection PhpPropertyOnlyWrittenInspection */

declare(strict_types=1);

namespace Codelicia\Soql\Factory;
Expand All @@ -13,6 +15,8 @@ final class HttpAccessTokenFactory implements AccessTokenFactory
{
private string $salesforceInstance;

private string $apiVersion;

private string $consumerKey;

private string $consumerSecret;
Expand All @@ -25,12 +29,14 @@ final class HttpAccessTokenFactory implements AccessTokenFactory

public function __construct(
string $salesforceInstance,
string $apiVersion,
string $consumerKey,
string $consumerSecret,
string $username,
string $password
) {
$this->salesforceInstance = $salesforceInstance;
$this->apiVersion = $apiVersion;
$this->consumerKey = $consumerKey;
$this->consumerSecret = $consumerSecret;
$this->username = $username;
Expand All @@ -48,11 +54,11 @@ public function __invoke(?ClientInterface $client = null): string

$options = [
'form_params' => [
'grant_type' => 'password',
'client_id' => $this->consumerKey,
'grant_type' => 'password',
'client_id' => $this->consumerKey,
'client_secret' => $this->consumerSecret,
'username' => $this->username,
'password' => $this->password,
'username' => $this->username,
'password' => $this->password,
],
];

Expand Down
6 changes: 3 additions & 3 deletions src/Soql/Factory/HttpAuthorizedClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function __invoke(): ClientInterface
return new Client([
'base_uri' => $this->salesforceInstance,
'headers' => [
'Authorization' => sprintf('Bearer %s', $this->accessTokenFactory->__invoke()),
'X-PrettyPrint' => '1',
'Content-Type' => 'application/json',
'Authorization' => sprintf('Bearer %s', $this->accessTokenFactory->__invoke()),
'X-PrettyPrint' => '1',
'Content-Type' => 'application/json',
'Sforce-Auto-Assign' => 'FALSE',
],
]);
Expand Down
2 changes: 2 additions & 0 deletions src/Soql/SoqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private function getAuthorizedClientFactory(
array $params
): AuthorizedClientFactory {
Assertion::keyExists($params, 'salesforceInstance');
Assertion::keyExists($params, 'apiVersion');
Assertion::keyExists($params, 'consumerKey');
Assertion::keyExists($params, 'consumerSecret');

Expand All @@ -79,6 +80,7 @@ private function getAuthorizedClientFactory(

return new HttpAuthorizedClientFactory(new HttpAccessTokenFactory(
$params['salesforceInstance'],
$params['apiVersion'],
$params['consumerKey'],
$params['consumerSecret'],
$username,
Expand Down
10 changes: 5 additions & 5 deletions src/Soql/SoqlStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class SoqlStatement implements IteratorAggregate, Statement
{
/** @var string[] */
protected static array $paramTypeMap = [
ParameterType::STRING => 's',
ParameterType::BINARY => 's',
ParameterType::BOOLEAN => 'i',
ParameterType::NULL => 's',
ParameterType::INTEGER => 'i',
ParameterType::STRING => 's',
ParameterType::BINARY => 's',
ParameterType::BOOLEAN => 'i',
ParameterType::NULL => 's',
ParameterType::INTEGER => 'i',
ParameterType::LARGE_OBJECT => 'b',
];

Expand Down
27 changes: 26 additions & 1 deletion tests/Soql/ConnectionWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected function setUp(): void

$this->connection = new ConnectionWrapper([
'salesforceInstance' => 'it_is_key_to_have_it',
'apiVersion' => 'api-version-456',
'user' => 'foo',
'password' => 'foo',
'consumerKey' => 'foo',
Expand All @@ -39,6 +40,31 @@ protected function setUp(): void
], new SoqlDriver());
}

/** @test */
public function it_is_using_the_right_api_version(): void
{
$this->client->expects(self::once())->method('send')
->with(self::callback(static function (Request $request) : bool {
self::assertSame('/services/data/api-version-456/sobjects/User', $request->getUri()->getPath());

return true;
}))
->willReturn($this->response);

$this->response->expects(self::exactly(2))->method('getBody')->willReturn($this->stream);
$this->response->expects(self::exactly(2))->method('getBody')->willReturn($this->stream);

$this->stream->expects(self::once())->method('rewind');
$this->stream->expects(self::once())->method('getContents')
->willReturn(file_get_contents(__DIR__ . '/../fixtures/generic_success.json'));

$this->connection->insert(
'User',
['Name' => 'Pay as you go Opportunity'],
['Id' => 123],
);
}

/** @test */
public function insert_with_no_transaction(): void
{
Expand All @@ -47,7 +73,6 @@ public function insert_with_no_transaction(): void
->willReturn($this->response);

$this->response->expects(self::exactly(2))->method('getBody')->willReturn($this->stream);
$this->response->expects(self::exactly(2))->method('getBody')->willReturn($this->stream);

$this->stream->expects(self::once())->method('rewind');
$this->stream->expects(self::once())->method('getContents')
Expand Down
1 change: 1 addition & 0 deletions tests/Soql/Factory/HttpAccessTokenFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function it_should_reuse_access_token_after_authentication() : void
{
$factory = new HttpAccessTokenFactory(
'salesforceInstance',
'apiVersion',
'consumerKey',
'consumerSecret',
'username',
Expand Down

0 comments on commit 2e29aa9

Please sign in to comment.