Skip to content

Commit

Permalink
Merge pull request #95 from codelicia/infection
Browse files Browse the repository at this point in the history
fix(infection): errors
  • Loading branch information
malukenho authored Sep 23, 2023
2 parents 126d762 + 4777cc8 commit 0622e54
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
vendor/
composer.lock
cov/
.phpcs-cache
.phpunit.result.cache
.phpunit.cache/
2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"text": "infection.diff",
"perMutator": "php://stderr"
},
"minCoveredMsi": 50,
"minCoveredMsi": 55,
"minMsi": 7,
"mutators": { "@default": true },
"source": { "directories": [ "src" ] },
Expand Down
7 changes: 1 addition & 6 deletions src/Soql/BoundValuesSeparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Codelicia\Soql;

use Doctrine\DBAL\ParameterType;

use function implode;
use function is_array;
use function is_object;
Expand All @@ -20,15 +18,12 @@ private function __construct()
}

/** @return list<string> */
public static function separateBoundValues(array $boundValues, array $types): array
public static function separateBoundValues(array $boundValues): array
{
$values = [];

foreach ($boundValues as $parameter => $value) {
$parameter = sprintf(':%s', $parameter);
if (! isset($types[$parameter])) {
$types[$parameter] = ParameterType::STRING;
}

if (is_object($value) && method_exists($value, '__toString')) {
$value = (string) $value;
Expand Down
2 changes: 0 additions & 2 deletions src/Soql/ConnectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ public function update(
array $refs = [],
array $headers = [],
): int {
invariant(array_key_exists('Id', $identifier), 'No Identifier was detected.');

$param = $identifier['Id'] ?? (key($identifier) . '/' . $identifier[key($identifier)]);

$request = new Request(
Expand Down
1 change: 0 additions & 1 deletion src/Soql/SoqlStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function execute($params = null): \Doctrine\DBAL\Driver\Result
if ($this->boundValues !== []) {
$values = BoundValuesSeparator::separateBoundValues(
$this->boundValues,
$this->types,
);

$this->statement = str_replace(array_keys($values), $values, $this->statement);
Expand Down
65 changes: 65 additions & 0 deletions tests/Soql/BoundValuesSeparatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace CodeliciaTest\Soql;

use Codelicia\Soql\BoundValuesSeparator;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class BoundValuesSeparatorTest extends TestCase
{
#[Test]
public function separate_bound_values(): void
{
$boundValues = [
'name' => 'John',
'age' => 30,
'hobbies' => ['reading', 'swimming'],
'description' => null,
];

$expected = [
':name' => "'John'",
':age' => 30,
':hobbies' => "'reading', 'swimming'",
':description' => null,
];

$result = BoundValuesSeparator::separateBoundValues($boundValues);

self::assertSame($expected, $result);
}

#[Test]
public function with_objects(): void
{
$boundValues = [
'name' => new class () {
public function __toString(): string
{
return 'John';
}
},
];

$expected = [':name' => "'John'"];

$result = BoundValuesSeparator::separateBoundValues($boundValues);

self::assertSame($expected, $result);
}

#[Test]
public function with_missing_type(): void
{
$boundValues = ['name' => 'John'];

$expected = [':name' => "'John'"];

$result = BoundValuesSeparator::separateBoundValues($boundValues);

self::assertSame($expected, $result);
}
}
107 changes: 100 additions & 7 deletions tests/Soql/ConnectionWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,78 @@ public function insert_with_no_transaction(): void
$this->stream->expects(self::once())->method('getContents')
->willReturn(file_get_contents(__DIR__ . '/../fixtures/generic_success.json'));

$this->connection->insert(
$result = $this->connection->insert(
'User',
['Name' => 'Pay as you go Opportunity'],
['Id' => 123],
['X-Unit-Testing' => 'Yes'],
);

self::assertSame(1, $result);
}

#[Test]
public function update_with_no_transaction(): void
{
$this->client->expects(self::once())->method('send')
->with(self::assertHttpHeaderIsPropagated())
->with(self::callback(static function (Request $req): bool {
self::assertSame(['X-Unit-Testing' => ['Yes']], $req->getHeaders());
self::assertSame('PATCH', $req->getMethod());
self::assertSame(
'/services/data/api-version-456/sobjects/User/123',
$req->getUri()->getPath(),
);

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

$this->response->expects(self::once())->method('getBody')->willReturn($this->stream);
$this->response->expects(self::once())->method('getStatusCode')->willReturn(204);

$this->stream->expects(self::once())->method('rewind');

$this->connection->update(
$result = $this->connection->update(
'User',
['Name' => 'Pay as you go Opportunity'],
['Id' => 123],
[],
['X-Unit-Testing' => 'Yes'],
);

self::assertSame(1, $result);
}

#[Test]
public function update_with_no_transaction_and_external_id(): void
{
$this->client->expects(self::once())->method('send')
->with(self::callback(static function (Request $req): bool {
self::assertSame(['X-Unit-Testing' => ['Yes']], $req->getHeaders());
self::assertSame('PATCH', $req->getMethod());
self::assertSame(
'/services/data/api-version-456/sobjects/User/External__Id/123',
$req->getUri()->getPath(),
);

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

$this->response->expects(self::once())->method('getBody')->willReturn($this->stream);
$this->response->expects(self::once())->method('getStatusCode')->willReturn(204);

$this->stream->expects(self::once())->method('rewind');

$result = $this->connection->update(
'User',
['Name' => 'Pay as you go Opportunity'],
['External__Id' => 123],
[],
['X-Unit-Testing' => 'Yes'],
);

self::assertSame(1, $result);
}

#[Test]
Expand All @@ -128,19 +173,30 @@ public function stopQuery()
$this->stream->expects(self::once())->method('getContents')->willReturn('');
$this->stream->expects(self::once())->method('rewind');

$this->connection->delete(
$result = $this->connection->delete(
'User',
['Id' => 123],
['ref' => '1234'],
['X-Unit-Testing' => 'Yes'],
);

self::assertSame(1, $result);
}

#[Test]
public function delete_with_no_transaction(): void
{
$this->client->expects(self::once())->method('send')
->with(self::assertHttpHeaderIsPropagated())
->with(self::callback(static function (Request $req): bool {
self::assertSame(['X-Unit-Testing' => ['Yes']], $req->getHeaders());
self::assertSame('DELETE', $req->getMethod());
self::assertSame(
'/services/data/api-version-456/sobjects/User/123',
$req->getUri()->getPath(),
);

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

$this->response->expects(self::once())->method('getBody')->willReturn($this->stream);
Expand All @@ -155,6 +211,34 @@ public function delete_with_no_transaction(): void
);
}

#[Test]
public function delete_with_no_transaction_and_external_id(): void
{
$this->client->expects(self::once())->method('send')
->with(self::callback(static function (Request $req): bool {
self::assertSame(['X-Unit-Testing' => ['Yes']], $req->getHeaders());
self::assertSame('DELETE', $req->getMethod());
self::assertSame(
'/services/data/api-version-456/sobjects/User/External__Id/123',
$req->getUri()->getPath(),
);

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

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

$this->stream->expects(self::once())->method('rewind');

$this->connection->delete(
'User',
['External__Id' => 123],
['ref' => '1234'],
['X-Unit-Testing' => 'Yes'],
);
}

#[Test]
public function transactional_state(): void
{
Expand Down Expand Up @@ -199,10 +283,19 @@ public function transactional_should_throws_exception_when_error_occurs(): void
}

#[Test]
public function upsert_with_no_transaction(): void
public function upsert_with_no_transaction_and_external_id(): void
{
$this->client->expects(self::once())->method('send')
->with(self::assertHttpHeaderIsPropagated())
->with(self::callback(static function (Request $req): bool {
self::assertSame(['X-Unit-Testing' => ['Yes']], $req->getHeaders());
self::assertSame('PATCH', $req->getMethod());
self::assertSame(
'/services/data/api-version-456/sobjects/User/ExternalId__c/12345',
$req->getUri()->getPath(),
);

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

$this->response->expects(self::once())->method('getBody')->willReturn($this->stream);
Expand Down
6 changes: 3 additions & 3 deletions tests/Soql/Factory/HttpAccessTokenFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
final class HttpAccessTokenFactoryTest extends TestCase
{
#[Test]
public function it_should_reuse_access_token_after_authentication() : void
public function it_should_reuse_access_token_after_authentication(): void
{
$factory = new HttpAccessTokenFactory(
'salesforceInstance',
'apiVersion',
'consumerKey',
'consumerSecret',
'username',
'password'
'password',
);

$client = $this->createMock(ClientInterface::class);
$client->expects(self::once())->method('request')->willReturn(
new Response(200, [], '{"access_token": "s3Gr3D0"}')
new Response(200, [], '{"access_token": "s3Gr3D0"}'),
);

$factory->__invoke($client);
Expand Down
Loading

0 comments on commit 0622e54

Please sign in to comment.