{"id":24780394,"url":"https://github.com/webgriffe/functional-test-trait","last_synced_at":"2025-03-24T04:40:25.169Z","repository":{"id":62547764,"uuid":"82272702","full_name":"webgriffe/functional-test-trait","owner":"webgriffe","description":"Trait which enables functional testing in Magento with  EcomDev_PHPUnit and using a CgiHttpKernel.","archived":false,"fork":false,"pushed_at":"2018-02-13T17:14:45.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-07T21:44:02.054Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webgriffe.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":"2017-02-17T08:04:27.000Z","updated_at":"2017-02-17T08:05:11.000Z","dependencies_parsed_at":"2022-11-02T22:16:15.053Z","dependency_job_id":null,"html_url":"https://github.com/webgriffe/functional-test-trait","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webgriffe%2Ffunctional-test-trait","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webgriffe%2Ffunctional-test-trait/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webgriffe%2Ffunctional-test-trait/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webgriffe%2Ffunctional-test-trait/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webgriffe","download_url":"https://codeload.github.com/webgriffe/functional-test-trait/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245212107,"owners_count":20578438,"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":"2025-01-29T10:30:18.030Z","updated_at":"2025-03-24T04:40:25.149Z","avatar_url":"https://github.com/webgriffe.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Webgriffe Functional Test Trait\n===============================\n\nTrait which enables functional testing in Magento with [EcomDev_PHPUnit](https://github.com/EcomDev/EcomDev_PHPUnit) and using a [CgiHttpKernel](https://github.com/webgriffe/CgiHttpKernel).\n\nInstallation\n------------\n\nImport it using Composer (note, it's a dev dependency):\n\n```\n$ composer require --dev webgriffe/functional-test-trait\n```\n    \nUsage\n-----\n\nThe suggested usage of this trait is in combination with [EcomDev_PHPUnit](https://github.com/EcomDev/EcomDev_PHPUnit) and the [Webgriffe's Magento Config Extension](https://github.com/webgriffe/config-extension).\nSo, first of all, add these dependencies to your Magento project:\n\n```\n$ composer require --dev webgriffe/functional-test-trait ecomdev/ecomdev_phpunit\n$ composer require webgriffe/config-extension\n```\t\n\t\nAnother suggested dependency is `symfony/css-selector`, which allows to select DOM elements using CSS selectors in your functional tests:\n\n```\n$ composer require --dev symfony/css-selector\n```\n\nThen setup your test suite as usual with EcomDev_PHPUnit.\n\nAlso you can add a `\u003ctestsuite\u003e` node to your `phpunit.xml.dist` to group your functional tests in a dedicated test suite:\n\n```xml\n\u003ctestsuite name=\"My Project Functional Test Suite\"\u003e\n    \u003cdirectory suffix=\"Test.php\"\u003etests\u003c/directory\u003e\n\u003c/testsuite\u003e\n``` \n\nAlso, to avoid session issues, you should make sure that the `base_url` is always the same during tests setup and tests run. You can do this by setting the `MAGE_ENVIRONMENT` server variable through your `phpunit.xml.dist`:\n\n```xml\n\u003cphp\u003e\n    \u003cserver name=\"MAGE_ENVIRONMENT\" value=\"test\" /\u003e\n\u003c/php\u003e\n```\nAnd then set the `base_url` with the override configuration file for that environment (`app/etc/config-override.test.xml.dist`), with the content:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cconfig\u003e\n    \u003cdefault\u003e\n        \u003cweb\u003e\n            \u003csecure\u003e\n                \u003cbase_url\u003ehttp://url-functional.test/\u003c/base_url\u003e\n            \u003c/secure\u003e\n            \u003cunsecure\u003e\n                \u003cbase_url\u003ehttp://url-functional.test/\u003c/base_url\u003e\n            \u003c/unsecure\u003e\n        \u003c/web\u003e\n    \u003c/default\u003e\n\u003c/config\u003e\n```\n\nNow you're ready to write your first Magento functional test. \nPut a test your `tests/` directory. For example, `HomepageTest.php`:\n\n```php\n\u003c?php\n\t\nclass HomepageTest extends EcomDev_PHPUnit_Test_Case_Controller\n{\n    use Webgriffe_FunctionalTest_Trait;\n\t\n    /**\n     * @loadFixture category.yaml\n     */\n    public function testHome()\n    {\n        $client = self::createClient();\n        $crawler = $client-\u003erequest('GET', '/');\n\t\n        $this-\u003eassertTrue($client-\u003egetResponse()-\u003eisSuccessful());\n        $this-\u003eassertContains('Default welcome msg!', $crawler-\u003efilter('div.welcome-msg')-\u003etext());\n        $this-\u003eassertCount(1, $crawler-\u003efilter('ul.nav.mega-menu'));\n        $categoryLink = $crawler-\u003efilter('ul.nav.mega-menu')-\u003efilter('li')-\u003eeq(0)-\u003efilter('a');\n        $this-\u003eassertCount(1, $categoryLink);\n        $this-\u003eassertContains('My Category', $categoryLink-\u003etext());\n\t\n        $crawler = $client-\u003eclick($categoryLink-\u003elink());\n\t\n        $this-\u003eassertTrue($client-\u003egetResponse()-\u003eisSuccessful());\n        $this-\u003eassertContains('My Category', $crawler-\u003efilter('div.page-title h1')-\u003etext());\n    }\n}\n```\nIt's important that your test extends the `EcomDev_PHPUnit_Test_Case_Controller` class to correctly load Magento's frontend area configuration.\n\t\nAs you may notice there is the `@loadFixture` annotation which loads data from `category.yaml` file located at `tests/HomepageTest/fixtures/category.yaml` which content could be:\n\n```yaml\neav:\n  catalog_category:\n    - entity_id: 3\n      name: My Category\n      parent_id: 2\n      path: 1/2/3\n      level: 2\n      url_key: my-category\n      is_active: 1\n      include_in_menu: 1\n```\n\nMultiple website supported\n--------------------------\n\nIf you want you can create a client for different websites:\n\n```php\n$secondaryWebsite = Mage::app()-\u003egetWebsite('otherwebsite');\n$client = self::createClient($secondaryWebsite);\n```\n\nThis will use the secondary website base URL and sets the `MAGE_RUN_TYPE` and `MAGE_RUN_CODE` environment variables.\n\nTest debug with xDebug\n----------------------\n\nSometimes you need to debug your application during functional test execution. To do so you create an xDebug enabled client by passing `true` to the `createClient()` method:\n\n```php\n$client = self::createClient(true);\n```\nDon't forget to configure the `XDEBUG_SESSION` cookie value in your `app/etc/local.xml` file according your IDE settings. For example for PHPStorm on MAC this value should be `PHPSTORM`:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cconfig\u003e\n\t\u003c!-- ... --\u003e\n\t\u003cphpunit\u003e\n        \u003cfunctional\u003e\n            \u003cxdebug_session\u003ePHPSTORM\u003c/xdebug_session\u003e\n        \u003c/functional\u003e\n    \u003c/phpunit\u003e\n\u003c/config\u003e\n```\n\nOpen response in browser\n------------------------\nSometimes is useful to open last response returned by Magento in your browser for further analysis. To do so you can use the `openResponseInBrowser()` method, for example:\n\n```php\n$client = self::createClient();\n$client-\u003erequest('GET', '/');\nself::openResponseInBrowser($client)\n```\nThis will dump the last response's content in a temporary file and opens it with a system command. Don't forget to configure the open file in browser command for your system in the `app/etc/local.xml` (the `%s` placeholder will be replaced with the temporary file path). For example on MAC you can use `open %s`:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cconfig\u003e\n\t\u003c!-- ... --\u003e\n\t\u003cphpunit\u003e\n        \u003cfunctional\u003e\n            \u003copen_browser_command\u003eopen %s\u003c/open_browser_command\u003e\n        \u003c/functional\u003e\n    \u003c/phpunit\u003e\n\u003c/config\u003e\n```\n\nDifferent front controller\n--------------------------\n\nIt's also possible to specify an alternative front controller instead of `index.php`. This is extremely useful when you have to serve static file through the front controller `get.php` provided by Magento:\n\n```php\n$staticFilesClient = self::createClient(null, false, 'test', 'get.php');\n$staticFilesClient-\u003erequest('GET', '/path/to/media/file.jpg');\n```\n\nLicense\n-------\n\nThis library is under the MIT license. See the complete license in the LICENSE file.\n\nCredits\n-------\n\nDeveloped by [Webgriffe®](http://www.webgriffe.com/). Please, report to us any bug or suggestion by GitHub issues.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebgriffe%2Ffunctional-test-trait","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebgriffe%2Ffunctional-test-trait","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebgriffe%2Ffunctional-test-trait/lists"}