Skip to content

Commit 031ba72

Browse files
committed
tests: cover Driver::cleanCache() and Driver::withoutCache()
(cherry picked from commit cad1ce4)
1 parent ce00b52 commit 031ba72

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/Driver/Driver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function withoutCache(): static
106106

107107
$driver = clone $this;
108108
$driver->useCache = false;
109+
$driver->queryCache = [];
109110

110111
return $driver;
111112
}

tests/Database/Functional/Driver/Common/Driver/DriverTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Cycle\Database\Tests\Functional\Driver\Common\Driver;
66

7+
use Cycle\Database\Config\DriverConfig;
8+
use Cycle\Database\Driver\Driver;
9+
use Cycle\Database\Exception\StatementException;
710
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
811

912
abstract class DriverTest extends BaseTest
@@ -59,4 +62,62 @@ public function datetimeDataProvider(): \Traversable
5962
yield [new class('2000-01-23T01:23:45.678+09:00') extends \DateTimeImmutable {}];
6063
yield [new class('2000-01-23T01:23:45.678+09:00') extends \DateTime {}];
6164
}
65+
66+
public function testClearCache(): void
67+
{
68+
$driver = $this->mockDriver();
69+
70+
$driver->testPolluteCache();
71+
self::assertNotEmpty($driver->testGetCache());
72+
73+
$driver->clearCache();
74+
75+
self::assertEmpty($driver->testGetCache());
76+
}
77+
78+
public function testWithoutCache(): void
79+
{
80+
$driver = $this->mockDriver();
81+
$driver->testPolluteCache();
82+
self::assertNotEmpty($driver->testGetCache());
83+
84+
$new = $driver->withoutCache();
85+
86+
self::assertNotEmpty($driver->testGetCache());
87+
self::assertEmpty($new->testGetCache());
88+
}
89+
90+
private function mockDriver(): Driver
91+
{
92+
return new class extends Driver {
93+
public function __construct() {}
94+
95+
public function testPolluteCache(): void
96+
{
97+
$this->queryCache[] = ['sql' => 'SELECT * FROM table', 'params' => []];
98+
}
99+
100+
public function testGetCache(): array
101+
{
102+
return $this->queryCache;
103+
}
104+
105+
protected function mapException(\Throwable $exception, string $query): StatementException
106+
{
107+
throw new \Exception('not needed');
108+
}
109+
110+
public static function create(DriverConfig $config): \Cycle\Database\Driver\DriverInterface
111+
{
112+
throw new \Exception('not needed');
113+
}
114+
115+
public function getType(): string
116+
{
117+
throw new \Exception('not needed');
118+
}
119+
120+
public function __clone(): void {}
121+
};
122+
}
62123
}

0 commit comments

Comments
 (0)