Skip to content

Commit 5b61f0b

Browse files
authored
Merge pull request #6 from aternosorg/test-driver
Added a test driver
2 parents b0b21f8 + fed0798 commit 5b61f0b

16 files changed

+3201
-107
lines changed

.github/workflows/tests.yaml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
- test-driver
9+
10+
jobs:
11+
tests:
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
php-version: [ '8.1', '8.2' ]
17+
18+
name: Run tests on PHP v${{ matrix.php-version }}
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v2
23+
24+
- name: Setup PHP
25+
uses: shivammathur/setup-php@v2
26+
with:
27+
php-version: ${{ matrix.php-version }}
28+
29+
- name: Set composer cache directory
30+
id: composer-cache
31+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
32+
33+
- name: Restore composer from cache
34+
uses: actions/cache@v2
35+
with:
36+
path: ${{ steps.composer-cache.outputs.dir }}
37+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
38+
restore-keys: ${{ runner.os }}-composer-
39+
40+
- name: Install composer dependencies
41+
run: composer install --no-interaction --prefer-dist --no-progress
42+
43+
- name: Run phpunit tests
44+
run: vendor/bin/phpunit --testsuite tests --colors=always --testdox

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor/
22
.idea
3-
test.php
3+
test.php
4+
.phpunit.result.cache

README.md

+85
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,91 @@ User::query(new \Aternos\Model\Query\DeleteQuery(
279279
));
280280
```
281281

282+
## Testing
283+
This library includes a [`TestDriver`](src/Driver/Test/TestDriver.php) which can be used to write tests without a database.
284+
It uses a simple array as storage and is not persistent. It supports most basic operations and queries, but might not work
285+
for all use cases yet especially not for database specific queries.
286+
287+
You can just add test data to your model which will also enable the test driver for that model.
288+
```php
289+
<?php
290+
291+
// add a single entry
292+
User::addTestEntry([
293+
"id" => 1,
294+
"name" => "Test",
295+
"email" => "[email protected]"
296+
]);
297+
298+
// add multiple entries at once
299+
User::addTestEntries($entries);
300+
301+
// clear all test entries
302+
User::clearTestEntries();
303+
```
304+
305+
Alternatively, you can also add data to the test driver directly.
306+
```php
307+
/** @var \Aternos\Model\Driver\Test\TestDriver $testDriver */
308+
$testDriver = \Aternos\Model\Driver\DriverRegistry::getInstance()->getDriver(\Aternos\Model\Driver\Test\TestDriver::ID);
309+
310+
// add multiple tables at once
311+
$testDriver->addTables([
312+
"user" => [
313+
[
314+
"id" => 1,
315+
"name" => "Test",
316+
"email" => "[email protected]"
317+
],
318+
...
319+
],
320+
"another_table" => [
321+
[
322+
"id" => 1,
323+
...
324+
]
325+
],
326+
...
327+
]);
328+
329+
// add a single table
330+
$testDriver->addTable("user", [
331+
[
332+
"id" => 1,
333+
"name" => "Test",
334+
"email" => "[email protected]"
335+
],
336+
...
337+
]);
338+
339+
// add an entry to a table
340+
$testDriver->addEntry("user", [
341+
"id" => 1,
342+
"name" => "Test",
343+
"email" => "[email protected]"
344+
]);
345+
346+
// clear all entries from a table
347+
$testDriver->clearEntries("user");
348+
349+
// clear all tables
350+
$testDriver->clearTables();
351+
```
352+
If you add data to the driver directly, you still have to enable the test driver for each model that you want to test.
353+
354+
```php
355+
<?php
356+
357+
// enable the test driver for the user model
358+
User::enableTestDriver();
359+
360+
// you can enable the test driver for all models at once by enabling it on a shared parent
361+
class MyModel extends Aternos\Model\GenericModel {}
362+
class User extends MyModel { ... }
363+
class AnotherModel extends MyModel { ... }
364+
MyModel::enableTestDriver();
365+
```
366+
282367
## Advanced usage
283368
*More information about more advanced usage, such as writing your own drivers, driver factory or models
284369
will be added in the future, in the meantime just take a look at the source code.*

composer.json

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"name": "aternos/model",
33
"description": "PHP library for simple and complex database models",
4-
"keywords": ["database", "model"],
4+
"keywords": [
5+
"database",
6+
"model"
7+
],
58
"readme": "README.md",
69
"type": "library",
710
"license": "MIT",
@@ -21,16 +24,20 @@
2124
"ext-json": "*"
2225
},
2326
"require-dev": {
24-
"ext-redis": "*",
25-
"ext-cassandra": "*",
26-
"ext-mysqli": "*",
27-
"elasticsearch/elasticsearch": "^7.10"
27+
"elasticsearch/elasticsearch": "^7.10",
28+
"phpunit/phpunit": "^10.2"
2829
},
2930
"autoload": {
3031
"psr-4": {
3132
"Aternos\\Model\\": "src/"
3233
}
3334
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Aternos\\Model\\Test\\Tests\\": "test/tests/",
38+
"Aternos\\Model\\Test\\Src\\": "test/src/"
39+
}
40+
},
3441
"suggest": {
3542
"ext-redis": "To use the Redis Cache driver",
3643
"ext-cassandra": "To use the Cassandra NoSQL driver",

0 commit comments

Comments
 (0)