Skip to content

Commit

Permalink
!!! FEATURE: Return rowCount in execute()
Browse files Browse the repository at this point in the history
  • Loading branch information
paxuclus committed Apr 19, 2022
1 parent 2875cd6 commit 9758faa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Upsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function withField(string $column, $value, int $parameterType = Parameter
return $this;
}

public function execute(): void
public function execute(): int
{
if (!$this->table) {
throw new Exception\NoTableGiven('No table name has been set!', 1603199471);
Expand All @@ -98,11 +98,13 @@ public function execute(): void

$sql = $this->buildQuery($columns, $values, $updates);

$this->connection->executeQuery(
$result = $this->connection->executeQuery(
$sql,
array_combine(array_keys($allFields), array_column($allFields, 'value')),
array_combine(array_keys($allFields), array_column($allFields, 'type'))
);

return $result->rowCount();
}

protected function buildQuery(string $columns, string $values, string $updates): string
Expand Down
52 changes: 50 additions & 2 deletions test/UpsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\ForwardCompatibility\DriverResultStatement;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Netlogix\Doctrine\Upsert\Exception;
Expand Down Expand Up @@ -113,7 +114,8 @@ public function Parameters_are_built_correctly(): void
'boo' => 4,
],
self::anything()
);
)
->willReturn($this->getMockResult(0));

Upsert::fromConnection($connection)
->forTable('foo_table')
Expand Down Expand Up @@ -143,7 +145,8 @@ public function ParameterTypes_are_built_correctly(): void
'baz' => ParameterType::BOOLEAN,
'boo' => ParameterType::LARGE_OBJECT,
]
);
)
->willReturn($this->getMockResult(0));

Upsert::fromConnection($connection)
->forTable('foo_table')
Expand All @@ -154,6 +157,39 @@ public function ParameterTypes_are_built_correctly(): void
->execute();
}

/**
* @test
*/
public function RowCount_is_returned(): void
{
$connection = $this->getMockConnection();

$connection
->expects(self::once())
->method('executeQuery')
->with(
self::anything(),
self::anything(),
[
'foo' => ParameterType::INTEGER,
'bar' => ParameterType::STRING,
'baz' => ParameterType::BOOLEAN,
'boo' => ParameterType::LARGE_OBJECT,
]
)
->willReturn($this->getMockResult(35));

$count = Upsert::fromConnection($connection)
->forTable('foo_table')
->withIdentifier('foo', 1, ParameterType::INTEGER)
->withIdentifier('bar', '2', ParameterType::STRING)
->withField('baz', true, ParameterType::BOOLEAN)
->withField('boo', 4, ParameterType::LARGE_OBJECT)
->execute();

self::assertEquals(35, $count);
}

/**
* @test
*/
Expand Down Expand Up @@ -249,4 +285,16 @@ private function getMockConnection()
return $connection;
}

private function getMockResult(int $rowCount): DriverResultStatement
{
$result = $this->getMockBuilder(DriverResultStatement::class)
->getMock();

$result
->method('rowCount')
->willReturn($rowCount);

return $result;
}

}

0 comments on commit 9758faa

Please sign in to comment.