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
6 changes: 0 additions & 6 deletions lint-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ code = "halstead"
message = "Method has a high halstead difficulty"
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Statement.php"
code = "kan-defect"
message = "Class has a high kan defect score."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Statement.php"
code = "literal-named-argument"
Expand Down
4 changes: 0 additions & 4 deletions src/Adapter/Driver/Pdo/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ public function setSql(?string $sql): StatementContainerInterface
/** Bind parameters from container */
protected function bindParametersFromContainer(): void
{
if ($this->parametersBound) {
return;
}

$parameters = $this->parameterContainer->getNamedArray();
$errata = $this->parameterContainer->getErrataIterator()->getArrayCopy();

Expand Down
72 changes: 49 additions & 23 deletions test/unit/Adapter/Driver/Pdo/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PDOStatement;
use PhpDb\Adapter\Driver\Pdo\Result;
use PhpDb\Adapter\Driver\Pdo\Statement;
use PhpDb\Adapter\Driver\ResultInterface;
use PhpDb\Adapter\Exception\InvalidQueryException;
use PhpDb\Adapter\Exception\RuntimeException;
use PhpDb\Adapter\ParameterContainer;
Expand All @@ -24,6 +25,7 @@
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

use function is_array;
use function sprintf;

#[CoversMethod(Statement::class, 'setDriver')]
Expand Down Expand Up @@ -94,19 +96,6 @@ public function bindParametersDetectsNullValueType(): void
static::assertInstanceOf(Result::class, $result);
}

#[Test]
public function bindParametersFromContainerSkipsWhenAlreadyBound(): void
{
$pdo = new SqliteMemoryPdo();
$this->statement->setDriver(new TestPdo(new TestConnection($pdo)));
$this->statement->initialize($pdo);
$this->statement->setSql('SELECT :val');
$this->statement->setParameterContainer(new ParameterContainer(['val' => 'first']));

$result1 = $this->statement->execute();
static::assertInstanceOf(Result::class, $result1);
}

#[Test]
public function bindParametersWithErrataTypeDefaultsToString(): void
{
Expand Down Expand Up @@ -493,19 +482,39 @@ public function prepareThrowsWhenAlreadyPrepared(): void
}

#[Test]
public function secondExecuteSkipsBindingWhenAlreadyBound(): void
public function reExecutingAfterMutatingTheParameterContainerUsesTheNewValues(): void
{
$pdo = new SqliteMemoryPdo();
$this->statement->setDriver(new TestPdo(new TestConnection($pdo)));
$this->statement->initialize($pdo);
$this->statement->setSql('SELECT :val');
$this->statement->setParameterContainer(new ParameterContainer(['val' => 'test']));
$this->prepareLookupStatement();
$container = new ParameterContainer(['id' => 1]);
$this->statement->setParameterContainer($container);

$first = $this->fetchName($this->statement->execute());
$container->offsetSet('id', 2);
$second = $this->fetchName($this->statement->execute());

static::assertSame(['one', 'two'], [$first, $second]);
}

#[Test]
public function reExecutingWithNewArrayParametersUsesTheNewValues(): void
{
$this->prepareLookupStatement();

$result1 = $this->statement->execute();
static::assertInstanceOf(Result::class, $result1);
$first = $this->fetchName($this->statement->execute(['id' => 1]));
$second = $this->fetchName($this->statement->execute(['id' => 2]));

$result2 = $this->statement->execute();
static::assertInstanceOf(Result::class, $result2);
static::assertSame(['one', 'two'], [$first, $second]);
}

#[Test]
public function reExecutingWithReplacementParameterContainerUsesTheNewValues(): void
{
$this->prepareLookupStatement();

$first = $this->fetchName($this->statement->execute(new ParameterContainer(['id' => 1])));
$second = $this->fetchName($this->statement->execute(new ParameterContainer(['id' => 2])));

static::assertSame(['one', 'two'], [$first, $second]);
}

#[Test]
Expand Down Expand Up @@ -550,4 +559,21 @@ protected function setUp(): void
* This method is called after a test is executed.
*/
protected function tearDown(): void {}

private function fetchName(?ResultInterface $result): mixed
{
$row = $result?->current();

return is_array($row) ? $row['name'] ?? null : null;
}

private function prepareLookupStatement(): void
{
$pdo = new SqliteMemoryPdo(
"CREATE TABLE t (id INTEGER, name TEXT); INSERT INTO t VALUES (1, 'one'), (2, 'two');",
);
$this->statement->setDriver(new TestPdo(new TestConnection($pdo)));
$this->statement->initialize($pdo);
$this->statement->setSql('SELECT name FROM t WHERE id = :id');
}
}
Loading