Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Oct 31, 2023
1 parent 403839f commit 29bbcd0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
9 changes: 3 additions & 6 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,9 @@ public function withoutCache(): self
}

if ($this->driver instanceof Driver) {
if ($database->readDriver === $database->driver) {
$database->readDriver = $database->readDriver->withoutCache();
$database->driver = $database->readDriver;
} else {
$database->driver = $database->driver->withoutCache();
}
$database->driver = $database->readDriver === $database->driver
? ($database->readDriver = $database->driver->withoutCache())
: $database->driver->withoutCache();

return $database;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public function isReadonly(): bool

public function withoutCache(): static
{
if ($this->useCache === false) {
// Cache already disabled
return $this;
}

$driver = clone $this;
$driver->useCache = false;

Expand Down
70 changes: 28 additions & 42 deletions tests/Database/Unit/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Cycle\Database\Tests\Unit;

use Cycle\Database\Database;
use Cycle\Database\DatabaseInterface;
use Cycle\Database\Driver\Driver;
use Cycle\Database\Driver\DriverInterface;
use PHPUnit\Framework\TestCase;
Expand All @@ -23,10 +24,8 @@ public function testWithoutCacheWithoutReadDriver(): void

$newDb = $database->withoutCache();

$ref = new \ReflectionProperty($newDb, 'readDriver');
$ref->setAccessible(true);

$this->assertNull($ref->getValue($newDb));
$this->assertNull($this->readProperty($newDb, 'readDriver'));
$this->assertsame($driver, $database->getDriver());
$this->assertNotSame($driver, $newDb->getDriver());
}

Expand All @@ -38,17 +37,12 @@ public function testWithoutCacheWithSameDriverAndReadDriver(): void
->expects($this->once())
->method('withoutCache');

$database = new Database('default', '', $driver, $driver);

$newDb = $database->withoutCache();
$db = new Database('default', '', $driver, $driver);

$refDriver = new \ReflectionProperty($newDb, 'driver');
$refDriver->setAccessible(true);
$newDb = $db->withoutCache();

$refReadDriver = new \ReflectionProperty($newDb, 'readDriver');
$refReadDriver->setAccessible(true);

$this->assertSame($refDriver->getValue($newDb), $refReadDriver->getValue($newDb));
$this->assertSame($db->getDriver(DatabaseInterface::WRITE), $db->getDriver(DatabaseInterface::READ));
$this->assertSame($newDb->getDriver(DatabaseInterface::WRITE), $newDb->getDriver(DatabaseInterface::READ));
}

public function testWithoutCacheWithDriverAndReadDriver(): void
Expand All @@ -64,54 +58,37 @@ public function testWithoutCacheWithDriverAndReadDriver(): void
->expects($this->once())
->method('withoutCache');

$database = new Database('default', '', $driver, $readDriver);

$newDb = $database->withoutCache();

$refDriver = new \ReflectionProperty($newDb, 'driver');
$refDriver->setAccessible(true);
$db = new Database('default', '', $driver, $readDriver);

$refReadDriver = new \ReflectionProperty($newDb, 'readDriver');
$refReadDriver->setAccessible(true);
$newDb = $db->withoutCache();

$this->assertNotSame($refDriver->getValue($newDb), $refReadDriver->getValue($newDb));
$this->assertNotSame($driver, $refDriver->getValue($newDb));
$this->assertNotSame($readDriver, $refReadDriver->getValue($newDb));
$this->assertNotSame($newDb->getDriver(DatabaseInterface::WRITE), $newDb->getDriver(DatabaseInterface::READ));
$this->assertNotSame($driver, $newDb->getDriver(DatabaseInterface::WRITE));
$this->assertNotSame($readDriver, $newDb->getDriver(DatabaseInterface::READ));
}

public function testWithoutCacheWithoutReadDriverAndWithoutMethod(): void
{
$driver = $this->createMock(DriverInterface::class);

$database = new Database('default', '', $driver);

$newDb = $database->withoutCache();

$ref = new \ReflectionProperty($newDb, 'readDriver');
$ref->setAccessible(true);

$this->assertNull($ref->getValue($newDb));
$this->assertNull($this->readProperty($newDb, 'readDriver'));
$this->assertSame($driver, $newDb->getDriver());
}

public function testWithoutCacheWithDriverAndReadDriverWithoutMethod(): void
{
$driver = $this->createMock(DriverInterface::class);
$readDriver = $this->createMock(DriverInterface::class);

$database = new Database('default', '', $driver, $readDriver);

$newDb = $database->withoutCache();

$refDriver = new \ReflectionProperty($newDb, 'driver');
$refDriver->setAccessible(true);

$refReadDriver = new \ReflectionProperty($newDb, 'readDriver');
$refReadDriver->setAccessible(true);

$this->assertNotSame($refDriver->getValue($newDb), $refReadDriver->getValue($newDb));
$this->assertSame($driver, $refDriver->getValue($newDb));
$this->assertSame($readDriver, $refReadDriver->getValue($newDb));
$this->assertNotSame($newDb->getDriver(DatabaseInterface::WRITE), $newDb->getDriver(DatabaseInterface::READ));
$this->assertSame($driver, $newDb->getDriver(DatabaseInterface::WRITE));
$this->assertSame($readDriver, $newDb->getDriver(DatabaseInterface::READ));
}

public function testWithoutCacheWithSameDriversAndWithoutMethod(): void
Expand All @@ -128,8 +105,17 @@ public function testWithoutCacheWithSameDriversAndWithoutMethod(): void
$refReadDriver = new \ReflectionProperty($newDb, 'readDriver');
$refReadDriver->setAccessible(true);

$this->assertSame($refDriver->getValue($newDb), $refReadDriver->getValue($newDb));
$this->assertSame($driver, $refDriver->getValue($newDb));
$this->assertSame($driver, $refReadDriver->getValue($newDb));
$this->assertSame($newDb->getDriver(DatabaseInterface::WRITE), $newDb->getDriver(DatabaseInterface::READ));
$this->assertSame($driver, $newDb->getDriver(DatabaseInterface::WRITE));
$this->assertSame($driver, $newDb->getDriver(DatabaseInterface::READ));
}

private function readProperty(object $object, string $property): mixed
{
$fn = function () use ($property) {
return $this->$property;
};

return $fn->call($object);
}
}
16 changes: 16 additions & 0 deletions tests/Database/Unit/Driver/AbstractDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ public function testWithoutCache(): void
$this->assertFalse($ref->getValue($driver->withoutCache()));
}

public function testWithoutCacheTwice(): void
{
$driver = TestDriver::create(new SQLiteDriverConfig(queryCache: true));

$ncDriver = $driver->withoutCache();

$this->assertSame($ncDriver, $ncDriver->withoutCache());
}

public function testWithoutCacheOnWithoutCacheInitially(): void
{
$driver = TestDriver::create(new SQLiteDriverConfig(queryCache: false));

$this->assertSame($driver, $driver->withoutCache());
}

public function testPdoNotClonedAfterCacheDisabled(): void
{
$ref = new \ReflectionMethod(Driver::class, 'getPDO');
Expand Down

0 comments on commit 29bbcd0

Please sign in to comment.