{"id":15226368,"url":"https://github.com/selective-php/test-traits","last_synced_at":"2025-10-27T23:37:38.855Z","repository":{"id":45647198,"uuid":"332190326","full_name":"selective-php/test-traits","owner":"selective-php","description":"Test Traits","archived":false,"fork":false,"pushed_at":"2025-05-26T19:23:11.000Z","size":70,"stargazers_count":13,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-27T23:37:35.876Z","etag":null,"topics":["php","slim","test"],"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/selective-php.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-01-23T11:08:30.000Z","updated_at":"2025-05-26T19:20:41.000Z","dependencies_parsed_at":"2023-01-21T19:29:53.302Z","dependency_job_id":"3d90b59f-869a-4991-b510-0e0d2cb874ef","html_url":"https://github.com/selective-php/test-traits","commit_stats":{"total_commits":63,"total_committers":5,"mean_commits":12.6,"dds":"0.17460317460317465","last_synced_commit":"91b5af54a0763445ed27946463a9d892516efa72"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/selective-php/test-traits","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Ftest-traits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Ftest-traits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Ftest-traits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Ftest-traits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/selective-php","download_url":"https://codeload.github.com/selective-php/test-traits/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Ftest-traits/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281361403,"owners_count":26487881,"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-27T02:00:05.855Z","response_time":61,"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":["php","slim","test"],"created_at":"2024-09-28T20:04:50.563Z","updated_at":"2025-10-27T23:37:38.837Z","avatar_url":"https://github.com/selective-php.png","language":"PHP","readme":"# selective/test-traits\n\nA collection of PHPUnit test traits.\n\n[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/test-traits.svg)](https://packagist.org/packages/selective/test-traits)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)\n[![Build Status](https://github.com/selective-php/test-traits/workflows/build/badge.svg)](https://github.com/selective-php/test-traits/actions)\n[![Total Downloads](https://img.shields.io/packagist/dt/selective/test-traits.svg)](https://packagist.org/packages/selective/test-traits/stats)\n\n\n## Requirements\n\n* PHP 8.1+\n\n## Installation\n\n```bash\ncomposer require selective/test-traits --dev\n```\n\n## Traits\n\n### MailerTestTrait\n\nRequirements: `composer require symfony/mailer`\n\nDI container setup example:\n\n```php\nuse Symfony\\Component\\EventDispatcher\\EventDispatcher;\nuse Symfony\\Component\\EventDispatcher\\EventDispatcherInterface;\nuse Symfony\\Component\\Mailer\\EventListener\\EnvelopeListener;\nuse Symfony\\Component\\Mailer\\EventListener\\MessageListener;\nuse Symfony\\Component\\Mailer\\EventListener\\MessageLoggerListener;\nuse Symfony\\Component\\Mailer\\Mailer;\nuse Symfony\\Component\\Mailer\\MailerInterface;\nuse Symfony\\Component\\Mailer\\Transport;\nuse Symfony\\Component\\Mailer\\Transport\\TransportInterface;\n// ...\n\nreturn [\n    // Mailer\n    MailerInterface::class =\u003e function (ContainerInterface $container) {\n        return new Mailer($container-\u003eget(TransportInterface::class));\n    },\n\n    // Mailer transport\n    TransportInterface::class =\u003e function (ContainerInterface $container) {\n        $settings = $container-\u003eget('settings')['smtp'];\n\n        // smtp://user:pass@smtp.example.com:25\n        $dsn = sprintf(\n            '%s://%s:%s@%s:%s',\n            $settings['type'],\n            $settings['username'],\n            $settings['password'],\n            $settings['host'],\n            $settings['port']\n        );\n\n        $eventDispatcher = $container-\u003eget(EventDispatcherInterface::class);\n\n        return Transport::fromDsn($dsn, $eventDispatcher);\n    },\n\n    EventDispatcherInterface::class =\u003e function () {\n        $eventDispatcher = new EventDispatcher();\n        $eventDispatcher-\u003eaddSubscriber(new MessageListener());\n        $eventDispatcher-\u003eaddSubscriber(new EnvelopeListener());\n        $eventDispatcher-\u003eaddSubscriber(new MessageLoggerListener());\n\n        return $eventDispatcher;\n    },\n    \n    // ...\n],\n```\n\n**Usage**:\n\nPHPUnit test case:\n\n```php\n\u003c?php\n\nnamespace App\\Test\\TestCase;\n\nuse PHPUnit\\Framework\\TestCase;\nuse Selective\\TestTrait\\Traits\\ContainerTestTrait;\nuse Selective\\TestTrait\\Traits\\MailerTestTrait;\nuse Symfony\\Component\\Mailer\\MailerInterface;\nuse Symfony\\Component\\Mime\\Email;\n\nfinal class MailerExampleTest extends TestCase\n{\n    use ContainerTestTrait;\n    use MailerTestTrait;\n\n    public function testMailer(): void\n    {\n        $mailer = $this-\u003econtainer-\u003eget(MailerInterface::class);\n\n        // Send email\n        $email = (new Email())\n            -\u003efrom('hello@example.com')\n            -\u003eto('you@example.com')\n            -\u003esubject('Time for Symfony Mailer!')\n            -\u003etext('Sending emails is fun again!')\n            -\u003ehtml('\u003cp\u003eMy HTML content\u003c/p\u003e');\n\n        $mailer-\u003esend($email);\n\n        $this-\u003eassertEmailCount(1);\n        $this-\u003eassertEmailTextBodyContains($this-\u003egetMailerMessage(), 'Sending emails is fun again!');\n        $this-\u003eassertEmailHtmlBodyContains($this-\u003egetMailerMessage(), '\u003cp\u003eMy HTML content\u003c/p\u003e');\n    }\n}\n```\n\n### HttpTestTrait\n\nRequirements\n\n* Any PSR-7 and PSR-17 factory implementation.\n\n```\ncomposer require nyholm/psr7-server\ncomposer require nyholm/psr7\n```\n\n**Provided methods**\n\n* `createRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface`\n* `createFormRequest(string $method, $uri, array $data = null): ServerRequestInterface`\n* `createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface`\n\n**Usage**\n\n```php\n\u003c?php\n\nnamespace App\\Test\\TestCase;\n\nuse PHPUnit\\Framework\\TestCase;\nuse Selective\\TestTrait\\Traits\\ContainerTestTrait;\nuse Selective\\TestTrait\\Traits\\HttpTestTrait;\n\nclass GetUsersTestAction extends TestCase\n{\n    use ContainerTestTrait;\n    use HttpTestTrait;\n     \n    public function test(): void\n    {\n        $request = $this-\u003ecreateRequest('GET', '/api/users');\n        $response = $this-\u003eapp-\u003ehandle($request);\n        $this-\u003eassertSame(200, $response-\u003egetStatusCode());\n    }\n}\n```\n\nCreating a request with query string parameters:\n\n```php\n$request = $this-\u003ecreateRequest('GET', '/api/users')\n    -\u003ewithQueryParams($queryParams);\n```\n\n## RouteTestTrait\n\nA Slim 4 framework router test trait.\n\nRequirements\n\n* A Slim 4 framework application\n\n**Provided methods:**\n\n* `urlFor(string $routeName, array $data = [], array $queryParams = []): string`\n\n**Usage:**\n\n```php\n\u003c?php\n\nnamespace App\\Test\\TestCase;\n\nuse PHPUnit\\Framework\\TestCase;\nuse Selective\\TestTrait\\Traits\\ContainerTestTrait;\nuse Selective\\TestTrait\\Traits\\HttpTestTrait;\nuse Selective\\TestTrait\\Traits\\RouteTestTrait;\n\nfinal class GetUsersTestAction extends TestCase\n{\n    use ContainerTestTrait;\n    use HttpTestTrait;\n    use RouteTestTrait;\n     \n    public function test(): void\n    {\n        $request = $this-\u003ecreateRequest('GET', $this-\u003eurlFor('get-users'));\n        $response = $this-\u003eapp-\u003ehandle($request);\n        $this-\u003eassertSame(200, $response-\u003egetStatusCode());\n    }\n}\n```\n\nCreating a request with a named route and query string parameters:\n\n```php\n$request = $this-\u003ecreateRequest('GET',  $this-\u003eurlFor('get-users'))\n    -\u003ewithQueryParams($queryParams);\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselective-php%2Ftest-traits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fselective-php%2Ftest-traits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselective-php%2Ftest-traits/lists"}