Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .laminas-ci.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "PhpStan",
"job": {
"php": "8.2",
"php": "8.3",
"dependencies": "latest",
"command": "composer require --dev phpstan/phpstan && vendor/bin/phpstan analyse"
}
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"config": {
"sort-packages": true,
"platform": {
"php": "8.2.99"
"php": "8.3.99"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
Expand All @@ -30,14 +30,14 @@
}
},
"require": {
"php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
"php": "~8.3.0 || ~8.4.0 || ~8.5.0",
"php-db/phpdb": "^0.6.0"
},
"require-dev": {
"ext-pdo_pgsql": "*",
"ext-pgsql": "*",
"laminas/laminas-coding-standard": "^3.0.1",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan": "^2.2",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^11.5.42"
},
Expand Down
65 changes: 37 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,3 @@ parameters:
count: 1
path: src/Container/ConnectionInterfaceFactory.php

-
message: '#^Parameter \#2 \$array of function implode expects array\<string\>, array\<int, array\<int\|string, string\>\> given\.$#'
identifier: argument.type
count: 1
path: src/Metadata/Source.php

-
message: '#^Parameter \#2 \$array of function implode expects array\<string\>, array\<int, list\<string\>\> given\.$#'
identifier: argument.type
count: 1
path: src/Metadata/Source.php
23 changes: 23 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Override;
use PgSql\Result as PgSqlResult;
use PhpDb\Adapter\Driver\ResultInterface;
use PhpDb\Adapter\Exception;
use PhpDb\ResultSet\ResultSet;
use PhpDb\ResultSet\ResultSetInterface;

use function pg_affected_rows;
use function pg_fetch_assoc;
Expand Down Expand Up @@ -94,6 +97,26 @@ public function getGeneratedValue(): int|string|false|null
return $this->generatedValue;
}

/**
* @throws Exception\RuntimeException When this result is not a query result.
*/
#[Override]
public function getQueryResult(?ResultSetInterface $resultPrototype = null): ResultSetInterface
{
if (! $this->isQueryResult()) {
throw new Exception\RuntimeException(
'Cannot produce a query result set from a result that is not a query result;'
. ' check isQueryResult() first'
);
}

$resultPrototype ??= new ResultSet();
$resultSet = clone $resultPrototype;
$resultSet->initialize($this);

return $resultSet;
}

/**
* Get resource
*/
Expand Down
32 changes: 32 additions & 0 deletions test/asset/ResultStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace PhpDbTestAsset\Pgsql;

use Override;
use PhpDb\Pgsql\Result;

/**
* Result::getQueryResult() only needs isQueryResult() and getFieldCount() to be
* answerable, both of which read the pgsql resource. Overriding them keeps the
* method unit-testable without a live connection.
*/
final class ResultStub extends Result
{
public function __construct(private bool $isQueryResult, private int $fieldCount = 0)
{
}

#[Override]
public function isQueryResult(): bool
{
return $this->isQueryResult;
}

#[Override]
public function getFieldCount(): int
{
return $this->fieldCount;
}
}
57 changes: 57 additions & 0 deletions test/integration/ResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace PhpDbIntegrationTest\Pgsql;

use PhpDb\Adapter\Exception;
use PhpDb\Pgsql\Result;
use PhpDbTestAsset\Pgsql\SetupTrait;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;

#[CoversMethod(Result::class, 'getQueryResult')]
#[CoversMethod(Result::class, 'isQueryResult')]
class ResultTest extends TestCase
{
use SetupTrait;

public function testGetQueryResultSeedsTheResultSetFromASelect(): void
{
$result = $this->getAdapter()->executeQuery('SELECT id, name, value FROM test');

self::assertSame($result->getFieldCount(), $result->getQueryResult()->getFieldCount());
}

public function testGetQueryResultIteratesTheSelectedRows(): void
{
$result = $this->getAdapter()->executeQuery('SELECT name FROM test ORDER BY id');

$names = [];
foreach ($result->getQueryResult() as $row) {
$names[] = $row['name'];
}

self::assertSame(['foo', 'bar'], $names);
}

public function testAStatementReturningNoFieldsIsNotAQueryResult(): void
{
$result = $this->getAdapter()->executeQuery('SET search_path TO public');

self::assertFalse($result->isQueryResult());
}

public function testGetQueryResultRejectsAStatementThatReturnsNoFields(): void
{
$result = $this->getAdapter()->executeQuery('SET search_path TO public');

$this->expectException(Exception\RuntimeException::class);
$this->expectExceptionMessage(
'Cannot produce a query result set from a result that is not a query result;'
. ' check isQueryResult() first'
);

$result->getQueryResult();
}
}
Loading