{"id":25009312,"url":"https://github.com/tarantool-php/phpunit-extras","last_synced_at":"2025-04-12T19:09:17.452Z","repository":{"id":57065194,"uuid":"252861037","full_name":"tarantool-php/phpunit-extras","owner":"tarantool-php","description":"A collection of helpers for PHPUnit to ease testing Tarantool libraries.","archived":false,"fork":false,"pushed_at":"2022-10-21T20:39:10.000Z","size":55,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T13:21:16.329Z","etag":null,"topics":["phpunit","phpunit-assertions","phpunit-extension","phpunit-extras","phpunit-util","tarantool"],"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/tarantool-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["rybakit"]}},"created_at":"2020-04-03T23:00:28.000Z","updated_at":"2021-11-27T23:23:35.000Z","dependencies_parsed_at":"2022-08-24T14:01:04.125Z","dependency_job_id":null,"html_url":"https://github.com/tarantool-php/phpunit-extras","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarantool-php%2Fphpunit-extras","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarantool-php%2Fphpunit-extras/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarantool-php%2Fphpunit-extras/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarantool-php%2Fphpunit-extras/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tarantool-php","download_url":"https://codeload.github.com/tarantool-php/phpunit-extras/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248145014,"owners_count":21055029,"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":["phpunit","phpunit-assertions","phpunit-extension","phpunit-extras","phpunit-util","tarantool"],"created_at":"2025-02-05T04:39:14.054Z","updated_at":"2025-04-12T19:09:17.431Z","avatar_url":"https://github.com/tarantool-php.png","language":"PHP","readme":"# PHPUnit Extras\n\n[![Quality Assurance](https://github.com/tarantool-php/phpunit-extras/workflows/QA/badge.svg)](https://github.com/tarantool-php/phpunit-extras/actions?query=workflow%3AQA)\n[![Telegram](https://img.shields.io/badge/Telegram-join%20chat-blue.svg)](https://t.me/tarantool_php)\n\nA collection of helpers for [PHPUnit](https://phpunit.de/) to ease testing [Tarantool](https://www.tarantool.io/en/developers/) libraries.\nIt is based on [rybakit/phpunit-extras](https://github.com/rybakit/phpunit-extras), please refer to this package for more documentation.\n\n\n## Table of contents\n\n * [Installation](#installation)\n * [Annotations](#annotations)\n   * [Processors](#processors)\n     * [Lua](#lua)\n     * [Sql](#sql)\n   * [Requirements](#requirements)\n     * [LuaCondition](#luacondition)\n     * [TarantoolVersion](#tarantoolversion)\n * [Expectations](#expectations)\n   * [Requests](#requests)\n   * [Prepared statements](#prepared-statements)\n * [Mocking](#mocking)\n * [Testing](#testing)\n * [License](#license)\n\n\n## Installation\n\n```bash\ncomposer require --dev tarantool/phpunit-extras\n```\n\n\n## Annotations\n\nBesides the annotations provided by the package `rybakit/phpunit-extras`, the library is shipped\nwith annotations specific to Tarantool. The easiest way to enable them is by inheriting your test classes\nfrom `Tarantool\\PhpUnit\\TestCase`:\n\n```php\nuse Tarantool\\Client\\Client;\nuse Tarantool\\PhpUnit\\TestCase;\n\nfinal class MyTest extends TestCase\n{\n    protected function getClient() : Client\n    {\n        // TODO: Implement getClient() method.\n    }\n    \n    // ...\n}\n```\n\nAnother option is to register an extension called `AnnotationExtension`:\n\n```xml\n\u003cphpunit xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xsi:noNamespaceSchemaLocation=\"vendor/phpunit/phpunit/phpunit.xsd\"\n    bootstrap=\"vendor/autoload.php\"\n\u003e\n    \u003c!-- ... --\u003e\n\n    \u003cextensions\u003e\n        \u003cextension class=\"Tarantool\\PhpUnit\\Annotation\\AnnotationExtension\" /\u003e\n    \u003c/extensions\u003e\n\u003c/phpunit\u003e\n```\n\nBy default, the extension assumes that the Tarantool server you are going to connect to is available on `127.0.0.1:3301`.\nYou can customize the default settings by specifying either a [DSN string](https://github.com/tarantool-php/client#dsn-string) or an [array of options](https://github.com/tarantool-php/client#array-of-options)\nas extension configuration values:\n\n```xml\n\u003cextension class=\"Tarantool\\PhpUnit\\Annotation\\AnnotationExtension\"\u003e\n    \u003carguments\u003e\n        \u003cstring\u003etcp://127.0.0.1:3301/?socket_timeout=10\u003c/string\u003e\n    \u003c/arguments\u003e\n\u003c/extension\u003e\n```\nor\n```xml\n\u003cextension class=\"Tarantool\\PhpUnit\\Annotation\\AnnotationExtension\"\u003e\n    \u003carguments\u003e\n        \u003carray\u003e\n            \u003celement key=\"uri\"\u003e\n                \u003cstring\u003etcp://127.0.0.1:3301\u003c/string\u003e\n            \u003c/element\u003e\n            \u003celement key=\"socket_timeout\"\u003e\n                \u003cinteger\u003e10\u003c/integer\u003e\n            \u003c/element\u003e\n        \u003c/array\u003e\n    \u003c/arguments\u003e\n\u003c/extension\u003e\n```\n\nOn top of that, the configuration values can resolve environment variables,\nwhich might be useful if you need to share the same settings with a Tarantool\ninstance file or any other script:\n\n```xml\n\u003cextension class=\"Tarantool\\PhpUnit\\Annotation\\AnnotationExtension\"\u003e\n    \u003carguments\u003e\n        \u003cstring\u003etcp://%env(TARANTOOL_HOST)%:%env(TARANTOOL_PORT)%\u003c/string\u003e\n    \u003c/arguments\u003e\n\u003c/extension\u003e\n```\n\nOnce the annotations are configured, you can start using them:\n\n### Processors\n\n#### Lua\n\nAllows executing Lua code before running a test.\n\n*Example:*\n\n```php\n/**\n * @lua tube:put('kick_me')\n * @lua tube:bury(0)\n */\npublic function testKickReleasesBuriedTask() : void\n{\n    // ...\n}\n```\n\n#### Sql\n\nAllows executing SQL statements before running a test (requires Tarantool 2.0+).\n\n*Example:*\n\n```php\n/**\n * @sql DROP TABLE IF EXISTS foobar\n * @sql CREATE TABLE foobar (id INTEGER PRIMARY KEY, name VARCHAR(50))\n * @sql INSERT INTO foobar VALUES (1, 'A'), (2, 'B')\n */ \npublic function testExecuteQueryFetchesAllRows() : void\n{\n    // ...\n}\n```\n\n\n### Requirements\n\nRequirements allow skipping tests based on preconditions.\n\n#### LuaCondition\n\n*Format:*\n\n```\n@requires luaCondition \u003ccondition\u003e\n```\nwhere `\u003ccondition\u003e` is an arbitrary lua expression that should be evaluated to a Boolean value.\n\n*Example:*\n\n```php\n/**\n * @requires luaCondition box.session.user() ~= 'guest'\n */\npublic function testChangeUserPassword() : void\n{\n    // ...\n}\n```\n\n#### TarantoolVersion\n\n*Format:*\n\n```\n@requires Tarantool \u003cversion-constraint\u003e\n```\nwhere `\u003cversion-constraint\u003e` is a composer-like version constraint. For details on supported formats, \nplease see the Composer [documentation](https://getcomposer.org/doc/articles/versions.md#writing-version-constraints).\n\n*Example:*\n\n```php\n/**\n * @requires Tarantool ^2.3.2 \n */\npublic function testPrepareCreatesPreparedStatement() : void\n{\n    // ...\n}\n```\n\n\u003e *If you're interested in how to create and register your own annotations and requirements,\n\u003e please refer to the `rybakit/phpunit-extras` [README](https://github.com/rybakit/phpunit-extras).*\n\n\n## Expectations\n\n### Requests\n\nTo test that your code sends (or does not send) certain requests, the following methods are available:\n\n * `TestCase::expect\u003cREQUEST_NAME\u003eRequestToBeCalled(int $count) : void`\n * `TestCase::expect\u003cREQUEST_NAME\u003eRequestToBeCalledAtLeast(int $count) : void`\n * `TestCase::expect\u003cREQUEST_NAME\u003eRequestToBeCalledAtMost(int $count) : void`\n * `TestCase::expect\u003cREQUEST_NAME\u003eRequestToBeCalledOnce() : void`\n * `TestCase::expect\u003cREQUEST_NAME\u003eRequestToBeCalledAtLeastOnce() : void`\n * `TestCase::expect\u003cREQUEST_NAME\u003eRequestToBeCalledAtMostOnce() : void`\n * `TestCase::expect\u003cREQUEST_NAME\u003eRequestToBeNeverCalled() : void`\n * `TestCase::expectNoRequestToBeCalled() : void`\n\nwhere `\u003cREQUEST_NAME\u003e` is the name of the request, for example `Call`, `Insert`, etc.\nThese methods are part of the `Tarantool\\PhpUnit\\TestCase` class, but they can also be enabled through a trait:\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse PHPUnitExtras\\Expectation\\Expectations as BaseExpectations;\nuse Tarantool\\Client\\Client;\nuse Tarantool\\PhpUnit\\Expectation\\RequestExpectations;\n\nfinal class MyTest extends TestCase\n{\n    use BaseExpectations;\n    use RequestExpectations;\n\n    protected function getClient() : Client\n    {\n        // TODO: Implement getClient() method.\n    }\n\n    /**\n     * @after\n     */\n    protected function verifyTestCaseExpectations() : void\n    {\n        $this-\u003everifyExpectations();\n    }\n\n    // ...\n}\n```\n\n*Example:*\n\n```php\npublic function testGetSpaceIsCached() : void\n{\n    $this-\u003eclient-\u003eflushSpaces();\n\n    $this-\u003eexpectSelectRequestToBeCalledOnce();\n    $this-\u003eclient-\u003egetSpace('test_space');\n    $this-\u003eclient-\u003egetSpace('test_space');\n}\n```\n\n### Prepared statements\n\nIn order to assert prepared statement allocations, use the `Tarantool\\PhpUnit\\Expectation\\PreparedStatementExpectations` trait,\nwhich contains the following methods:\n\n * `expectPreparedStatementToBe\u003cTYPE\u003e(int $count) : void`\n * `expectPreparedStatementToBe\u003cTYPE\u003eAtLeast(int $count) : void`\n * `expectPreparedStatementToBe\u003cTYPE\u003eAtMost(int $count) : void`\n * `expectPreparedStatementToBe\u003cTYPE\u003eOnce() : void`\n * `expectPreparedStatementToBeNever\u003cTYPE\u003e() : void`\n * `expectPreparedStatementToBe\u003cTYPE\u003eAtLeastOnce() : void`\n * `expectPreparedStatementToBe\u003cTYPE\u003eAtMostOnce() : void`\n\nwhere `\u003cTYPE\u003e` is either `Allocated` or `Deallocated`.\n\n*Example:*\n\n```php\npublic function testCloseDeallocatesPreparedStatement() : void\n{\n    $stmt = $this-\u003eclient-\u003eprepare('SELECT ?');\n\n    $this-\u003eexpectPreparedStatementToBeDeallocatedOnce();\n    $stmt-\u003eclose();\n}\n```\n\nTo enable all the above expectation methods in one go, use the `Tarantool\\PhpUnit\\Expectation\\Expectations` trait,\nor extend the `Tarantool\\PhpUnit\\TestCase` class.\n\n\n## Mocking\n\nThe library provides several helper classes to create test doubles for the [Tarantool Сlient](https://github.com/tarantool-php/client)\nto avoid sending real requests to the Tarantool server. For the convenience of creating such objects,\nadd the trait `TestDoubleClient` to your test class:\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse Tarantool\\PhpUnit\\Client\\TestDoubleClient;\n\nfinal class MyTest extends TestCase\n{\n    use TestDoubleClient;\n\n    // ...\n}\n```\n\n\u003e *If your test cases extend the `Tarantool\\PhpUnit\\TestCase` class, this step is not needed\n\u003e because the trait is already included in that class.*\n\nA dummy client object can be created as follows:\n\n```php\npublic function testFoo() : void\n{\n    $dummyClient = $this-\u003ecreateDummyClient();\n\n    // ...\n}\n```\n\nTo simulate specific scenarios, such as establishing a connection to a server\nor returning specific responses in a specific order from the server, use the facilities\nof the `TestDoubleClientBuilder` class. For example, to simulate the `PING` request:\n\n```php\nuse Tarantool\\Client\\Request\\PingRequest;\nuse Tarantool\\PhpUnit\\TestCase;\n\nfinal class MyTest extends TestCase\n{\n    public function testFoo() : void\n    {\n        $mockClient = $this-\u003egetTestDoubleClientBuilder()\n            -\u003eshouldSend(new PingRequest())\n            -\u003ebuild();\n\n        // ...\n    }\n\n    // ...\n}\n```\n\nAnother example, sending two `EVALUATE` requests and returning a different response for each:\n\n```php\nuse Tarantool\\Client\\RequestTypes;\nuse Tarantool\\PhpUnit\\Client\\TestDoubleFactory;\nuse Tarantool\\PhpUnit\\TestCase;\n\nfinal class MyTest extends TestCase\n{\n    public function testFoo() : void\n    {\n        $mockClient = $this-\u003egetTestDoubleClientBuilder()\n            -\u003eshouldSend(\n                RequestTypes::EVALUATE, \n                RequestTypes::EVALUATE\n            )-\u003ewillReceive(\n                TestDoubleFactory::createResponseFromData([2]),\n                TestDoubleFactory::createResponseFromData([3])\n            )-\u003ebuild();\n    \n        // ...\n    }\n\n    // ...\n}\n```\nThe above example can be simplified to:\n\n```php\n$mockClient = $this-\u003egetTestDoubleClientBuilder()\n    -\u003eshouldHandle(\n        RequestTypes::EVALUATE,\n        TestDoubleFactory::createResponseFromData([2]),\n        TestDoubleFactory::createResponseFromData([3])\n    )-\u003ebuild();\n```\n\nBesides, the builder allows setting custom `Connection` and `Packer` instances:\n\n```php\n$stubClient = $this-\u003egetMockClientBuilder()\n    -\u003ewillUseConnection($myConnection)\n    -\u003ewillUsePacker($myPacker)\n    -\u003ebuild();\n```\n\n## Testing\n\nBefore running tests, the development dependencies must be installed:\n\n```bash\ncomposer install\n```\n\nThen, to run all the tests:\n\n```bash\nvendor/bin/phpunit\nvendor/bin/phpunit -c phpunit-extension.xml\n```\n\n\n## License\n\nThe library is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.\n","funding_links":["https://github.com/sponsors/rybakit"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarantool-php%2Fphpunit-extras","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftarantool-php%2Fphpunit-extras","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarantool-php%2Fphpunit-extras/lists"}