{"id":15159635,"url":"https://github.com/zrnik/phpunit-exceptions","last_synced_at":"2026-01-30T14:16:43.659Z","repository":{"id":57092097,"uuid":"359431319","full_name":"Zrnik/PHPUnit-Exceptions","owner":"Zrnik","description":"Trait for easier exception testing in PHPUnit.","archived":false,"fork":false,"pushed_at":"2024-03-09T11:55:12.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-02T11:05:36.311Z","etag":null,"topics":["php","phpunit"],"latest_commit_sha":null,"homepage":"","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/Zrnik.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-19T11:16:23.000Z","updated_at":"2024-04-11T11:22:39.000Z","dependencies_parsed_at":"2024-09-22T09:00:50.827Z","dependency_job_id":"8b7834c9-86b4-416c-a4a5-acb2d0f5671c","html_url":"https://github.com/Zrnik/PHPUnit-Exceptions","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"e27a4348a2ad524581600b7ee650a537c777da92"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/Zrnik/PHPUnit-Exceptions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zrnik%2FPHPUnit-Exceptions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zrnik%2FPHPUnit-Exceptions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zrnik%2FPHPUnit-Exceptions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zrnik%2FPHPUnit-Exceptions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Zrnik","download_url":"https://codeload.github.com/Zrnik/PHPUnit-Exceptions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zrnik%2FPHPUnit-Exceptions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259737911,"owners_count":22903869,"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":["php","phpunit"],"created_at":"2024-09-26T21:41:01.452Z","updated_at":"2026-01-30T14:16:43.629Z","avatar_url":"https://github.com/Zrnik.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHPUnit Exceptions\n\nTrait for easier exception testing in [PHPUnit](https://github.com/sebastianbergmann/phpunit).\n\n## Requirements\n\n```\n{\n    \"php\": \"^8\",\n    \"phpunit/phpunit\": \"^9|^10|^11\"\n}\n```\n\n## Usage:\n\nAdd a trait `use Zrnik\\PHPUnit\\Exceptions;` to your test case. \nThen the testing assertions are available.\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse Tests\\ExampleObject;\nuse Tests\\NotInRangeException;\n\nclass ExampleTest extends TestCase\n{\n    use \\Zrnik\\PHPUnit\\Exceptions; // add this trait to your TestCase\n\n    public function textExample(): void\n    {        \n        $exampleObject = new ExampleObject();\n        \n        $this-\u003eassertExceptionThrown(\n            NotInRangeException::class, // Expected Exception Type\n            \n            // Closure running the code we expect to get an exception from.\n            function () use ($exampleObject) {\n                $exampleObject-\u003eassertRange(0);\n            }\n        );\n        \n        $this-\u003eassertNoExceptionThrown(\n            function () use ($exampleObject) {\n                $exampleObject-\u003eassertRange(1);\n                $exampleObject-\u003eassertRange(10);\n            }\n        );\n        \n        $this-\u003eassertExceptionThrown(\n            NotInRangeException::class,\n            function () use ($exampleObject) {\n                $exampleObject-\u003eassertRange(11);\n            }\n        );\n    }\n}\n```\n\n## Why\n\nI had problem with default `expectException`. The problem\nwas creating unnecessary amount of methods or using try/catch blocks\nto check for exceptions. All exceptions are available in \nthe [./tests/AssertExceptionTest.php](./tests/AssertExceptionTest.php) file.\n\n**Note:** *Maybe, im just bad at testing. It's **totally** possible...*\n\n### Example 1.: Using too many methods...\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse Tests\\ExampleObject;\nuse Tests\\NotInRangeException;\n\nclass ExampleTest extends TestCase\n{\n    public function test_ExpectException_First(): void\n    {\n        $exampleObject = new ExampleObject();\n        $this-\u003eexpectException(NotInRangeException::class);\n        $exampleObject-\u003eassertRange(0);\n        //The execution ends here, the method will not continue,\n        // after first exception thrown, so I need to create\n        // method for every exception tested...\n    }\n\n    public function test_ExpectException_Second(): void\n    {\n        $exampleObject = new ExampleObject();\n        $this-\u003eexpectException(NotInRangeException::class);\n        $exampleObject-\u003eassertRange(11);\n    }\n\n    public function test_OK_Values(): void\n    {\n        $exampleObject = new ExampleObject();\n\n        $exampleObject-\u003eassertRange(1);\n        $exampleObject-\u003eassertRange(10);\n\n        $this-\u003eaddToAssertionCount(2); // Yey! Not thrown!\n    }\n}\n```\n\n\n### Example 2.: Using try/catch block...\n\n```php\nuse PHPUnit\\Framework\\AssertionFailedError;\nuse PHPUnit\\Framework\\TestCase;\nuse Tests\\ExampleObject;\nuse Tests\\NotInRangeException;\n\nclass ExampleTest extends TestCase\n{\n    public function test_TryCatch(): void\n    {\n        $exampleObject = new ExampleObject();\n\n        try {\n            $exampleObject-\u003eassertRange(0);\n            // I don't want to write so long error text everytime I am checking for exceptions!\n            throw new AssertionFailedError(sprintf(\"Exception '%s' expected, but not thrown!\", NotInRangeException::class));\n        } catch (NotInRangeException $ex) {\n            $this-\u003eaddToAssertionCount(1); // Yey! Thrown!\n        }\n\n        $exampleObject-\u003eassertRange(1);\n        $exampleObject-\u003eassertRange(10);\n        $this-\u003eaddToAssertionCount(2); // Yey! Not thrown!\n\n        try {\n            $exampleObject-\u003eassertRange(11);\n            throw new AssertionFailedError(sprintf(\"Exception '%s' expected, but not thrown!\", NotInRangeException::class));\n        } catch (NotInRangeException $ex) {\n            $this-\u003eaddToAssertionCount(1); // Yey! Thrown!\n        }\n\n    }\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzrnik%2Fphpunit-exceptions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzrnik%2Fphpunit-exceptions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzrnik%2Fphpunit-exceptions/lists"}