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
27 changes: 11 additions & 16 deletions src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@
use function is_string;
use function strtolower;

class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface, SchemaAwareInterface
readonly class Adapter implements AdapterInterface, SchemaAwareInterface
{
/**
* @throws Exception\InvalidArgumentException
*/
public function __construct(
final public function __construct(
protected Driver\DriverInterface $driver,
protected Platform\PlatformInterface $platform,
protected ResultSet\ResultSetInterface $queryResultSetPrototype = new ResultSet\ResultSet(),
protected ?Profiler\ProfilerInterface $profiler = null
) {
if ($profiler) {
$this->setProfiler($profiler);
}
}

#[Override]
public function setProfiler(Profiler\ProfilerInterface $profiler): Profiler\ProfilerAwareInterface
{
$this->profiler = $profiler;
if ($this->driver instanceof Profiler\ProfilerAwareInterface) {
$this->driver->setProfiler($profiler);
}
return $this;
final public function withProfiler(
Profiler\ProfilerInterface $profiler
): static {
return new static(
$this->driver,
$this->platform,
$this->queryResultSetPrototype,
$profiler,
);
}

#[Override]
Expand Down
2 changes: 2 additions & 0 deletions src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface AdapterInterface

public const VALUE_QUOTE_SEPARATOR = 'quoteSeparator';

public function withProfiler(Profiler\ProfilerInterface $profiler): AdapterInterface;

public function getDriver(): Driver\DriverInterface;

public function getPlatform(): Platform\PlatformInterface;
Expand Down
15 changes: 8 additions & 7 deletions test/unit/Adapter/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpDb\Adapter\ParameterContainer;
use PhpDb\Adapter\Platform\PlatformInterface;
use PhpDb\Adapter\Profiler;
use PhpDb\Adapter\SchemaAwareInterface;
use PhpDb\ResultSet\ResultSet;
use PhpDb\ResultSet\ResultSetInterface;
use PhpDbTest\TestAsset\TemporaryResultSet;
Expand All @@ -24,7 +25,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

#[CoversMethod(Adapter::class, 'setProfiler')]
#[CoversMethod(Adapter::class, 'withProfiler')]
#[CoversMethod(Adapter::class, 'getProfiler')]
#[CoversMethod(Adapter::class, 'createDriver')]
#[CoversMethod(Adapter::class, 'createPlatform')]
Expand All @@ -45,7 +46,7 @@ final class AdapterTest extends TestCase

protected StatementInterface&MockObject $mockStatement;

protected Adapter $adapter;
protected AdapterInterface&SchemaAwareInterface&Adapter $adapter;

/**
* @throws Exception
Expand All @@ -66,17 +67,17 @@ protected function setUp(): void
$this->adapter = new Adapter($this->mockDriver, $this->mockPlatform);
}

#[TestDox('unit test: Test setProfiler() will store profiler')]
public function testSetProfiler(): void
#[TestDox('unit test: Test withProfiler() will store profiler')]
public function testWithProfiler(): void
{
$ret = $this->adapter->setProfiler(new Profiler\Profiler());
self::assertSame($this->adapter, $ret);
$ret = $this->adapter->withProfiler(new Profiler\Profiler());
self::assertNotSame($this->adapter, $ret);
}

#[TestDox('unit test: Test getProfiler() will store profiler')]
public function testGetProfiler(): void
{
$this->adapter->setProfiler($profiler = new Profiler\Profiler());
$this->adapter = $this->adapter->withProfiler($profiler = new Profiler\Profiler());
self::assertSame($profiler, $this->adapter->getProfiler());

$adapter = new Adapter(
Expand Down