{"id":30115721,"url":"https://github.com/metamodels/phpunit-contao-database","last_synced_at":"2025-10-09T14:19:01.134Z","repository":{"id":23113498,"uuid":"26467948","full_name":"MetaModels/phpunit-contao-database","owner":"MetaModels","description":"Small set of classes to allow to test MetaModels against a fake Contao database.","archived":false,"fork":false,"pushed_at":"2016-07-20T11:52:18.000Z","size":46,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-08-27T22:09:36.080Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MetaModels.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-11T03:35:44.000Z","updated_at":"2016-07-20T11:52:19.000Z","dependencies_parsed_at":"2022-08-18T23:51:13.736Z","dependency_job_id":null,"html_url":"https://github.com/MetaModels/phpunit-contao-database","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/MetaModels/phpunit-contao-database","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaModels%2Fphpunit-contao-database","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaModels%2Fphpunit-contao-database/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaModels%2Fphpunit-contao-database/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaModels%2Fphpunit-contao-database/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MetaModels","download_url":"https://codeload.github.com/MetaModels/phpunit-contao-database/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaModels%2Fphpunit-contao-database/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279001528,"owners_count":26083117,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-10T08:52:17.027Z","updated_at":"2025-10-09T14:19:01.096Z","avatar_url":"https://github.com/MetaModels.png","language":"PHP","readme":"MetaModels database objects for phpunit\n=======================================\n\nWhat you can expect here.\n-------------------------\n\nThis repository holds a small subset of classes that can be used to fake a Contao database connection.\n\nTo do so, you define the queries and their result sets.\n\nInstallation\n------------\n\nAdd `\"metamodels/phpunit-contao-database\": \"~1.0\"` to your `composer.json` in the `require-dev` section and you are set.\n\nHow to use it.\n--------------\n\nIn your unit test you simply replace the original Contao database classes by calling the following code (before the \noriginal Contao database classes get loaded via autoloading!). A good place might be the `setUp()`-method of your test\ncase.\n\n```php\nclass MyTest extends \\PHPUnit_Framework_TestCase\n{\n    /**\n     * Register the database.\n     *\n     * @return void\n     */\n    public function setUp()\n    {\n        \\MetaModels\\Test\\Contao\\Database::register();\n    }\n}\n```\n\nUsing it in unit tests\n----------------------\n\nYou should use dependency injection for proper unit testing, then you can use something like this:\n\n```php\n$objectToTest = new ClassToTest();\n$database     = \\MetaModels\\Test\\Contao\\Database::getNewTestInstance();\n\n// Inject the database into the instance.\n$objectToTest-\u003esetDatabase($database);\n\n$database\n    -\u003egetQueryCollection()\n    -\u003etheQuery('SELECT * FROM test WHERE id=?')\n    -\u003ewith(1)\n    -\u003eresult()\n        -\u003eaddRow(\n            array(\n              'id'     =\u003e 1,\n              'tstamp' =\u003e 343094400,\n              'title'  =\u003e 'test'\n            ),\n        );\n\n// This method will call the above query internally and will receive the given result.\n$result = $objectToTest-\u003etestMethod();\n\n$this-\u003eassertEquals(1, $result-\u003egetId());\n$this-\u003eassertEquals(343094400, $result-\u003egetModificationTime());\n$this-\u003eassertEquals('test', $result-\u003egetTitle());\n```\n\nIf you should be in the unfortunate position that you can not use dependency injection, as the underlying code is using\n```Database::getInstance()```, you are not out of luck. You can use the method \n```$database = \\MetaModels\\Test\\Contao\\Database::getNewTestInstance();``` which will return the \"default\" database \ninstance then.\n\nHowever using this approach is not suggested as the instance will be shared over all unit tests.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetamodels%2Fphpunit-contao-database","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetamodels%2Fphpunit-contao-database","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetamodels%2Fphpunit-contao-database/lists"}