{"id":15450380,"url":"https://github.com/czukowski/phpunit-mock-db","last_synced_at":"2025-07-04T12:32:41.813Z","repository":{"id":56960988,"uuid":"128817737","full_name":"czukowski/phpunit-mock-db","owner":"czukowski","description":"Database Abstraction Layer mocking for PHPUnit","archived":false,"fork":false,"pushed_at":"2020-12-21T21:02:52.000Z","size":134,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-28T12:00:36.170Z","etag":null,"topics":["database","mock","phpunit","sql"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/czukowski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-09T18:38:38.000Z","updated_at":"2025-05-29T17:34:41.000Z","dependencies_parsed_at":"2022-08-21T05:40:21.184Z","dependency_job_id":null,"html_url":"https://github.com/czukowski/phpunit-mock-db","commit_stats":null,"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"purl":"pkg:github/czukowski/phpunit-mock-db","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2Fphpunit-mock-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2Fphpunit-mock-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2Fphpunit-mock-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2Fphpunit-mock-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czukowski","download_url":"https://codeload.github.com/czukowski/phpunit-mock-db/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2Fphpunit-mock-db/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263541598,"owners_count":23477454,"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","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":["database","mock","phpunit","sql"],"created_at":"2024-10-01T21:04:58.601Z","updated_at":"2025-07-04T12:32:41.791Z","avatar_url":"https://github.com/czukowski.png","language":"PHP","readme":"Database Abstraction Layer mocking helpers for PHPUnit\n======================================================\n\n![PHPUnit](https://github.com/czukowski/phpunit-mock-db/workflows/PHPUnit/badge.svg)\n\nA mock-object library for database queries testing, without having to initialize in-memory\ndatabase from fixtures. Rather, every query executed by a tested code can be set to return\na pre-defined result set, affected rows count or last insert ID. All with a familiar interface\nsimilar to PHPUnit Mock Objects.\n\nInstallation\n------------\n\n```sh\ncomposer require czukowski/phpunit-mock-db\n```\n\nVersion numbering follows major PHPUnit version numbers, so for a given PHPUnit N.x, you'll get\nthe appropriate version of this package (this should happen automatically).\n\nUsage\n-----\n\nUse `Cz\\PHPUnit\\MockDB\\MockTrait` trait in a test case class, this will enable methods for\ncreating database mock instances. A 'fake' driver for a database abstraction layer used in the\ntested code must be used, which implements both `Cz\\PHPUnit\\MockDB\\DatabaseDriverInterface`\ninterface and that of the database abstraction layer's, additionally `getDatabaseDriver` method\nmust be implemented by the test case class, that returns an instance of that driver.\n\nNote: This covers just the most simple and the most common use case for testing against a single\ndatabase connection, and the trait has been designed accordingly. If it is required to have multiple\ndatabase connections mocked at the same time or a different way to inject dependencies into the\ntested code, a different implementation of `MockTrait` may be needed. But the trait is extremely\nsimple, especially in comparison to the 'fake' driver implementation that is needed anyway, you can\nclone and adjust the trait for your project or come up with a completely different implementation.\n\n### Examples:\n\nReturn a pre-defined result set on _any_ database query:\n\n```php\n$this-\u003ecreateDatabaseMock()\n    -\u003eexpects($this-\u003eany())\n    -\u003ewillReturnResultSet([\n        ['id' =\u003e 1, 'name' =\u003e 'foo'],\n        ['id' =\u003e 2, 'name' =\u003e 'bar'],\n    ]);\n```\n\nReturn a pre-defined result set on _any_ database query and expect it to be executed exactly once:\n\n```php\n$this-\u003ecreateDatabaseMock()\n    -\u003eexpects($this-\u003eonce())\n    -\u003ewillReturnResultSet([\n        ['id' =\u003e 1, 'name' =\u003e 'foo'],\n        ['id' =\u003e 2, 'name' =\u003e 'bar'],\n    ]);\n```\n\nReturn a pre-defined result set on each specific database query, expecting each query to be executed\nexactly once:\n\n_Note_: the order in which the query expectations are being set up doesn't have to be same as the order\nin which the queries will be executed.\n\n_Also note_: the whitespaces will be ignored in query constraints, so they can be loaded from well-formatted\nfiles, which could be especially useful for long and complex queries.\n\n```php\n$mock = $this-\u003ecreateDatabaseMock();\n$mock-\u003eexpects($this-\u003eonce())\n    -\u003equery('SELECT * FROM `t1`')\n    -\u003ewillReturnResultSet([['id' =\u003e 1, 'name' =\u003e 'foo']]);\n$mock-\u003eexpects($this-\u003eonce())\n    -\u003equery('SELECT * FROM `t2`')\n    -\u003ewillReturnResultSet([['id' =\u003e 2, 'name' =\u003e 'bar']]);\n```\n\nExpect mixed queries, some at specific invocations (note: SELECT query is set to return an empty\nresult set):\n\n```php\n$mock = $this-\u003ecreateDatabaseMock();\n$mock-\u003eexpects($this-\u003eat(1))\n    -\u003equery('INSERT INTO `t1` VALUES (1, \"foo\")')\n    -\u003ewillSetLastInsertId(1);\n$mock-\u003eexpects($this-\u003eat(2))\n    -\u003equery('INSERT INTO `t1` VALUES (2, \"bar\")')\n    -\u003ewillSetLastInsertId(2);\n$mock-\u003eexpects($this-\u003eonce())\n    -\u003equery('SELECT * FROM `t1`')\n    -\u003ewillReturnResultSet([]);\n```\n\nExpect same query executed exactly three times and return different last insert IDs on each\nconsecutive call, also note how this query is parametrized:\n\n```php\n$this-\u003ecreateDatabaseMock()\n    -\u003eexpects($this-\u003eexactly(3))\n    -\u003equery('INSERT INTO `t1` VALUES (?, ?, ?)')\n    -\u003ewith(['a', 'b', 'c'])\n    -\u003ewillSetLastInsertId(1, 2, 3);\n```\n\nReturn affected rows count:\n\n```php\n$this-\u003ecreateDatabaseMock()\n    -\u003eexpects($this-\u003eexactly(2))\n    -\u003equery('UPDATE `t1` SET `foo` = \"bar\" WHERE `id` = 1')\n    -\u003ewillSetAffectedRows(1);\n```\n\nMatch SQL query using PHPUnit constraint (note: whitespace will not be ignored when using default\nPHPUnit constraints):\n\n```php\n$this-\u003ecreateDatabaseMock()\n    -\u003eexpects($this-\u003eonce())\n    -\u003equery($this-\u003estringStartsWith('SELECT'))\n    -\u003ewillReturnResultSet([['id' =\u003e 1, 'name' =\u003e 'foo']]);\n```\n\nSet up different outcomes on consecutive calls for INSERT queries using a consecutive calls stub\nbuilder:\n\n```php\n$this-\u003ecreateDatabaseMock()\n    -\u003eexpects($this-\u003eexactly(4))\n    -\u003equery($this-\u003estringStartsWith('INSERT'))\n    -\u003eonConsecutiveCalls()\n    -\u003ewillSetLastInsertId(1)\n    -\u003ewillSetLastInsertId(2)\n    -\u003ewillThrowException(new RuntimeException('Deadlock'))\n    -\u003ewillSetLastInsertId(3);\n```\n\nSet up custom callbacks to handle database queries (callbacks don't have to return anything):\n\n```php\n$mock = $this-\u003ecreateDatabaseMock();\n$mock-\u003eexpects($this-\u003eany())\n    -\u003equery($this-\u003estringStartsWith('INSERT'))\n    -\u003ewillInvokeCallback(function ($invocation) {\n        $invocation-\u003esetLastInsertId(1);\n    });\n$mock-\u003eexpects($this-\u003eany())\n    -\u003equery($this-\u003estringStartsWith('UPDATE'))\n    -\u003ewillInvokeCallback(function ($invocation) {\n        $invocation-\u003esetAffectedRows(0);\n    });\n$mock-\u003eexpects($this-\u003eany())\n    -\u003equery($this-\u003estringStartsWith('SELECT'))\n    -\u003ewillInvokeCallback(function ($invocation) {\n        $invocation-\u003esetResultSet([]);\n    });\n```\n\nBy default, mock object is set to throw an exception if an unknown (unmatched) query is executed,\nbut this can be disabled:\n\n```php\n$mock = $this-\u003ecreateDatabaseMock();\n$mock-\u003esetRequireMatch(FALSE);\n```\n\nLicense\n-------\n\nThis work is released under the MIT License. See LICENSE.md for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczukowski%2Fphpunit-mock-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczukowski%2Fphpunit-mock-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczukowski%2Fphpunit-mock-db/lists"}