-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from aternosorg/test-driver
Added a test driver
- Loading branch information
Showing
16 changed files
with
3,201 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
vendor/ | ||
.idea | ||
test.php | ||
test.php | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.