Skip to content

Commit 67f2e46

Browse files
committed
feature: split query collection into directory/class subclasses
for #84
1 parent 58840e6 commit 67f2e46

9 files changed

+83
-20
lines changed

src/Query/QueryCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Gt\Database\Fetchable;
66
use Gt\Database\Result\ResultSet;
77

8-
class QueryCollection {
8+
abstract class QueryCollection {
99
use Fetchable;
1010

1111
protected string $directoryPath;
@@ -14,7 +14,7 @@ class QueryCollection {
1414
public function __construct(
1515
string $directoryPath,
1616
Driver $driver,
17-
?QueryFactory $queryFactory = null
17+
?QueryFactory $queryFactory = null,
1818
) {
1919
$this->directoryPath = $directoryPath;
2020
$this->queryFactory = $queryFactory ?? new QueryFactory(

src/Query/QueryCollectionClass.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Gt\Database\Query;
3+
4+
use Gt\Database\Query\QueryCollection;
5+
6+
class QueryCollectionClass extends QueryCollection {
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace Gt\Database\Query;
3+
4+
class QueryCollectionDirectory extends QueryCollection {
5+
}

src/Query/QueryCollectionFactory.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use DirectoryIterator;
55
use Gt\Database\Connection\Driver;
66
use Gt\Database\Database;
7+
use SplFileInfo;
78

89
class QueryCollectionFactory {
910
protected Driver $driver;
@@ -19,15 +20,9 @@ public function __construct(Driver $driver) {
1920

2021
public function create(string $name):QueryCollection {
2122
if(!isset($this->queryCollectionCache[$name])) {
22-
$directoryPath = $this->locateDirectory($name);
23-
24-
if(is_null($directoryPath)) {
25-
throw new QueryCollectionNotFoundException($name);
26-
}
27-
28-
$this->queryCollectionCache[$name] = new QueryCollection(
29-
$directoryPath,
30-
$this->driver
23+
$this->queryCollectionCache[$name] = $this->findQueryCollection(
24+
$name,
25+
$this->driver,
3126
);
3227
}
3328

@@ -74,13 +69,13 @@ protected function recurseLocateDirectory(
7469
throw new BaseQueryPathDoesNotExistException($basePath);
7570
}
7671

72+
/** @var SplFileInfo $fileInfo */
7773
foreach(new DirectoryIterator($basePath) as $fileInfo) {
78-
if($fileInfo->isDot()
79-
|| !$fileInfo->isDir()) {
74+
if($fileInfo->isDot()) {
8075
continue;
8176
}
8277

83-
$basename = $fileInfo->getBasename();
78+
$basename = $fileInfo->getBasename(".php");
8479
if(strtolower($part) === strtolower($basename)) {
8580
$realPath = $fileInfo->getRealPath();
8681

@@ -101,4 +96,30 @@ protected function recurseLocateDirectory(
10196
protected function getDefaultBasePath():string {
10297
return getcwd();
10398
}
99+
100+
private function findQueryCollection(
101+
string $name,
102+
Driver $driver,
103+
):QueryCollection {
104+
$path = $this->locateDirectory($name);
105+
106+
if($path && is_dir($path)) {
107+
$this->queryCollectionCache[$name] = new QueryCollectionDirectory(
108+
$path,
109+
$driver,
110+
);
111+
}
112+
elseif($path && is_file($path)) {
113+
$this->queryCollectionCache[$name] = new QueryCollectionClass(
114+
$path,
115+
$driver,
116+
);
117+
}
118+
else {
119+
throw new QueryCollectionNotFoundException($name);
120+
}
121+
122+
return $this->queryCollectionCache[$name];
123+
}
124+
104125
}

test/phpunit/DatabaseTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Gt\Database\Connection\Settings;
55
use Gt\Database\Database;
66
use Gt\Database\Query\QueryCollection;
7+
use Gt\Database\Query\QueryCollectionClass;
78
use Gt\Database\Query\QueryCollectionNotFoundException;
89
use PHPUnit\Framework\TestCase;
910

@@ -60,4 +61,27 @@ public function testQueryCollectionDots(
6061
$queryCollection = $db->queryCollection($dotName);
6162
self::assertInstanceOf(QueryCollection::class, $queryCollection);
6263
}
63-
}
64+
65+
/** @dataProvider \Gt\Database\Test\Helper\Helper::queryCollectionPathNotExistsProvider() */
66+
public function testQueryCollectionPhp(
67+
string $name,
68+
string $path,
69+
) {
70+
$path = "$path.php";
71+
$baseQueryDirectory = dirname($path);
72+
if(!is_dir($baseQueryDirectory)) {
73+
mkdir($baseQueryDirectory, recursive: true);
74+
}
75+
touch($path);
76+
77+
$settings = new Settings(
78+
$baseQueryDirectory,
79+
Settings::DRIVER_SQLITE,
80+
Settings::SCHEMA_IN_MEMORY,
81+
);
82+
$sut = new Database($settings);
83+
$queryCollection = $sut->queryCollection($name);
84+
85+
self::assertInstanceOf(QueryCollectionClass::class, $queryCollection);
86+
}
87+
}

test/phpunit/Helper/Helper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ private static function queryCollectionPathProvider(
141141
$nameParts = [];
142142

143143
for($n = 0; $n < $nested; $n++) {
144-
$nameParts []= uniqid();
144+
$uniqid = uniqid();
145+
if(is_numeric($uniqid[0])) {
146+
$uniqid = chr(rand(97, 102)) . $uniqid; // Add a random lowercase letter (a-f) to the beginning
147+
}
148+
$nameParts []= $uniqid;
145149
}
146150

147151
$name = implode(DIRECTORY_SEPARATOR, $nameParts);

test/phpunit/Query/QueryCollectionCRUDsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Gt\Database\Connection\Driver;
66
use Gt\Database\Query\Query;
77
use Gt\Database\Query\QueryCollection;
8+
use Gt\Database\Query\QueryCollectionDirectory;
89
use Gt\Database\Query\QueryFactory;
910
use Gt\Database\Result\ResultSet;
1011
use Gt\Database\Result\Row;
@@ -27,7 +28,7 @@ protected function setUp():void {
2728
->willReturn($this->mockQuery);
2829

2930
/** @var QueryFactory $mockQueryFactory */
30-
$this->queryCollection = new QueryCollection(
31+
$this->queryCollection = new QueryCollectionDirectory(
3132
__DIR__,
3233
new Driver(new DefaultSettings()),
3334
$mockQueryFactory

test/phpunit/Query/QueryCollectionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Gt\Database\Connection\Driver;
66
use Gt\Database\Query\Query;
77
use Gt\Database\Query\QueryCollection;
8+
use Gt\Database\Query\QueryCollectionDirectory;
89
use Gt\Database\Query\QueryFactory;
910
use Gt\Database\Result\ResultSet;
1011
use PHPUnit\Framework\MockObject\MockObject;
@@ -26,7 +27,7 @@ protected function setUp():void {
2627
->with("something")
2728
->willReturn($this->mockQuery);
2829

29-
$this->queryCollection = new QueryCollection(
30+
$this->queryCollection = new QueryCollectionDirectory(
3031
__DIR__,
3132
new Driver(new DefaultSettings()),
3233
$mockQueryFactory
@@ -83,4 +84,4 @@ public function testQueryShorthandNoParams() {
8384
$this->queryCollection->something()
8485
);
8586
}
86-
}
87+
}

test/phpunit/Query/QueryFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ public function testSelectsCorrectFile() {
9292
$queryFileList[$queryName] = $query->getFilePath();
9393
}
9494
}
95-
}
95+
}

0 commit comments

Comments
 (0)