Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a test driver #6

Merged
merged 11 commits into from
Jun 29, 2023
44 changes: 44 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Tests

on:
pull_request:
push:
branches:
- master
- test-driver

jobs:
tests:
runs-on: ubuntu-latest

strategy:
matrix:
php-version: [ '8.1', '8.2' ]

name: Run tests on PHP v${{ matrix.php-version }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}

- name: Set composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Restore composer from cache
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install composer dependencies
run: composer install --no-interaction --prefer-dist --no-progress

- name: Run phpunit tests
run: vendor/bin/phpunit --testsuite tests --colors=always --testdox
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
.idea
test.php
test.php
.phpunit.result.cache
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,91 @@ User::query(new \Aternos\Model\Query\DeleteQuery(
));
```

## Testing
This library includes a [`TestDriver`](src/Driver/Test/TestDriver.php) which can be used to write tests without a database.
It uses a simple array as storage and is not persistent. It supports most basic operations and queries, but might not work
for all use cases yet especially not for database specific queries.

You can just add test data to your model which will also enable the test driver for that model.
```php
<?php

// add a single entry
User::addTestEntry([
"id" => 1,
"name" => "Test",
"email" => "[email protected]"
]);

// add multiple entries at once
User::addTestEntries($entries);

// clear all test entries
User::clearTestEntries();
```

Alternatively, you can also add data to the test driver directly.
```php
/** @var \Aternos\Model\Driver\Test\TestDriver $testDriver */
$testDriver = \Aternos\Model\Driver\DriverRegistry::getInstance()->getDriver(\Aternos\Model\Driver\Test\TestDriver::ID);

// add multiple tables at once
$testDriver->addTables([
"user" => [
[
"id" => 1,
"name" => "Test",
"email" => "[email protected]"
],
...
],
"another_table" => [
[
"id" => 1,
...
]
],
...
]);

// add a single table
$testDriver->addTable("user", [
[
"id" => 1,
"name" => "Test",
"email" => "[email protected]"
],
...
]);

// add an entry to a table
$testDriver->addEntry("user", [
"id" => 1,
"name" => "Test",
"email" => "[email protected]"
]);

// clear all entries from a table
$testDriver->clearEntries("user");

// clear all tables
$testDriver->clearTables();
```
If you add data to the driver directly, you still have to enable the test driver for each model that you want to test.

```php
<?php

// enable the test driver for the user model
User::enableTestDriver();

// you can enable the test driver for all models at once by enabling it on a shared parent
class MyModel extends Aternos\Model\GenericModel {}
class User extends MyModel { ... }
class AnotherModel extends MyModel { ... }
MyModel::enableTestDriver();
```

## Advanced usage
*More information about more advanced usage, such as writing your own drivers, driver factory or models
will be added in the future, in the meantime just take a look at the source code.*
17 changes: 12 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "aternos/model",
"description": "PHP library for simple and complex database models",
"keywords": ["database", "model"],
"keywords": [
"database",
"model"
],
"readme": "README.md",
"type": "library",
"license": "MIT",
Expand All @@ -21,16 +24,20 @@
"ext-json": "*"
},
"require-dev": {
"ext-redis": "*",
"ext-cassandra": "*",
"ext-mysqli": "*",
"elasticsearch/elasticsearch": "^7.10"
"elasticsearch/elasticsearch": "^7.10",
"phpunit/phpunit": "^10.2"
},
"autoload": {
"psr-4": {
"Aternos\\Model\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Aternos\\Model\\Test\\Tests\\": "test/tests/",
"Aternos\\Model\\Test\\Src\\": "test/src/"
}
},
"suggest": {
"ext-redis": "To use the Redis Cache driver",
"ext-cassandra": "To use the Cassandra NoSQL driver",
Expand Down
Loading