{"id":15014394,"url":"https://github.com/cspray/assert-throws","last_synced_at":"2025-06-30T23:03:11.523Z","repository":{"id":241395303,"uuid":"806773140","full_name":"cspray/assert-throws","owner":"cspray","description":"A library for testing complex exceptions","archived":false,"fork":false,"pushed_at":"2025-03-15T11:42:28.000Z","size":12,"stargazers_count":6,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-30T23:02:48.778Z","etag":null,"topics":["phpunit"],"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/cspray.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2024-05-27T21:51:27.000Z","updated_at":"2025-03-15T11:38:52.000Z","dependencies_parsed_at":"2024-05-28T05:34:37.803Z","dependency_job_id":"1dbe267a-4911-45a3-b3d7-a13320f60ddc","html_url":"https://github.com/cspray/assert-throws","commit_stats":null,"previous_names":["cspray/assert-throws"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/cspray/assert-throws","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fassert-throws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fassert-throws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fassert-throws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fassert-throws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cspray","download_url":"https://codeload.github.com/cspray/assert-throws/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fassert-throws/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262863687,"owners_count":23376452,"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"],"created_at":"2024-09-24T19:45:34.772Z","updated_at":"2025-06-30T23:03:11.416Z","avatar_url":"https://github.com/cspray.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cspray/assert-throws\n\nProvides an alternative way to test code that throws exceptions using PHPUnit 10 or 11. The primary benefit being access \nto the thrown Exception for further inspection. This might prove useful when you need to check for details of an \nexception that aren't available to be expected in PHPUnit. The below example acts as a Quick Start, for more details \ncheck out the [Detailed Guide](#Detailed-Guide) below.\n\n```php\n\u003c?php declare(strict_types=1);\n\nuse Cspray\\AssertThrows\\ThrowableAssert;\nuse PHPUnit\\Framework\\TestCase;\n\nfinal class MyTest extends TestCase {\n    public function testExceptionPreviousInstanceOf() : void {\n        $throwable = ThrowableAssert::assertThrows(\n            static fn() =\u003e throw new RuntimeException(previous: new BadMethodCallException())\n        );\n        \n        self::assertInstanceOf(BadMethodCallException::class, $throwable-\u003egetPrevious());\n    }\n}\n```\n\n## Installation\n\n[Composer](https://getcomposer.org/) is the only supported method to install this package.\n\n```shell\ncomposer require --dev cspray/assert-throws\n```\n\n## Detailed Guide\n\nPHPUnit provides a reasonable way to test for expected exceptions out-of-the-box. For most situations, the PHPUnit provided\nmethods should be sufficient. However, there are scenarios where you might need to test exception throwing code in a way \nnot readily available in PHPUnit. Examples of this include code where you want to test the _previous_ exception or if the\nexception includes domain specific information. It is in these situations that this library is most appropriate.\n\nAll the assertions provided by this library takes a callable that is expected to throw an exception. If an exception \nis not thrown a `PHPUnit\\Framework\\ExpectationFailedException` will be thrown resulting in a test failure. Otherwise, the \nthrown exception will be returned for additional assertions.\n\nThe following static methods are available on the `Cspray\\AssertThrows\\ThrowableAssert` class:\n\n```php\n\u003c?php\n\nuse \\Cspray\\AssertThrows\\ThrowableAssert;\n\n$throwable = ThrowableAssert::assertThrows(static fn() =\u003e throw new RuntimeException());\n\n$throwable = ThrowableAssert::assertThrowsExceptionType(\n    static fn() =\u003e throw new BadMethodCallException(),\n    BadMethodCallException::class\n);\n\n$throwable = ThrowableAssert::assertThrowsExceptionTypeWithMessage(\n    static fn() =\u003e throw new RuntimeException('My exception message'),\n    RuntimeException::class,\n    'My exception message'\n);\n```\n\nIn addition to the `ThrowableAssert` class with static methods there are global functions available:\n\n```php\n\u003c?php\n\nuse function Cspray\\AssertThrows\\assertThrows;\nuse function Cspray\\AssertThrows\\assertThrowsExceptionType;\nuse function Cspray\\AssertThrows\\assertThrowsExceptionTypeWithMessage;\n\n$throwable = assertThrows(static fn() =\u003e throw new RuntimeException());\n\n$throwable = assertThrowsExceptionType(\n    static fn() =\u003e throw new BadMethodCallException(),\n    BadMethodCallException::class\n);\n\n$throwable = assertThrowsExceptionTypeWithMessage(\n    static fn() =\u003e throw new RuntimeException('My exception message'),\n    RuntimeException::class,\n    'My exception message'\n);\n```\n\nAs well as a trait to use in your `PHPUnit\\Framework\\TestCase` implementations:\n\n```php\n\u003c?php\n\nuse Cspray\\AssertThrows\\ThrowableAssertTestCaseMethods;\nuse PHPUnit\\Framework\\TestCase;\n\nclass MyTestCase extends TestCase {\n    use ThrowableAssertTestCaseMethods;\n    \n    public function testAssertThrows() : void {\n        $throwable = self::assertThrows(static fn() =\u003e new RuntimeException());\n    }\n    \n    public function testAssertThrowsExceptionType() : void {\n        $throwable = self::assertThrowsExceptionType(\n            static fn() =\u003e throw new BadMethodCallException(),\n            BadMethodCallException::class\n        );\n    }\n    \n    public function testAssertThrowsExceptionTypeWithMessage() : void {\n        $throwable = self::assertThrowsExceptionTypeWithMessage(\n            static fn() =\u003e throw new RuntimeException('My exception message'),\n            RuntimeException::class,\n            'My exception message'\n        );\n    }\n}\n```\n\nWhich method you use is a personal preference. Ultimately, all examples utilize the static methods available in the \n`ThrowableAssert` class.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fassert-throws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcspray%2Fassert-throws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fassert-throws/lists"}