{"id":15284051,"url":"https://github.com/onthegosystems/otgs-unit-tests-framework","last_synced_at":"2025-04-12T23:20:42.298Z","repository":{"id":57033659,"uuid":"117663869","full_name":"OnTheGoSystems/otgs-unit-tests-framework","owner":"OnTheGoSystems","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-16T07:39:09.000Z","size":103,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":19,"default_branch":"develop","last_synced_at":"2025-03-26T17:21:17.900Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OnTheGoSystems.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-16T09:38:18.000Z","updated_at":"2023-05-02T10:26:41.000Z","dependencies_parsed_at":"2022-08-23T20:50:36.728Z","dependency_job_id":null,"html_url":"https://github.com/OnTheGoSystems/otgs-unit-tests-framework","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OnTheGoSystems%2Fotgs-unit-tests-framework","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OnTheGoSystems%2Fotgs-unit-tests-framework/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OnTheGoSystems%2Fotgs-unit-tests-framework/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OnTheGoSystems%2Fotgs-unit-tests-framework/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OnTheGoSystems","download_url":"https://codeload.github.com/OnTheGoSystems/otgs-unit-tests-framework/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248644011,"owners_count":21138542,"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":[],"created_at":"2024-09-30T14:48:55.272Z","updated_at":"2025-04-12T23:20:42.278Z","avatar_url":"https://github.com/OnTheGoSystems.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PhpUnit\n\n* Create a `tests` directory in your project\n* Create a `phpunit` subdirectory in `tests`[^1]\n* Copy `samples/phpunit.xml` file in the root of your project[^2]\n  * Change `test-suite-name` into something appropriated\n  * Most likely, you won't need to change anything else in that file\n* Copy `samples/bootstrap.php` into `tests/phpunit`\n  * Read the comments in this file and make the appropriated changes\n* Run `composer require --dev otgs/unit-tests-framework`[^3]\n* Create another `tests` subdirectory in `tests/phpunit`\n  * Write your tests there\n  * Unless you have customized the `phpunit.xml` file, you don't need to name the test files and classes in any particular way\n* Run `phpunit` from your project's root to start your tests[^4]\n\n[^1]: it is recommended to keep your PHPUnit tests into a subfolder, because there are good changes you want to add other kind of tests (e.g. QUnit, PHPSpec, etc.)\n[^2]: in case, for whatever reason, you can't do that, copy this file in `tests/phpunit` or wherever you find it more convenient \n[^3]: In some cases, you may want to use `composer require --dev otgs/unit-tests-framework:dev-develop`. When you do that, you may also need to run this command first `composer config minimum-stability dev`\n[^4]: If you've placed the `phpunit.xml` you need to either move to that directory first, or tell phpunit where the file is with `phpunit --configuration path/to/phpunit.xml`\n\n## How to use the OTGS_TestCase class\n\nIn order to take the most of this library, all your tests classes should extext `OTGS_TestCase`.\n\nOnce you do that, this will happen:\n\n1. `\\OTGS_TestCase::setupBeforeClass`:\n  - `$_GET` and `$_POST` are set to an empty array\n  - An instance of `FactoryMuffin` is provided: you can refer to it as `self::$fm` (see \"Resources and dependencies\" for more details)\n2. `\\OTGS_TestCase::setUp`\n  - `FunctionMocker` is initialized (see \"Resources and dependencies\" for more details)\n  - `WP_Mock` is initialized (see \"Resources and dependencies\" for more details)\n3. `\\OTGS_TestCase::tearDown`\n  - `WP_Mock` is destroyed\n  - `FunctionMocker ` is destroyed\n  - `Mockery` is destroyed (just in case it has been used)\n4. `\\OTGS_TestCase::tearDownAfterClass`\n  - Deletes all models created with `FactoryMuffin`\n\n### Mock WP Core functions\nThis class also provide an helper method to quickly mock the functions defined by WordPress by using the `\\OTGS_TestCase::get_mocked_wp_core_functions` which returns an instance of `OTGS_Mocked_WP_Core_Functions`.\n\n`OTGS_Mocked_WP_Core_Functions` organize mocks in methods named using the same name of the file where the function is defined in WordPress codebase.\n\nFor instance, to mock of all functions defined in `post.php` like `get_post`, in your test you should simply call `$this-\u003eget_mocked_wp_core_functions()-\u003epost()`.\n\nTo mock `add_query_arg` yo call `$this-\u003eget_mocked_wp_core_functions()-\u003efunctions()` because `add_query_arg` is defined in `functions.php`.\n\n`OTGS_Mocked_WP_Core_Functions` tries to handle dependencies.\n\nSo, if you call `$this-\u003eget_mocked_wp_core_functions()-\u003epost()` to mock `wp_insert_post`, you automatically call `$this-\u003eget_mocked_wp_core_functions()-\u003epost()`, so to get all the meta related functions mocked as well.  \nFinally, there is a \"mock all\" method you could use (though is discouraged) with `$this-\u003emock_all_core_functions()`.\n\n### Stub WP common classes\n`\\OTGS_TestCase` provides a helpful way to quickly get a stub of some of the most commonly used classes in WordPress.\n\nBy calling `$this-\u003estubs-\u003ewpdb()` you will get a stub you can pass as a dependency of the classes you are testing.\nIf you need to control the behavior of this stub, you just use the standard PHPUnit mock helpers.\n\nE.g. 1:  \n\n```php\n$wpdb = $this-\u003estubs-\u003ewpdb();  \n$wpdb-\u003emethod( 'get_var' )-\u003ewillReturn( 1 );\n```\nE.g. 2:\n \n```php\n$results = array(\n\tarray( 'translation_id' =\u003e 1, 'element_id' =\u003e 1, 'language_code' =\u003e 'en', 'source_language_code' =\u003e null, 'trid' =\u003e 1, 'element_type' =\u003e 'post_page' ),\n\tarray( 'translation_id' =\u003e 2, 'element_id' =\u003e 2, 'language_code' =\u003e 'fr', 'source_language_code' =\u003e 'en', 'trid' =\u003e 1, 'element_type' =\u003e 'post_page' ),\n);\n$wpdb = $this-\u003ethis-\u003ewpdb();\n$wpdb-\u003eexpects( $this-\u003eexactly( 2 ) )-\u003emethod( 'get_results' )-\u003ewillReturn( $results );\n```\n\nOther stubs you can get:\n\n- `WP_Widget` with `$this-\u003estubs-\u003eWP_Widget()`\n- `WP_Theme` with `$this-\u003estubs-\u003eWP_Theme()`\n- `WP_Filesystem_Direct` with `$this-\u003estubs-\u003eWP_Filesystem_Direct()`\n- `WP_Query` with `$this-\u003estubs-\u003eWP_Query()`\n\nIt is important to know that, if you only need the class to be defined (e.g. hard-dependency, or sub-classing), you don't need to assig the stub to a variable: just call the method.\n\nA good example is with WordPress' widgets, where you may have your own widget which is supposed to extend `WP_Widget`.\n\nIn this case, unless you want to mock some of the `WP_Widget` methods, you simply call `$this-\u003estubs-\u003eWP_Widget()`, then write your tests.  \nThe class which extends `WP_Widget` will find a definition of this class, with all the methods (doing nothing).\n\n## Resources and dependencies\n\nBelow are some resources on writing unit tests which lead to the creation of this library and links to the libraries included here:\n\n* Start from here for a general explanation: http://wordpress.stackexchange.com/a/164138/7291\n* 10up's WP_Mock`: https://github.com/10up/wp_mock\n* Mockery: https://github.com/padraic/mockery\n* Function mocker: https://github.com/lucatume/function-mocker\n* Factory Muffin: https://github.com/thephpleague/factory-muffin\n* Factory Muffin Faker: https://github.com/thephpleague/factory-muffin-faker\n* The DomCrawler Component: http://symfony.com/doc/current/components/dom_crawler.html\n* The CssSelector Component: http://symfony.com/doc/current/components/css_selector.html\n* php-loremipsum: https://github.com/joshtronic/php-loremipsum","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonthegosystems%2Fotgs-unit-tests-framework","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonthegosystems%2Fotgs-unit-tests-framework","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonthegosystems%2Fotgs-unit-tests-framework/lists"}