diff --git a/src/Upsert.php b/src/Upsert.php index b85fb59..694ab20 100644 --- a/src/Upsert.php +++ b/src/Upsert.php @@ -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); @@ -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 diff --git a/test/UpsertTest.php b/test/UpsertTest.php index 6aafe47..48923d0 100644 --- a/test/UpsertTest.php +++ b/test/UpsertTest.php @@ -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; @@ -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') @@ -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') @@ -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 */ @@ -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; + } + }